<?php
// Enqueue theme styles and custom inline CSS
function kindergarten_art_school_enqueue_styles() {
    wp_enqueue_style('kindergarten-art-school-style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'kindergarten_art_school_enqueue_styles');

// custom header
    add_theme_support('custom-header', array(
            'width'                  => 1920, 
            'height'                 => 400,  
            'flex-height'            => true,
            'flex-width'             => true,
            'header-text'            => true, // Enable or disable header text
            'default-text-color'     => '000000', // Default header text color
            'wp-head-callback'       => 'kindergarten_art_school_header_style',
        ) );

// custom-background
    add_theme_support( 'custom-background', array(
          'default-color' => 'ffffff',
        ));

// Style the header
function kindergarten_art_school_header_style() {
    $kindergarten_art_school_header_image = get_header_image();    
    $kindergarten_art_school_header_text_color = get_header_textcolor();
   
     if (get_theme_support('custom-header', 'default-text-color') !== $kindergarten_art_school_header_text_color || !empty($kindergarten_art_school_header_image)) {
            ?>
        <style type="text/css" id="entr-header-css">
            <?php
            // Has a Custom Header been added?
            if (!empty($kindergarten_art_school_header_image)) :
                ?>
                 #custom-header {
                    background-image: url(<?php header_image(); ?>);
                    background-repeat: no-repeat;
                    background-position: 50% 50%;
                    -webkit-background-size: cover;
                    -moz-background-size:    cover;
                    -o-background-size:      cover;
                    background-size:         cover;
                }
            <?php endif; ?> 
            <?php
                if ('blank' === $kindergarten_art_school_header_text_color) :
                ?>
                    .site-title a,.site-description {
                        color: #<?php echo esc_attr( $kindergarten_art_school_header_text_color ); ?>;
                    }
                <?php elseif ('' !== $kindergarten_art_school_header_text_color) : ?>
                    .site-title a,.site-description {
                        color: #<?php echo esc_attr($kindergarten_art_school_header_text_color); ?>;
                    }            
                <?php endif; ?>
        </style>
    <?php
        }
    }
// site-title-checkbox
// Remove "Display Site Title and Tagline" checkbox from Customizer
function kindergarten_art_school_remove_header_text_display_checkbox( $wp_customize ) {
    $wp_customize->remove_control( 'display_header_text' ); // Removes the checkbox
}
add_action( 'customize_register', 'kindergarten_art_school_remove_header_text_display_checkbox', 11 );

/**
* Custom logo
*/
function kindergarten_art_school_logo_setup(){
    add_theme_support('custom-logo', array(
        'height' => 65,
        'width' => 350,
        'flex-height' => true,
        'flex-width' => true,
    ));
}
add_action('after_setup_theme', 'kindergarten_art_school_logo_setup');

function kindergarten_art_school_logo_dynamic_css() {
    $kindergarten_art_school_logo_width = get_theme_mod( 'kindergarten_art_school_logo_width', 150 );
    ?>
    <style type="text/css">
        .logo .custom-logo {
            max-width: <?php echo esc_attr( $kindergarten_art_school_logo_width ); ?>px;
            height: auto;
        }
    </style>
    <?php
}
add_action( 'wp_head', 'kindergarten_art_school_logo_dynamic_css' );

// buttons
function kindergarten_art_school_custom_button_styles() {
    $kindergarten_art_school_radius = get_theme_mod( 'kindergarten_art_school_button_border_radius', '0px' );
    $kindergarten_art_school_padding = get_theme_mod( 'kindergarten_art_school_button_padding', '10px 10px' );
    ?>
    <style type="text/css">
        .btn,
        .button,
        button,
        input[type="submit"],
        .wp-block-button__link,#blog-section .read-more a,.read-more a {
            border-radius: <?php echo esc_attr($kindergarten_art_school_radius); ?>;
            padding: <?php echo esc_attr($kindergarten_art_school_padding); ?>;
        }
    </style>
    <?php
}
add_action( 'wp_head', 'kindergarten_art_school_custom_button_styles' );

function kindergarten_art_school_customize_fonts() {
    $kindergarten_art_school_body_font = get_theme_mod('kindergarten_art_school_body_font_family', 'Ubuntu, sans-serif');
    $kindergarten_art_school_heading_font = get_theme_mod('kindergarten_art_school_heading_font_family', 'Ubuntu, sans-serif');

    // Extract Google font names (e.g., Roboto from "Roboto, sans-serif")
    $kindergarten_art_school_body_font_name = trim(explode(',', $kindergarten_art_school_body_font)[0]);
    $kindergarten_art_school_heading_font_name = trim(explode(',', $kindergarten_art_school_heading_font)[0]);

    // Generate Google Fonts URL
    $kindergarten_art_school_google_font_url = 'https://fonts.googleapis.com/css2?family=' . urlencode($kindergarten_art_school_body_font_name) . '&family=' . urlencode($kindergarten_art_school_heading_font_name) . '&display=swap';

    // Enqueue fonts
    wp_enqueue_style('kindergarten-art-school-fonts', $kindergarten_art_school_google_font_url, array(), null);

    // Custom inline style for font application
    $custom_css = "
        body, p, span, label, div {
            font-family: {$kindergarten_art_school_body_font};
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: {$kindergarten_art_school_heading_font};
        }
    ";
    wp_add_inline_style('kindergarten-art-school-fonts', $custom_css);
}
add_action('wp_enqueue_scripts', 'kindergarten_art_school_customize_fonts');

// page title box
function kindergarten_art_school_page_title_dynamic_styles() {
    $bg_type = get_theme_mod('kindergarten_art_school_page_bg_radio', '');
    $bg_color = get_theme_mod('kindergarten_art_school_page_bg_color', '');
    $bg_image = get_theme_mod('kindergarten_art_school_page_bg_image', '');
    $kindergarten_art_school_alpha = get_theme_mod('kindergarten_art_school_global_color1', 'var(--alpha-color)');

    $kindergarten_art_school_dynamic_css = '';

    if ($bg_type === 'image' && !empty($bg_image)) {
        $kindergarten_art_school_dynamic_css .= '.page-title {';
        $kindergarten_art_school_dynamic_css .= 'background-image: url("' . esc_url($bg_image) . '");';
        $kindergarten_art_school_dynamic_css .= 'background-size: cover;';
        $kindergarten_art_school_dynamic_css .= 'background-position: center;';
        $kindergarten_art_school_dynamic_css .= '}';
    } elseif ($bg_type === 'color' && !empty($bg_color)) {
        $kindergarten_art_school_dynamic_css .= '.page-title {';
        $kindergarten_art_school_dynamic_css .= 'background-color: ' . esc_attr($bg_color) . ';';
        $kindergarten_art_school_dynamic_css .= '}';
    } else {
        // Fallback to global theme color
        $kindergarten_art_school_dynamic_css .= '.page-title {';
        $kindergarten_art_school_dynamic_css .= 'background-color: ' . esc_attr($kindergarten_art_school_alpha) . ';';
        $kindergarten_art_school_dynamic_css .= '}';
    }

    if (!empty($kindergarten_art_school_dynamic_css)) {
        echo '<style type="text/css">' . $kindergarten_art_school_dynamic_css . '</style>';
    }
}
add_action('wp_head', 'kindergarten_art_school_page_title_dynamic_styles', 20);

// Register page title action globally
add_action('kindergarten_art_school_get_page_title', 'kindergarten_art_school_show_page_title');

// global color
function kindergarten_art_school_global_color_custom_css() {
    $kindergarten_art_school_bg_type       = get_theme_mod('kindergarten_art_school_page_bg_radio', 'color');

    $kindergarten_art_school_theme_css = '';

    if (!empty($kindergarten_art_school_alpha)) {
        // Apply global background only if no custom bg for page-title
        if ($kindergarten_art_school_bg_type !== 'color' && $kindergarten_art_school_bg_type !== 'image') {
            $kindergarten_art_school_theme_css .= '.page-title .content-section{ background: ' . esc_attr($kindergarten_art_school_alpha) . '; }';
        }
    }

    if (!empty($kindergarten_art_school_theme_css)) {
        echo '<style type="text/css">' . $kindergarten_art_school_theme_css . '</style>';
    }
}
add_action('wp_head', 'kindergarten_art_school_global_color_custom_css', 10);

/*---------------------------Global Color-------------------*/

function kindergarten_art_school_global_color() {

    $kindergarten_art_school_css  = '';
    $kindergarten_art_school_alpha = get_theme_mod( 'kindergarten_art_school_global_color1' );
    $kindergarten_art_school_beta  = get_theme_mod( 'kindergarten_art_school_global_color2' );

    $kindergarten_art_school_root_vars = '';
    if ( $kindergarten_art_school_alpha ) {
        $kindergarten_art_school_root_vars .= '--alpha-color: ' . esc_attr( $kindergarten_art_school_alpha ) . ' !important;';
    }
    if ( $kindergarten_art_school_beta ) {
        $kindergarten_art_school_root_vars .= '--beta-color: ' . esc_attr( $kindergarten_art_school_beta ) . ' !important;';
    }

    if ( $kindergarten_art_school_root_vars !== '' ) {
        $kindergarten_art_school_css .= ':root {' . $kindergarten_art_school_root_vars . '}';
    }

    if ( $kindergarten_art_school_css !== '' ) {
        // Replace these handles with your theme's main & WooCommerce stylesheet handles.
        wp_add_inline_style( 'kindergarten-art-school-style', $kindergarten_art_school_css );
        wp_add_inline_style( 'kindergarten-art-school-woocommerce-css', $kindergarten_art_school_css );
    }
}
add_action( 'wp_enqueue_scripts', 'kindergarten_art_school_global_color', 20 );