<?php
/**
 * Theme Customizer
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

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

// Load repeater defaults (needed on frontend and in customizer)
require_once get_template_directory() . '/inc/customizer/defaults.php';

// Load section shortcut functionality (edit icons in Customizer preview)
require_once get_template_directory() . '/inc/customizer/preview-shortcuts.php';

// Load Upgrade to Pro section
require_once get_template_directory() . '/inc/customizer/panels/upgrade-pro.php';

/**
 * Add customizer options
 */
function paws_and_care_customize_register($wp_customize)
{

    // Load Custom Customizer Controls
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-base-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-toggle-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-heading-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-parent-heading-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-color-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-range-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-slider-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-radio-image-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-radio-buttonset-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-sortable-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-category-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-dropdown-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-tinymce-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-typography-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-plugin-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-upgrade-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-icon-picker-control.php';
    require_once get_template_directory() . '/inc/customizer/controls/code/customize-button-control.php';

    // ========================================
    // PANELS
    // ========================================

    // Theme Settings Panel
    $wp_customize->add_panel('paws_and_care_theme_settings', array(
        'title' => __('Theme Settings', 'paws-and-care'),
        'description' => __('Configure general theme settings including layout, topbar, social links, and footer.', 'paws-and-care'),
        'priority' => 25,
    ));

    // Front Page Settings Panel
    $wp_customize->add_panel('paws_and_care_front_page', array(
        'title' => __('Front Page Settings', 'paws-and-care'),
        'description' => __('Customize the front page sections.', 'paws-and-care'),
        'priority' => 50,
    ));

    // Theme Templates Panel
    $wp_customize->add_panel('paws_and_care_templates', array(
        'title' => __('Theme Templates', 'paws-and-care'),
        'description' => __('Customize theme template settings for archive pages, single posts, pages, and more.', 'paws-and-care'),
        'priority' => 55,
    ));

    // ========================================
    // THEME SETTINGS PANEL SECTIONS
    // ========================================

    // Color Settings (priority 5 - first in Theme Settings)
    require_once get_template_directory() . '/inc/customizer/panels/theme-settings/colors.php';
    paws_and_care_customizer_colors($wp_customize);

    // Layout Settings
    require_once get_template_directory() . '/inc/customizer/panels/theme-settings/layout.php';
    paws_and_care_customizer_layout($wp_customize);

    // Header Settings
    require_once get_template_directory() . '/inc/customizer/panels/theme-settings/header.php';
    paws_and_care_customizer_header($wp_customize);

    // Topbar Settings
    require_once get_template_directory() . '/inc/customizer/panels/theme-settings/topbar.php';
    paws_and_care_customizer_topbar($wp_customize);

    // Social Links
    require_once get_template_directory() . '/inc/customizer/panels/theme-settings/social.php';
    paws_and_care_customizer_social($wp_customize);

    // Footer Settings
    require_once get_template_directory() . '/inc/customizer/panels/theme-settings/footer.php';
    paws_and_care_customizer_footer($wp_customize);

    // Page Header / Breadcrumb Settings
    require_once get_template_directory() . '/inc/customizer/panels/theme-settings/page-header.php';
    paws_and_care_customizer_page_header($wp_customize);

    // ========================================
    // FRONT PAGE PANEL SECTIONS
    // ========================================

    // Slider Section
    require_once get_template_directory() . '/inc/customizer/panels/front-page/slider.php';
    paws_and_care_customizer_slider($wp_customize);

    // About Section
    require_once get_template_directory() . '/inc/customizer/panels/front-page/about.php';
    paws_and_care_customizer_about($wp_customize);

    // Services Section
    require_once get_template_directory() . '/inc/customizer/panels/front-page/services.php';
    paws_and_care_customizer_services($wp_customize);

    // Blog Section
    require_once get_template_directory() . '/inc/customizer/panels/front-page/blog.php';
    paws_and_care_customizer_blog($wp_customize);

    // WooCommerce Section
    if (class_exists('WooCommerce')) {
        require_once get_template_directory() . '/inc/customizer/panels/front-page/woocommerce.php';
        paws_and_care_customizer_woo($wp_customize);
    }

    // ========================================
    // THEME TEMPLATES PANEL SECTIONS
    // ========================================

    // Services Page Template
    require_once get_template_directory() . '/inc/customizer/panels/templates/services-page.php';
    paws_and_care_customizer_services_page_template($wp_customize);

    // Contact Page Template
    require_once get_template_directory() . '/inc/customizer/panels/templates/contact-page.php';
    paws_and_care_customizer_contact_page_template($wp_customize);

    // ========================================
    // CORE SITE IDENTITY EXTENSIONS
    // ========================================

    // Show Site Title Icon Toggle
    $wp_customize->add_setting('paws_and_care_show_site_title_icon', array(
        'default' => true,
        'sanitize_callback' => 'paws_and_care_sanitize_checkbox',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Toggle_Control($wp_customize, 'paws_and_care_show_site_title_icon', array(
        'label' => __('Paws Icon in Site Title', 'paws-and-care'),
        'description' => __('Toggle the paw icon next to the site title.', 'paws-and-care'),
        'section' => 'title_tagline',
        'priority' => 9, // After logo (8) and before site title (10)
    )));
}
add_action('customize_register', 'paws_and_care_customize_register');

/**
 * Sanitize checkbox
 */
function paws_and_care_sanitize_checkbox($checked)
{
    return ((isset($checked) && true === $checked) ? true : false);
}

/**
 * Sanitize select
 */
function paws_and_care_sanitize_select($input, $setting)
{
    $choices = $setting->manager->get_control($setting->id)->choices;
    return (array_key_exists($input, $choices) ? $input : $setting->default);
}

/**
 * Sanitize RGBA color
 */
function paws_and_care_sanitize_rgba_color($color)
{
    if (empty($color) || is_array($color)) {
        return '';
    }

    // If hex color
    if (false === strpos($color, 'rgba')) {
        return sanitize_hex_color($color);
    }

    // Sanitize rgba color
    $color = str_replace(' ', '', $color);
    sscanf($color, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha);

    return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')';
}

/**
 * Active callback: Page Header enabled
 */
function paws_and_care_is_page_header_enabled()
{
    return get_theme_mod('paws_and_care_show_page_header', true);
}

/**
 * Active callback: Breadcrumbs enabled
 */
function paws_and_care_is_breadcrumbs_enabled()
{
    return get_theme_mod('paws_and_care_show_page_header', true) && get_theme_mod('paws_and_care_show_breadcrumbs', true);
}

/**
 * Custom Separator Control for Customizer
 */
if (class_exists('WP_Customize_Control')) {
    class Paws_And_Care_Separator_Control extends WP_Customize_Control
    {
        public $type = 'separator';

        public function render_content()
        {
            ?>
            <label>
                <span class="customize-control-title"
                    style="border-top: 1px solid #ddd; padding-top: 15px; margin-top: 10px; font-weight: 600; font-size: 14px;">
                    <?php echo esc_html($this->label); ?>
                </span>
                <?php if (!empty($this->description)): ?>
                    <span class="description customize-control-description"><?php echo esc_html($this->description); ?></span>
                <?php endif; ?>
            </label>
            <?php
        }
    }
}

/**
 * Output customizer CSS for layout settings
 */
function paws_and_care_customizer_css()
{
    $site_layout = get_theme_mod('paws_and_care_site_layout', 'full-width');
    $container_width = get_theme_mod('paws_and_care_container_width', '1320');
    $custom_width = get_theme_mod('paws_and_care_custom_container_width', 1320);

    // Determine container width value
    if ('custom' === $container_width) {
        $width_value = absint($custom_width);
    } else {
        $width_value = absint($container_width);
    }

    $css = '';

    // Container width CSS
    if ($width_value !== 1320) {
        $css .= "
        @media (min-width: 1200px) {
            .container {
                max-width: {$width_value}px;
            }
        }";
    }

    // Boxed layout CSS
    if ('boxed' === $site_layout) {
        $boxed_width = $width_value + 60; // Add some padding
        $css .= "
        body {
            background-color: #e9ecef;
        }
        .site-wrapper {
            max-width: {$boxed_width}px;
            margin: 0 auto;
            background-color: #ffffff;
            box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
        }
        .hero-section,
        .callout-section {
            max-width: 100%;
        }";
    }

    if (!empty($css)) {
        wp_add_inline_style('paws-and-care-style', $css);
    }
}
add_action('wp_enqueue_scripts', 'paws_and_care_customizer_css', 20);

/**
 * Add Documentation external link to Customizer sidebar
 */
function paws_and_care_customizer_docs_link() {
    $docs_url = esc_url('https://wpfrank.com/docs/paws-and-care/');
    ?>
    <style type="text/css">
        #accordion-section-paws-care-docs .accordion-section-title::after {
            display: none !important;
        }
    </style>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            var tmpl = '<li id="accordion-section-paws-care-docs" class="accordion-section control-section control-section-default" style="display: list-item; margin-top: 10px;"><h3 class="accordion-section-title" tabindex="0"><a href="<?php echo $docs_url; ?>" target="_blank" style="text-decoration: none; display: block; color: inherit;">📖 <?php esc_html_e('Theme Documentation', 'paws-and-care'); ?> <span class="dashicons dashicons-external" style="float: right; margin-top: 3px;"></span></a></h3></li>';
            
            // Wait for Customizer to finish rendering panels
            setTimeout(function() {
                if ($('#accordion-section-paws_and_care_upgrade_section').length) {
                    $('#accordion-section-paws_and_care_upgrade_section').after(tmpl);
                } else {
                    $('#customize-theme-controls > ul').append(tmpl);
                }
            }, 1000);
        });
    </script>
    <?php
}
add_action('customize_controls_print_footer_scripts', 'paws_and_care_customizer_docs_link');
