<?php

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


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


function add_custom_script_in_footer() {
    if ( get_theme_mod( 'dark_fitness_enable_sticky_header', false ) ) {
        ?>
        <script>
            jQuery(document).ready(function($) {
                $(window).on('scroll', function() {
                    var scroll = $(window).scrollTop();
                    if (scroll > 0) {
                        $('.bottom-header-part-wrapper.hello').addClass('is-sticky');
                    } else {
                        $('.bottom-header-part-wrapper.hello').removeClass('is-sticky');
                    }
                });
            });
        </script>
        <?php
    }
}
add_action( 'wp_footer', 'add_custom_script_in_footer' );

function dark_fitness_enqueue_selected_fonts() {
    $dark_fitness_fonts_url = dark_fitness_get_fonts_url();
    if (!empty($dark_fitness_fonts_url)) {
        wp_enqueue_style('dark-fitness-google-fonts', $dark_fitness_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'dark_fitness_enqueue_selected_fonts');

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

function dark_fitness_blog_layout_customizer_css() {
    // Retrieve the blog layout option
    $dark_fitness_blog_layout_option = get_theme_mod('dark_fitness_blog_layout_option_setting', 'Left');

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

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

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

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

if ( ! function_exists( 'dark_fitness_get_page_title' ) ) {
    function dark_fitness_get_page_title() {
        $dark_fitness_title = '';

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

        return apply_filters('dark_fitness_page_title', $dark_fitness_title);
    }
}

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

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

        // Apply filters and return
        return apply_filters('dark_fitness_display_page_header', $dark_fitness_return);
    }
}

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

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

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

function dark_fitness_customize_register_colors( $wp_customize ) {
    $dark_fitness_default_color = (get_stylesheet() === get_template()) ? '#c1e503' : '#fb5b13';

    // Add setting for primary color
    $wp_customize->add_setting(
        'primary_color',
        array(
            'default'           => $dark_fitness_default_color,
            'sanitize_callback' => 'sanitize_hex_color',
        )
    );

    // Add control for primary color
    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize,
            'primary_color',
            array(
                'label'    => __( 'Primary Color', 'dark-fitness' ),
                'section'  => 'colors',
                'priority' => 5,
            )
        )
    );
}
add_action( 'customize_register', 'dark_fitness_customize_register_colors' );


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

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

function dark_fitness_sidebar_widget_font_size_css() {
    $dark_fitness_sidebar_widget_font_size = get_theme_mod('dark_fitness_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($dark_fitness_sidebar_widget_font_size); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'dark_fitness_sidebar_widget_font_size_css');

// Retrieve the slider visibility setting
$dark_fitness_slider = get_theme_mod('dark_fitness_enable_banner_section', false);

// Function to output custom CSS directly in the head section
function dark_fitness_custom_css() {
    global $dark_fitness_slider;
    if ($dark_fitness_slider == false) {
        echo '<style type="text/css">
            .home header.site-header .header-main-wrapper .bottom-header-outer-wrapper .bottom-header-part {
                background: #1E1E1E !important;
                position: relative !important;
            }
        </style>';
    }
}

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

// Featured Image Dimension
function dark_fitness_custom_featured_image_css() {
    $dark_fitness_dimension = get_theme_mod('dark_fitness_blog_post_featured_image_dimension', 'default');
    $dark_fitness_width = get_theme_mod('dark_fitness_blog_post_featured_image_custom_width', '');
    $dark_fitness_height = get_theme_mod('dark_fitness_blog_post_featured_image_custom_height', '');
    
    if ($dark_fitness_dimension === 'custom' && $dark_fitness_width && $dark_fitness_height) {
        $dark_fitness_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$dark_fitness_width}px !important; height: {$dark_fitness_height}px !important; }";
        wp_add_inline_style('dark-fitness-style', $dark_fitness_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'dark_fitness_custom_featured_image_css');

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

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

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

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

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

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

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

//First Capital Letter
function dark_fitness_show_first_caps() {
	$dark_fitness_first_caps = get_theme_mod('dark_fitness_show_first_caps', false);
	$dark_fitness_css = '';
	if ( $dark_fitness_first_caps ) {
	$dark_fitness_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 {
		$dark_fitness_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
			display: none;
		}';
	}
	wp_add_inline_style( 'dark-fitness-style', $dark_fitness_css );
}
add_action( 'wp_enqueue_scripts', 'dark_fitness_show_first_caps' );

// Topbar Padding Dynamic CSS
function dark_fitness_topbar_padding_css(){

    $padding = get_theme_mod('dark_fitness_topbar_padding', 20);

    $custom_css = ".top-header-part {
        padding-top: {$padding}px !important;
        padding-bottom: {$padding}px !important;
    }";

    echo '<style>'.$custom_css.'</style>';
}
add_action('wp_head','dark_fitness_topbar_padding_css');