<?php

/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package cat_groomer
 */

 function cat_groomer_customize_css() {
    ?>
    <style type="text/css">
        :root {
            --primary-color: <?php echo esc_html( get_theme_mod( 'cat_groomer_primary_color', '#00C9D5' ) ); ?>;
        }
    </style>
    <?php
}
add_action( 'wp_head', 'cat_groomer_customize_css' );

function cat_groomer_enqueue_selected_fonts() {
    $cat_groomer_fonts_url = cat_groomer_get_fonts_url();
    if (!empty($cat_groomer_fonts_url)) {
        wp_enqueue_style('cat-groomer-google-fonts', $cat_groomer_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'cat_groomer_enqueue_selected_fonts');

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

function cat_groomer_blog_layout_customizer_css() {
    // Retrieve the blog layout option
    $cat_groomer_blog_layout_option = get_theme_mod('cat_groomer_blog_layout_option_setting', 'Left');

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

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

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

// Featured Image Dimension
function cat_groomer_custom_featured_image_css() {
    $cat_groomer_dimension = get_theme_mod('cat_groomer_blog_post_featured_image_dimension', 'default');
    $cat_groomer_width = get_theme_mod('cat_groomer_blog_post_featured_image_custom_width', '');
    $cat_groomer_height = get_theme_mod('cat_groomer_blog_post_featured_image_custom_height', '');
    
    if ($cat_groomer_dimension === 'custom' && $cat_groomer_width && $cat_groomer_height) {
        $cat_groomer_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$cat_groomer_width}px !important; height: {$cat_groomer_height}px !important; }";
        wp_add_inline_style('cat-groomer-style', $cat_groomer_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'cat_groomer_custom_featured_image_css');

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

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

if ( ! function_exists( 'cat_groomer_get_page_title' ) ) {
    function cat_groomer_get_page_title() {
        $cat_groomer_title = '';

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

        return apply_filters('cat_groomer_page_title', $cat_groomer_title);
    }
}

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

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

        // Apply filters and return
        return apply_filters('cat_groomer_display_page_header', $cat_groomer_return);
    }
}

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

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

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

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

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

//Menu Font Weight
function cat_groomer_menu_font_weight_css() {
	$cat_groomer_font_weight = get_theme_mod( 'cat_groomer_menu_font_weight', 400 );

	$cat_groomer_custom_css = "
		.main-navigation a {
		    font-weight: {$cat_groomer_font_weight};
		}
	";

	wp_add_inline_style( 'cat-groomer-style', $cat_groomer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'cat_groomer_menu_font_weight_css' );

function cat_groomer_menu_text_transform_css() {
    $menu_text_transform = get_theme_mod('cat_groomer_menu_text_transform', 'uppercase');
    ?>
    <style type="text/css">
        .main-navigation a {
            text-transform: <?php echo esc_attr($menu_text_transform); ?>;
        }
    </style>
    <?php
}
add_action('wp_head', 'cat_groomer_menu_text_transform_css');

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

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

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

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

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

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

//Copyright Alignment
function cat_groomer_footer_copyright_alignment_css() {
    $cat_groomer_footer_bottom_align = get_theme_mod( 'cat_groomer_footer_bottom_align', 'center' );   
    ?>
    <style type="text/css">
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: <?php echo esc_attr( $cat_groomer_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', 'cat_groomer_footer_copyright_alignment_css' );

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

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

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

//Footer Heading Alignment
function cat_groomer_footer_heading_alignment_css() {
    $cat_groomer_footer_header_align = get_theme_mod( 'cat_groomer_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( $cat_groomer_footer_header_align ); ?> 
        }
    </style>
    <?php
}
add_action( 'wp_head', 'cat_groomer_footer_heading_alignment_css' );

//First Capital Letter
function cat_groomer_show_first_caps() {
	$cat_groomer_first_caps = get_theme_mod('cat_groomer_show_first_caps', false);
	$cat_groomer_css = '';
	if ( $cat_groomer_first_caps ) {
	$cat_groomer_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 {
		$cat_groomer_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
			display: none;
		}';
	}
	wp_add_inline_style( 'cat-groomer-style', $cat_groomer_css );
}
add_action( 'wp_enqueue_scripts', 'cat_groomer_show_first_caps' );

// Banner Button Color
function cat_groomer_banner_button_color_css() {
    $btn_color = get_theme_mod('cat_groomer_banner_btn_color');
	$btn_bg_color = get_theme_mod('cat_groomer_banner_btn_bg_color');

	$custom_css = '';
    if ( !empty($btn_color) ) {
		$custom_css .= "
		.banner-slider-btn a.asterthemes-button{
			color: {$btn_color};
		}";
	}

	if ( !empty($btn_bg_color) ) {
		$custom_css .= "
		.banner-slider-btn a.asterthemes-button{
			background-color: {$btn_bg_color};
		}";
	}

	wp_add_inline_style('cat-groomer-style', $custom_css);
}
add_action('wp_enqueue_scripts','cat_groomer_banner_button_color_css');
