<?php
// Enqueue theme styles and custom inline CSS
function unique_gift_shop_enqueue_styles() {
    wp_enqueue_style('unique-gift-shop-style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'unique_gift_shop_enqueue_styles');

// Custom Header Support
add_theme_support('custom-header', array(
    'width'              => 1920,
    'height'             => 400,
    'flex-height'        => true,
    'flex-width'         => true,
    'header-text'        => true,
    'default-text-color' => '000000',
    'wp-head-callback'   => 'unique_gift_shop_header_style',
));

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

// Style the Header
function unique_gift_shop_header_style() {
    $header_image = get_header_image();
    $header_text_color = get_header_textcolor();
    $default_text_color = get_theme_support('custom-header', 'default-text-color');

    $custom_css = '';

    if ($default_text_color !== $header_text_color || !empty($header_image)) {
        if (!empty($header_image)) {
            $custom_css .= "
                #custom-header {
                    background-image: url(" . esc_url($header_image) . ");
                    background-repeat: no-repeat;
                    background-position: 50% 50%;
                    background-size: cover;
                }
            ";
        }

        if ('blank' === $header_text_color || '' !== $header_text_color) {
            $custom_css .= "
                .site-title a, .site-description {
                    color: #" . esc_attr($header_text_color) . ";
                }
            ";
        }

        wp_add_inline_style('unique-gift-shop-style', $custom_css);
    }
}
add_action('wp_enqueue_scripts', 'unique_gift_shop_header_style');

// Remove Customizer Header Text Checkbox
function unique_gift_shop_remove_header_text_display_checkbox($wp_customize) {
    $wp_customize->remove_control('display_header_text');
}
add_action('customize_register', 'unique_gift_shop_remove_header_text_display_checkbox', 11);

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

// Logo Dynamic CSS
function unique_gift_shop_logo_dynamic_css() {
    $logo_width = get_theme_mod('unique_gift_shop_logo_width', 150);

    $custom_css = "
        .logo .custom-logo {
            max-width: {$logo_width}px;
            height: auto;
        }
    ";

    wp_add_inline_style('unique-gift-shop-style', $custom_css);
}
add_action('wp_enqueue_scripts', 'unique_gift_shop_logo_dynamic_css');

// Button Styling
function unique_gift_shop_custom_button_styles() {
    $radius = get_theme_mod('unique_gift_shop_button_border_radius', '0px');
    $padding = get_theme_mod('unique_gift_shop_button_padding', '10px 35px');

    $custom_css = "
        .btn,
        .button,
        button,
        .wp-block-button__link,
        #blog-section .read-more a,
        .read-more a,
        a.btn-slid.btn {
            border-radius: {$radius};
            padding: {$padding};
        }
    ";

    wp_add_inline_style('unique-gift-shop-style', $custom_css);
}
add_action('wp_enqueue_scripts', 'unique_gift_shop_custom_button_styles');

// Font Customization
function unique_gift_shop_customize_fonts() {
    $body_font = get_theme_mod('unique_gift_shop_body_font_family', 'Poppins, sans-serif');
    $heading_font = get_theme_mod('unique_gift_shop_heading_font_family', 'Poppins, sans-serif');

    $body_font_name = trim(explode(',', $body_font)[0]);
    $heading_font_name = trim(explode(',', $heading_font)[0]);

    $google_fonts_url = 'https://fonts.googleapis.com/css2?family=' . urlencode($body_font_name) . '&family=' . urlencode($heading_font_name) . '&display=swap';

    wp_enqueue_style('unique-gift-shop-fonts', $google_fonts_url, array(), null);

    $custom_css = "
        body, p, span, label, div {
            font-family: {$body_font};
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: {$heading_font};
        }
    ";

    wp_add_inline_style('unique-gift-shop-fonts', $custom_css);
}
add_action('wp_enqueue_scripts', 'unique_gift_shop_customize_fonts');

function unique_gift_shop_custom_woocommerce_cart_icon() {
    
    if ( class_exists( 'WooCommerce' ) && WC()->cart ) {
        
        $unique_gift_shop_cart_count = WC()->cart->get_cart_contents_count();
        $unique_gift_shop_cart_url = wc_get_cart_url();
        ?>
        
        <span class="cart-icon-wrapper">
            <a class="cart-contents" href="<?php echo esc_url($unique_gift_shop_cart_url); ?>">
                <i class="bi bi-bag-fill"></i>
                <?php if ($unique_gift_shop_cart_count > 0) { ?>
                    <span class="cart-count"><?php echo esc_html($unique_gift_shop_cart_count); ?></span>
                <?php } ?>
            </a>
        </span>
        
        <?php
    }
}

add_filter( 'woocommerce_add_to_cart_fragments', 'unique_gift_shop_custom_woocommerce_cart_icon_fragments' );

function unique_gift_shop_custom_woocommerce_cart_icon_fragments( $fragments ) {
    
    if ( class_exists( 'WooCommerce' ) ) {
        ob_start();
        unique_gift_shop_custom_woocommerce_cart_icon();
        $fragments['div.cart-icon-wrapper'] = ob_get_clean();
    }
    return $fragments;
}

// page title box
function unique_gift_shop_page_title_dynamic_styles() {
    $bg_type = get_theme_mod('unique_gift_shop_page_bg_radio', '');
    $bg_color = get_theme_mod('unique_gift_shop_page_bg_color', '');
    $bg_image = get_theme_mod('unique_gift_shop_page_bg_image', '');
    $unique_gift_shop_alpha = get_theme_mod( 'unique_gift_shop_global_color1','var(--alpha-color)' );

    $unique_gift_shop_dynamic_css = '';

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

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

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

function unique_gift_shop_global_color() {

    $unique_gift_shop_css  = '';
    $unique_gift_shop_alpha = get_theme_mod( 'unique_gift_shop_global_color1' );
    $unique_gift_shop_beta  = get_theme_mod( 'unique_gift_shop_global_color2' );
    $unique_gift_shop_gama  = get_theme_mod( 'unique_gift_shop_global_color3' );

    $unique_gift_shop_root_vars = '';
    if ( $unique_gift_shop_alpha ) {
        $unique_gift_shop_root_vars .= '--alpha-color: ' . esc_attr( $unique_gift_shop_alpha ) . ' !important;';
    }
    if ( $unique_gift_shop_beta ) {
        $unique_gift_shop_root_vars .= '--beta-color: ' . esc_attr( $unique_gift_shop_beta ) . ' !important;';
    }
    if ( $unique_gift_shop_gama ) {
        $unique_gift_shop_root_vars .= '--gama-color: ' . esc_attr( $unique_gift_shop_gama ) . ' !important;';
    }

    if ( $unique_gift_shop_root_vars !== '' ) {
        $unique_gift_shop_css .= ':root {' . $unique_gift_shop_root_vars . '}';
    }

    if ( $unique_gift_shop_css !== '' ) {
        // Replace these handles with your theme's main & WooCommerce stylesheet handles.
        wp_add_inline_style( 'unique-gift-shop-style', $unique_gift_shop_css );
        wp_add_inline_style( 'unique-gift-shop-woocommerce-css', $unique_gift_shop_css );
    }
}
add_action( 'wp_enqueue_scripts', 'unique_gift_shop_global_color', 20 );

// global color
function unique_gift_shop_global_color_custom_css() {
    $unique_gift_shop_bg_type       = get_theme_mod('unique_gift_shop_page_bg_radio', 'color');
    

    $unique_gift_shop_theme_css = '';

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

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