<?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);

    // ========================================
    // 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);
}
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);
