<?php
/**
 * Menu Customizer Settings
 *
 * @package Blog_Over
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Register Menu Customizer Settings
 */
function blog_over_menu_customizer_register($wp_customize)
{

    // =========================================
    // Panel: Theme Options
    // =========================================
    $wp_customize->add_panel('blog_over_theme_options', array(
        'title' => __('Theme Options', 'blog-over'),
        'priority' => 30,
    ));

    // =========================================
    // Section: Menu Layout Settings
    // =========================================
    $wp_customize->add_section('blog_over_menu_layout_section', array(
        'title' => __('Menu Layout', 'blog-over'),
        'panel' => 'blog_over_theme_options',
        'priority' => 10,
    ));

    // Layout Select
    $wp_customize->add_setting('blog_over_menu_layout', array(
        'default' => 'layout-1',
        'sanitize_callback' => 'blog_over_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_over_menu_layout', array(
        'label' => __('Select Menu Layout', 'blog-over'),
        'section' => 'blog_over_menu_layout_section',
        'type' => 'select',
        'choices' => array(
            'layout-1' => __('Layout 1: Classic (Logo Left, Menu Right)', 'blog-over'),
            'layout-2' => __('Layout 2: Centered (Logo Center, Menu Below)', 'blog-over'),
            'layout-3' => __('Layout 3: Split (Menu-Logo-Menu)', 'blog-over'),
            'layout-4' => __('Layout 4: Minimal (Hamburger Always)', 'blog-over'),
        ),
    ));

    // Menu Alignment
    $wp_customize->add_setting('blog_over_menu_alignment', array(
        'default' => 'right',
        'sanitize_callback' => 'blog_over_sanitize_select',
    ));
    $wp_customize->add_control('blog_over_menu_alignment', array(
        'label' => __('Menu Alignment', 'blog-over'),
        'section' => 'blog_over_menu_layout_section',
        'type' => 'select',
        'choices' => array(
            'left' => __('Left', 'blog-over'),
            'center' => __('Center', 'blog-over'),
            'right' => __('Right', 'blog-over'),
        ),
    ));

    // Full Width Toggle
    $wp_customize->add_setting('blog_over_menu_full_width', array(
        'default' => false,
        'sanitize_callback' => 'blog_over_sanitize_checkbox',
    ));
    $wp_customize->add_control('blog_over_menu_full_width', array(
        'label' => __('Enable Full-Width Menu', 'blog-over'),
        'section' => 'blog_over_menu_layout_section',
        'type' => 'checkbox',
    ));

    // =========================================
    // Section: Sticky Menu Settings
    // =========================================
    $wp_customize->add_section('blog_over_sticky_menu_section', array(
        'title' => __('Sticky Header', 'blog-over'),
        'panel' => 'blog_over_theme_options',
        'priority' => 15,
    ));

    // Enable Sticky
    $wp_customize->add_setting('blog_over_sticky_enable', array(
        'default' => false,
        'sanitize_callback' => 'blog_over_sanitize_checkbox',
    ));
    $wp_customize->add_control('blog_over_sticky_enable', array(
        'label' => __('Enable Sticky Header', 'blog-over'),
        'section' => 'blog_over_sticky_menu_section',
        'type' => 'checkbox',
    ));

    // Sticky Offset
    $wp_customize->add_setting('blog_over_sticky_offset', array(
        'default' => 100,
        'sanitize_callback' => 'absint',
    ));
    $wp_customize->add_control('blog_over_sticky_offset', array(
        'label' => __('Sticky Scroll Offset (px)', 'blog-over'),
        'section' => 'blog_over_sticky_menu_section',
        'type' => 'number',
        'input_attrs' => array('min' => 0, 'max' => 500, 'step' => 10),
    ));

    // Sticky Logo
    $wp_customize->add_setting('blog_over_sticky_logo', array(
        'default' => '',
        'sanitize_callback' => 'absint',
    ));
    $wp_customize->add_control(new WP_Customize_Media_Control($wp_customize, 'blog_over_sticky_logo', array(
        'label' => __('Sticky Logo (Optional)', 'blog-over'),
        'section' => 'blog_over_sticky_menu_section',
        'mime_type' => 'image',
    )));

    // =========================================
    // Section: Menu Colors
    // =========================================
    $wp_customize->add_section('blog_over_menu_colors_section', array(
        'title' => __('Menu Colors', 'blog-over'),
        'panel' => 'blog_over_theme_options',
        'priority' => 20,
    ));

    $color_settings = array(
        'blog_over_menu_text_color' => array(__('Menu Text Color', 'blog-over'), '#1e293b'),
        'blog_over_menu_text_hover' => array(__('Menu Text Hover Color', 'blog-over'), '#3b82f6'),
        'blog_over_menu_bg_color' => array(__('Menu Background Color', 'blog-over'), '#ffffff'),
        'blog_over_dropdown_bg_color' => array(__('Dropdown Background', 'blog-over'), '#ffffff'),
        'blog_over_dropdown_text_color' => array(__('Dropdown Text Color', 'blog-over'), '#1e293b'),
        'blog_over_dropdown_hover_color' => array(__('Dropdown Hover Color', 'blog-over'), '#3b82f6'),
        'blog_over_menu_border_color' => array(__('Menu Border Color', 'blog-over'), '#e2e8f0'),
        'blog_over_sticky_bg_color' => array(__('Sticky Background Color', 'blog-over'), '#ffffff'),
        'blog_over_sticky_text_color' => array(__('Sticky Text Color', 'blog-over'), '#1e293b'),
    );

    foreach ($color_settings as $setting_id => $setting_data) {
        $wp_customize->add_setting($setting_id, array(
            'default' => $setting_data[1],
            'sanitize_callback' => 'sanitize_hex_color',
            'transport' => 'postMessage',
        ));
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, $setting_id, array(
            'label' => $setting_data[0],
            'section' => 'blog_over_menu_colors_section',
        )));
    }

    // =========================================
    // Section: Menu Typography
    // =========================================
    $wp_customize->add_section('blog_over_menu_typography_section', array(
        'title' => __('Menu Typography', 'blog-over'),
        'panel' => 'blog_over_theme_options',
        'priority' => 25,
    ));

    // Font Size
    $wp_customize->add_setting('blog_over_menu_font_size', array(
        'default' => 15,
        'sanitize_callback' => 'absint',
    ));
    $wp_customize->add_control('blog_over_menu_font_size', array(
        'label' => __('Font Size (px)', 'blog-over'),
        'section' => 'blog_over_menu_typography_section',
        'type' => 'number',
        'input_attrs' => array('min' => 10, 'max' => 24, 'step' => 1),
    ));

    // Font Weight
    $wp_customize->add_setting('blog_over_menu_font_weight', array(
        'default' => '600',
        'sanitize_callback' => 'blog_over_sanitize_select',
    ));
    $wp_customize->add_control('blog_over_menu_font_weight', array(
        'label' => __('Font Weight', 'blog-over'),
        'section' => 'blog_over_menu_typography_section',
        'type' => 'select',
        'choices' => array(
            '400' => __('Normal', 'blog-over'),
            '500' => __('Medium', 'blog-over'),
            '600' => __('Semi-Bold', 'blog-over'),
            '700' => __('Bold', 'blog-over'),
        ),
    ));

    // Letter Spacing
    $wp_customize->add_setting('blog_over_menu_letter_spacing', array(
        'default' => 0,
        'sanitize_callback' => 'blog_over_sanitize_float',
    ));
    $wp_customize->add_control('blog_over_menu_letter_spacing', array(
        'label' => __('Letter Spacing (px)', 'blog-over'),
        'section' => 'blog_over_menu_typography_section',
        'type' => 'number',
        'input_attrs' => array('min' => -1, 'max' => 5, 'step' => 0.5),
    ));

    // Text Transform
    $wp_customize->add_setting('blog_over_menu_text_transform', array(
        'default' => 'none',
        'sanitize_callback' => 'blog_over_sanitize_select',
    ));
    $wp_customize->add_control('blog_over_menu_text_transform', array(
        'label' => __('Text Transform', 'blog-over'),
        'section' => 'blog_over_menu_typography_section',
        'type' => 'select',
        'choices' => array(
            'none' => __('None', 'blog-over'),
            'uppercase' => __('Uppercase', 'blog-over'),
            'capitalize' => __('Capitalize', 'blog-over'),
            'lowercase' => __('Lowercase', 'blog-over'),
        ),
    ));

    // =========================================
    // Section: Menu Icons
    // =========================================
    $wp_customize->add_section('blog_over_menu_icons_section', array(
        'title' => __('Menu Icons', 'blog-over'),
        'panel' => 'blog_over_theme_options',
        'priority' => 30,
    ));

    // Enable Dropdown Arrow
    $wp_customize->add_setting('blog_over_dropdown_arrow_enable', array(
        'default' => true,
        'sanitize_callback' => 'blog_over_sanitize_checkbox',
    ));
    $wp_customize->add_control('blog_over_dropdown_arrow_enable', array(
        'label' => __('Enable Dropdown Arrow Icon', 'blog-over'),
        'section' => 'blog_over_menu_icons_section',
        'type' => 'checkbox',
    ));

    // Arrow Icon Type
    $wp_customize->add_setting('blog_over_dropdown_arrow_type', array(
        'default' => 'chevron',
        'sanitize_callback' => 'blog_over_sanitize_select',
    ));
    $wp_customize->add_control('blog_over_dropdown_arrow_type', array(
        'label' => __('Dropdown Icon Type', 'blog-over'),
        'section' => 'blog_over_menu_icons_section',
        'type' => 'select',
        'choices' => array(
            'chevron' => __('Chevron', 'blog-over'),
            'plus-minus' => __('Plus/Minus', 'blog-over'),
            'caret' => __('Caret', 'blog-over'),
        ),
    ));

    // =========================================
    // Section: Mobile Menu
    // =========================================
    $wp_customize->add_section('blog_over_mobile_menu_section', array(
        'title' => __('Mobile Menu', 'blog-over'),
        'panel' => 'blog_over_theme_options',
        'priority' => 35,
    ));

    // Mobile Breakpoint
    $wp_customize->add_setting('blog_over_mobile_breakpoint', array(
        'default' => 992,
        'sanitize_callback' => 'absint',
    ));
    $wp_customize->add_control('blog_over_mobile_breakpoint', array(
        'label' => __('Mobile Breakpoint (px)', 'blog-over'),
        'section' => 'blog_over_mobile_menu_section',
        'type' => 'number',
        'input_attrs' => array('min' => 600, 'max' => 1400, 'step' => 10),
    ));

    // Mobile Menu BG Color
    $wp_customize->add_setting('blog_over_mobile_menu_bg', array(
        'default' => '#ffffff',
        'sanitize_callback' => 'sanitize_hex_color',
    ));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'blog_over_mobile_menu_bg', array(
        'label' => __('Mobile Menu Background', 'blog-over'),
        'section' => 'blog_over_mobile_menu_section',
    )));

    // Mobile Menu Text Color
    $wp_customize->add_setting('blog_over_mobile_menu_text', array(
        'default' => '#1e293b',
        'sanitize_callback' => 'sanitize_hex_color',
    ));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'blog_over_mobile_menu_text', array(
        'label' => __('Mobile Menu Text Color', 'blog-over'),
        'section' => 'blog_over_mobile_menu_section',
    )));

}
// DISABLED: Using Kirki-based customizer instead (customizer-kirki.php)
// add_action('customize_register', 'blog_over_menu_customizer_register');

// =========================================
// Sanitization Functions
// =========================================
// Note: blog_over_sanitize_select and blog_over_sanitize_bool are defined in customizer.php

function blog_over_sanitize_checkbox($checked)
{
    // Handle all truthy values: true, 'true', '1', 1
    return (isset($checked) && (true === $checked || 'true' === $checked || '1' === $checked || 1 === $checked)) ? true : false;
}

function blog_over_sanitize_float($input)
{
    return floatval($input);
}

// =========================================
// Output Customizer Styles
// =========================================
function blog_over_menu_customizer_css()
{
    $menu_text = get_theme_mod('blog_over_menu_text_color', '#1e293b');
    $menu_hover = get_theme_mod('blog_over_menu_text_hover', '#3b82f6');
    $menu_bg = get_theme_mod('blog_over_menu_bg_color', '#ffffff');
    $dropdown_bg = get_theme_mod('blog_over_dropdown_bg_color', '#ffffff');
    $dropdown_text = get_theme_mod('blog_over_dropdown_text_color', '#1e293b');
    $dropdown_hover = get_theme_mod('blog_over_dropdown_hover_color', '#3b82f6');
    $border_color = get_theme_mod('blog_over_menu_border_color', '#e2e8f0');
    $sticky_bg = get_theme_mod('blog_over_sticky_bg_color', '#ffffff');
    $sticky_text = get_theme_mod('blog_over_sticky_text_color', '#1e293b');
    $font_size = get_theme_mod('blog_over_menu_font_size', 15);
    $font_weight = get_theme_mod('blog_over_menu_font_weight', '600');
    $letter_spacing = get_theme_mod('blog_over_menu_letter_spacing', 0);
    $text_transform = get_theme_mod('blog_over_menu_text_transform', 'none');
    $mobile_bg = get_theme_mod('blog_over_mobile_menu_bg', '#ffffff');
    $mobile_text = get_theme_mod('blog_over_mobile_menu_text', '#1e293b');
    $breakpoint = get_theme_mod('blog_over_mobile_breakpoint', 992);
    ?>
    <style id="blog-over-menu-customizer-css">
        :root {
            --blog-over-menu-text:
                <?php echo esc_attr($menu_text); ?>
            ;
            --blog-over-menu-hover:
                <?php echo esc_attr($menu_hover); ?>
            ;
            --blog-over-menu-bg:
                <?php echo esc_attr($menu_bg); ?>
            ;
            --blog-over-dropdown-bg:
                <?php echo esc_attr($dropdown_bg); ?>
            ;
            --blog-over-dropdown-text:
                <?php echo esc_attr($dropdown_text); ?>
            ;
            --blog-over-dropdown-hover:
                <?php echo esc_attr($dropdown_hover); ?>
            ;
            --blog-over-menu-border:
                <?php echo esc_attr($border_color); ?>
            ;
            --blog-over-sticky-bg:
                <?php echo esc_attr($sticky_bg); ?>
            ;
            --blog-over-sticky-text:
                <?php echo esc_attr($sticky_text); ?>
            ;
            --blog-over-menu-font-size:
                <?php echo esc_attr($font_size); ?>
                px;
            --blog-over-menu-font-weight:
                <?php echo esc_attr($font_weight); ?>
            ;
            --blog-over-menu-letter-spacing:
                <?php echo esc_attr($letter_spacing); ?>
                px;
            --blog-over-menu-text-transform:
                <?php echo esc_attr($text_transform); ?>
            ;
            --blog-over-mobile-bg:
                <?php echo esc_attr($mobile_bg); ?>
            ;
            --blog-over-mobile-text:
                <?php echo esc_attr($mobile_text); ?>
            ;
            --blog-over-mobile-breakpoint:
                <?php echo esc_attr($breakpoint); ?>
                px;
        }
    </style>
    <?php
}
add_action('wp_head', 'blog_over_menu_customizer_css', 100);

/**
 * Pass settings to JS
 */
function blog_over_menu_js_vars()
{
    $sticky_enable = get_theme_mod('blog_over_sticky_enable', false);
    $sticky_offset = get_theme_mod('blog_over_sticky_offset', 100);
    $breakpoint = get_theme_mod('blog_over_mobile_breakpoint', 992);
    ?>
    <script>
        var novaMenuSettings = {
            stickyEnable: <?php echo wp_json_encode((bool) $sticky_enable); ?>,
            stickyOffset: <?php echo intval($sticky_offset); ?>,
            mobileBreakpoint: <?php echo intval($breakpoint); ?>
        };
    </script>
    <?php
}
add_action('wp_head', 'blog_over_menu_js_vars', 5);
