<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package wheel_alignment_service
 */

function wheel_alignment_service_customize_css() {
    ?>
    <style type="text/css">
        :root {
            --primary-color: <?php echo esc_html( get_theme_mod( 'wheel_alignment_service_primary_color', '#FFC107' ) ); ?>;
        }
        a.asterthemes-button{
            background-color: <?php echo esc_html( get_theme_mod( 'wheel_alignment_service_primary_color', '#FFC107' ) ); ?>;
        }
    </style>
    <?php
}
add_action( 'wp_head', 'wheel_alignment_service_customize_css' );

function wheel_alignment_service_add_custom_script_in_footer() {
    if ( get_theme_mod( 'wheel_alignment_service_enable_sticky_header', false ) ) {
        ?>
        <script>
            jQuery(document).ready(function($) {
                $(window).on('scroll', function() {
                    var scroll = $(window).scrollTop();
                    if (scroll > 0) {
                        $('.navigation-menus.hello').addClass('is-sticky');
                    } else {
                        $('.navigation-menus.hello').removeClass('is-sticky');
                    }
                });
            });
        </script>
        <?php
    }
}
add_action( 'wp_footer', 'wheel_alignment_service_add_custom_script_in_footer' );

function wheel_alignment_service_enqueue_selected_fonts() {
    $wheel_alignment_service_fonts_url = wheel_alignment_service_get_fonts_url();
    if (!empty($wheel_alignment_service_fonts_url)) {
        wp_enqueue_style('wheel-alignment-service-google-fonts', $wheel_alignment_service_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'wheel_alignment_service_enqueue_selected_fonts');

function wheel_alignment_service_layout_customizer_css() {
    $wheel_alignment_service_margin = get_theme_mod('wheel_alignment_service_layout_width_margin', 50);
    ?>
    <style type="text/css">
        body.site-boxed--layout #page  {
            margin: 0 <?php echo esc_attr($wheel_alignment_service_margin); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_layout_customizer_css');

function wheel_alignment_service_blog_layout_customizer_css() {
    // Retrieve the blog layout option
    $wheel_alignment_service_blog_layout_option = get_theme_mod('wheel_alignment_service_blog_layout_option_setting', 'Left');

    // Initialize custom CSS variable
    $wheel_alignment_service_custom_css = '';

    // Generate custom CSS based on the layout option
    if ($wheel_alignment_service_blog_layout_option === 'Default') {
        $wheel_alignment_service_custom_css .= '.mag-post-detail { text-align: center; }';
    } elseif ($wheel_alignment_service_blog_layout_option === 'Left') {
        $wheel_alignment_service_custom_css .= '.mag-post-detail { text-align: left; }';
    } elseif ($wheel_alignment_service_blog_layout_option === 'Right') {
        $wheel_alignment_service_custom_css .= '.mag-post-detail { text-align: right; }';
    }

    // Output the combined CSS
    ?>
    <style type="text/css">
        <?php echo wp_kses($wheel_alignment_service_custom_css, array( 'style' => array(), 'text-align' => array() )); ?>
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_blog_layout_customizer_css');

function wheel_alignment_service_sidebar_width_customizer_css() {
    $wheel_alignment_service_sidebar_width = get_theme_mod('wheel_alignment_service_sidebar_width', '30');
    ?>
    <style type="text/css">
        .right-sidebar .asterthemes-wrapper .asterthemes-page {
            grid-template-columns: auto <?php echo esc_attr($wheel_alignment_service_sidebar_width); ?>%;
        }
        .left-sidebar .asterthemes-wrapper .asterthemes-page {
            grid-template-columns: <?php echo esc_attr($wheel_alignment_service_sidebar_width); ?>% auto;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_sidebar_width_customizer_css');

if ( ! function_exists( 'wheel_alignment_service_get_page_title' ) ) {
    function wheel_alignment_service_get_page_title() {
        $wheel_alignment_service_title = '';

        if (is_404()) {
            $wheel_alignment_service_title = esc_html__('Page Not Found', 'wheel-alignment-service');
        } elseif (is_search()) {
            $wheel_alignment_service_title = esc_html__('Search Results for: ', 'wheel-alignment-service') . esc_html(get_search_query());
        } elseif (is_home() && !is_front_page()) {
            $wheel_alignment_service_title = esc_html__('Blogs', 'wheel-alignment-service');
        } elseif (function_exists('is_shop') && is_shop()) {
            $wheel_alignment_service_title = esc_html__('Shop', 'wheel-alignment-service');
        } elseif (is_page()) {
            $wheel_alignment_service_title = get_the_title();
        } elseif (is_single()) {
            $wheel_alignment_service_title = get_the_title();
        } elseif (is_archive()) {
            $wheel_alignment_service_title = get_the_archive_title();
        } else {
            $wheel_alignment_service_title = get_the_archive_title();
        }

        return apply_filters('wheel_alignment_service_page_title', $wheel_alignment_service_title);
    }
}

if ( ! function_exists( 'wheel_alignment_service_has_page_header' ) ) {
    function wheel_alignment_service_has_page_header() {
        // Default to true (display header)
        $wheel_alignment_service_return = true;

        // Custom conditions for disabling the header
        if ('hide-all-devices' === get_theme_mod('wheel_alignment_service_page_header_visibility', 'all-devices')) {
            $wheel_alignment_service_return = false;
        }

        // Apply filters and return
        return apply_filters('wheel_alignment_service_display_page_header', $wheel_alignment_service_return);
    }
}

if ( ! function_exists( 'wheel_alignment_service_page_header_style' ) ) {
    function wheel_alignment_service_page_header_style() {
        $wheel_alignment_service_style = get_theme_mod('wheel_alignment_service_page_header_style', 'default');
        return apply_filters('wheel_alignment_service_page_header_style', $wheel_alignment_service_style);
    }
}

function wheel_alignment_service_page_title_customizer_css() {
    $wheel_alignment_service_layout_option = get_theme_mod('wheel_alignment_service_page_header_layout', 'left');
    ?>
    <style type="text/css">
        .asterthemes-wrapper.page-header-inner {
            <?php if ($wheel_alignment_service_layout_option === 'flex') : ?>
                display: flex;
                justify-content: space-between;
                align-items: center;
            <?php else : ?>
                text-align: <?php echo esc_attr($wheel_alignment_service_layout_option); ?>;
            <?php endif; ?>
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_page_title_customizer_css');

function wheel_alignment_service_pagetitle_height_css() {
    $wheel_alignment_service_height = get_theme_mod('wheel_alignment_service_pagetitle_height', 50);
    ?>
    <style type="text/css">
        header.page-header {
            padding: <?php echo esc_attr($wheel_alignment_service_height); ?>px 0;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_pagetitle_height_css');

function wheel_alignment_service_site_logo_width() {
    $wheel_alignment_service_site_logo_width = get_theme_mod('wheel_alignment_service_site_logo_width', 200);
    ?>
    <style type="text/css">
        .site-logo img {
            max-width: <?php echo esc_attr($wheel_alignment_service_site_logo_width); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_site_logo_width');

function wheel_alignment_service_menu_font_size_css() {
    $wheel_alignment_service_menu_font_size = get_theme_mod('wheel_alignment_service_menu_font_size', 15);
    ?>
    <style type="text/css">
        .main-navigation a {
            font-size: <?php echo esc_attr($wheel_alignment_service_menu_font_size); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_menu_font_size_css');

//Menu Font Weight
function wheel_alignment_service_menu_font_weight_css() {
	$wheel_alignment_service_font_weight = get_theme_mod( 'wheel_alignment_service_menu_font_weight', 500 );

	$wheel_alignment_service_custom_css = "
		.main-navigation a {
		    font-weight: {$wheel_alignment_service_font_weight};
		}
	";

	wp_add_inline_style( 'wheel-alignment-service-style', $wheel_alignment_service_custom_css );
}
add_action( 'wp_enqueue_scripts', 'wheel_alignment_service_menu_font_weight_css' );

function wheel_alignment_service_sidebar_widget_font_size_css() {
    $wheel_alignment_service_sidebar_widget_font_size = get_theme_mod('wheel_alignment_service_sidebar_widget_font_size', 24);
    ?>
    <style type="text/css">
        h2.wp-block-heading,aside#secondary .widgettitle,aside#secondary .widget-title {
            font-size: <?php echo esc_attr($wheel_alignment_service_sidebar_widget_font_size); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_sidebar_widget_font_size_css');


// Featured Image Dimension
function wheel_alignment_service_custom_featured_image_css() {
    $wheel_alignment_service_dimension = get_theme_mod('wheel_alignment_service_blog_post_featured_image_dimension', 'default');
    $wheel_alignment_service_width = get_theme_mod('wheel_alignment_service_blog_post_featured_image_custom_width', '');
    $wheel_alignment_service_height = get_theme_mod('wheel_alignment_service_blog_post_featured_image_custom_height', '');
    
    if ($wheel_alignment_service_dimension === 'custom' && $wheel_alignment_service_width && $wheel_alignment_service_height) {
        $wheel_alignment_service_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$wheel_alignment_service_width}px !important; height: {$wheel_alignment_service_height}px !important; }";
        wp_add_inline_style('wheel-alignment-service-style', $wheel_alignment_service_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'wheel_alignment_service_custom_featured_image_css');

// Featured Image Border Radius
function wheel_alignment_service_featured_image_border_radius_css() {
    $wheel_alignment_service_featured_image_border_radius = get_theme_mod('wheel_alignment_service_featured_image_border_radius', 10);
    ?>
    <style type="text/css">  
        .mag-post-single img {
            border-radius: <?php echo esc_attr($wheel_alignment_service_featured_image_border_radius); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_featured_image_border_radius_css');

// Woocommerce Related Products Settings
function wheel_alignment_service_related_product_css() {
    $wheel_alignment_service_related_product_show_hide = get_theme_mod('wheel_alignment_service_related_product_show_hide', true);

    if ( $wheel_alignment_service_related_product_show_hide != true) {
        ?>
        <style type="text/css">
            .related.products {
                display: none;
            }
        </style>
        <?php
    }
}
add_action('wp_head', 'wheel_alignment_service_related_product_css');

// Woocommerce Product Sale Position 
function wheel_alignment_service_product_sale_position_customizer_css() {
    $wheel_alignment_service_layout_option = get_theme_mod('wheel_alignment_service_product_sale_position', 'left');
    ?>
    <style type="text/css">
        .woocommerce ul.products li.product .onsale {
            <?php if ($wheel_alignment_service_layout_option === 'left') : ?>
                right: auto;
                left: 0px;
            <?php else : ?>
                left: auto;
                right: 0px;
            <?php endif; ?>
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_product_sale_position_customizer_css'); 

// Retrieve the slider visibility setting
$wheel_alignment_service_slider = get_theme_mod('wheel_alignment_service_enable_banner_section', false);

// Function to output custom CSS directly in the head section
function wheel_alignment_service_custom_css() {
    global $wheel_alignment_service_slider;
    if ($wheel_alignment_service_slider == true) {
        echo '<style type="text/css">
            .home .bottom-header-outer-wrapper {
                padding: 15px 0;
                position: absolute;
                z-index: 99;
                width: 100%;
            }
        </style>';
    }
}

// Hook the function into the wp_head action
add_action('wp_head', 'wheel_alignment_service_custom_css');

//Footer Social Icon Alignment
function wheel_alignment_service_footer_icons_alignment_css() {
    $wheel_alignment_service_footer_social_align = get_theme_mod( 'wheel_alignment_service_footer_social_align', 'center' );   
    ?>
    <style type="text/css">
        .socialicons {
            text-align: <?php echo esc_attr( $wheel_alignment_service_footer_social_align ); ?> 
        }

        /* Mobile Specific */
        @media screen and (max-width: 575px) {
            .socialicons {
                text-align: center;
            }
        }
    </style>
    <?php
}
add_action( 'wp_head', 'wheel_alignment_service_footer_icons_alignment_css' );

//Copyright Alignment
function wheel_alignment_service_footer_copyright_alignment_css() {
    $wheel_alignment_service_footer_bottom_align = get_theme_mod( 'wheel_alignment_service_footer_bottom_align', 'center' );   
    ?>
    <style type="text/css">
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: <?php echo esc_attr( $wheel_alignment_service_footer_bottom_align ); ?> 
        }

        /* Mobile Specific */
        @media screen and (max-width: 575px) {
            .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
                justify-content: center;
                text-align:center;
            }
        }
    </style>
    <?php
}
add_action( 'wp_head', 'wheel_alignment_service_footer_copyright_alignment_css' );

//Copyright Font Size
function wheel_alignment_service_copyright_font_size_css() {
    $wheel_alignment_service_copyright_font_size = get_theme_mod('wheel_alignment_service_copyright_font_size', 16);
    ?>
    <style type="text/css">
        .site-footer-bottom .site-info span {
            font-size: <?php echo esc_attr($wheel_alignment_service_copyright_font_size); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_copyright_font_size_css');

// Preloader Background Color Setting
function wheel_alignment_service_preloader_background_colors_css() {
    $wheel_alignment_service_preloader_background_color_setting = get_theme_mod('wheel_alignment_service_preloader_background_color_setting', '');
        // Only output CSS if a color is set
        if (empty($wheel_alignment_service_preloader_background_color_setting)) {
            return;
        }
    ?>
    <style type="text/css">
        #loader {
            background-color: <?php echo esc_attr($wheel_alignment_service_preloader_background_color_setting); ?>;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_preloader_background_colors_css');

// Preloader Background Image Setting
function wheel_alignment_service_preloader_background_image_css() {
    $wheel_alignment_service_preloader_background_image_setting = get_theme_mod('wheel_alignment_service_preloader_background_image_setting', '');
        // Only output CSS if the background image is set
        if (empty($wheel_alignment_service_preloader_background_image_setting)) {
            return;
        }
    ?>
    <style type="text/css">
        #loader {
            background-image: url('<?php echo esc_url($wheel_alignment_service_preloader_background_image_setting); ?>');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }
    </style>
    <?php
}
add_action('wp_head', 'wheel_alignment_service_preloader_background_image_css');

//Footer Heading Alignment
function wheel_alignment_service_footer_heading_alignment_css() {
    $wheel_alignment_service_footer_header_align = get_theme_mod( 'wheel_alignment_service_footer_header_align', 'left' );   
    ?>
    <style type="text/css">
        .site-footer h4, footer#colophon h2.wp-block-heading, footer#colophon .widgettitle, footer#colophon .widget-title {
            text-align: <?php echo esc_attr( $wheel_alignment_service_footer_header_align ); ?> 
        }
    </style>
    <?php
}
add_action( 'wp_head', 'wheel_alignment_service_footer_heading_alignment_css' );

//First Capital Letter
function wheel_alignment_service_show_first_caps() {
	$wheel_alignment_service_first_caps = get_theme_mod('wheel_alignment_service_show_first_caps', false);
	$wheel_alignment_service_css = '';
	if ( $wheel_alignment_service_first_caps ) {
	$wheel_alignment_service_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
		    font-size: 55px;
			font-weight: 600;
			margin-right: 6px;
			line-height: 1;
			display: inline-block;
			vertical-align: baseline;
		}';
	} else {
		$wheel_alignment_service_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
			display: none;
		}';
	}
	wp_add_inline_style( 'wheel-alignment-service-style', $wheel_alignment_service_css );
}
add_action( 'wp_enqueue_scripts', 'wheel_alignment_service_show_first_caps' );

// Slider Button Color
function wheel_alignment_service_slider_button_color_css() {
    $wheel_alignment_service_btn_color = get_theme_mod('wheel_alignment_service_slider_btn_color');
	$wheel_alignment_service_btn_bg_color = get_theme_mod('wheel_alignment_service_slider_btn_bg_color');

	$wheel_alignment_service_custom_css = '';

	if ( !empty($wheel_alignment_service_btn_color) ) {
		$wheel_alignment_service_custom_css .= "
		#wheel_alignment_service_banner_section .banner-slider-btn a.asterthemes-button{
			color: {$wheel_alignment_service_btn_color};
		}";
	}
    if ( !empty($wheel_alignment_service_btn_bg_color) ) {
		$wheel_alignment_service_custom_css .= "
		#wheel_alignment_service_banner_section .banner-slider-btn a.asterthemes-button{
			background-color: {$wheel_alignment_service_btn_bg_color};
		}";
	}
    wp_add_inline_style('wheel-alignment-service-style', $wheel_alignment_service_custom_css);

}
add_action('wp_enqueue_scripts','wheel_alignment_service_slider_button_color_css');