<?php
// Enqueue theme styles and custom inline CSS
function business_article_ship_enqueue_styles() {
    wp_enqueue_style('business-article-ship-style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'business_article_ship_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'   => 'business_article_ship_header_style',
));

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

// Style the Header
function business_article_ship_header_style() {
    $business_article_ship_header_image = get_header_image();
    $business_article_ship_header_text_color = get_header_textcolor();
    $business_article_ship_default_text_color = get_theme_support('custom-header', 'default-text-color');

    $custom_css = '';

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

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

        wp_add_inline_style('business-article-ship-style', $custom_css);
    }
}
add_action('wp_enqueue_scripts', 'business_article_ship_header_style');

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

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

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

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

    wp_add_inline_style('business-article-ship-style', $custom_css);
}
add_action('wp_enqueue_scripts', 'business_article_ship_logo_dynamic_css');

// Button Styling
function business_article_ship_custom_button_styles() {
    $business_article_ship_radius = get_theme_mod('business_article_ship_button_border_radius', '0px');
    $business_article_ship_padding = get_theme_mod('business_article_ship_button_padding', '10px 35px');

    $custom_css = "
        .btn,
        .button,
        button,
        input[type='submit'],
        .wp-block-button__link,
        #blog-section .read-more a,
        .read-more a,
        a.btn-slid.btn {
            border-radius: {$business_article_ship_radius};
            padding: {$business_article_ship_padding};
        }
    ";

    wp_add_inline_style('business-article-ship-style', $custom_css);
}
add_action('wp_enqueue_scripts', 'business_article_ship_custom_button_styles');

// Font Customization
function business_article_ship_customize_fonts() {
    $business_article_ship_body_font = get_theme_mod('business_article_ship_body_font_family', 'Poppins, sans-serif');
    $business_article_ship_heading_font = get_theme_mod('business_article_ship_heading_font_family', 'Poppins, sans-serif');

    $business_article_ship_body_font_name = trim(explode(',', $business_article_ship_body_font)[0]);
    $business_article_ship_heading_font_name = trim(explode(',', $business_article_ship_heading_font)[0]);

    $business_article_ship_google_fonts_url = 'https://fonts.googleapis.com/css2?family=' . urlencode($business_article_ship_body_font_name) . '&family=' . urlencode($business_article_ship_heading_font_name) . '&display=swap';

    wp_enqueue_style('business-article-ship-fonts', $business_article_ship_google_fonts_url, array(), null);

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

    wp_add_inline_style('business-article-ship-fonts', $custom_css);
}
add_action('wp_enqueue_scripts', 'business_article_ship_customize_fonts');


// page title box
function business_article_ship_page_title_dynamic_styles() {
    $bg_type = get_theme_mod('business_article_ship_page_bg_radio', '');
    $bg_color = get_theme_mod('business_article_ship_page_bg_color', '');
    $bg_image = get_theme_mod('business_article_ship_page_bg_image', '');
    $business_article_ship_alpha = get_theme_mod('business_article_ship_global_color1', 'var(--alpha-color)');

    $business_article_ship_dynamic_css = '';

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

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

// Register page title action globally
add_action('business_article_ship_get_page_title', 'business_article_ship_show_page_title');

// global color
function business_article_ship_global_color_custom_css() {
    $business_article_ship_bg_type       = get_theme_mod('business_article_ship_page_bg_radio', 'color');

    $business_article_ship_theme_css = '';

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

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

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

function business_article_ship_global_color() {

    $business_article_ship_css  = '';
    $business_article_ship_alpha = get_theme_mod( 'business_article_ship_global_color1' );

    $business_article_ship_root_vars = '';
    if ( $business_article_ship_alpha ) {
        $business_article_ship_root_vars .= '--alpha-color: ' . esc_attr( $business_article_ship_alpha ) . ' !important;';
    }

    if ( $business_article_ship_root_vars !== '' ) {
        $business_article_ship_css .= ':root {' . $business_article_ship_root_vars . '}';
    }

    if ( $business_article_ship_css !== '' ) {
        // Replace these handles with your theme's main & WooCommerce stylesheet handles.
        wp_add_inline_style( 'business-article-ship-style', $business_article_ship_css );
        wp_add_inline_style( 'business-article-ship-woocommerce-css', $business_article_ship_css );
    }
}
add_action( 'wp_enqueue_scripts', 'business_article_ship_global_color', 20 );
