<?php
/**
 * Customizer Integration - Live preview with Seazonify controller
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class Seazonify_Customizer {
    
    public function __construct() {
        add_action('customize_register', array($this, 'register_customizer'));
        add_action('customize_preview_init', array($this, 'enqueue_preview_scripts'));
        add_action('customize_controls_enqueue_scripts', array($this, 'enqueue_control_scripts'));
    }
    
    /**
     * Register customizer settings and controls
     */
    public function register_customizer($wp_customize) {
        // Add Seazonify Section
        $wp_customize->add_section('seazonify_section', array(
            'title' => __('Seazonify Effects', 'seazonify'),
            'priority' => 30,
        ));
        
        // Enable Setting
        $wp_customize->add_setting('seazonify_settings[enabled]', array(
            'default' => false,
            'type' => 'option',
            'capability' => 'edit_theme_options',
            'transport' => 'postMessage',
        ));
        
        $wp_customize->add_control('seazonify_enabled', array(
            'label' => __('Enable Seazonify Effects', 'seazonify'),
            'section' => 'seazonify_section',
            'settings' => 'seazonify_settings[enabled]',
            'type' => 'checkbox',
        ));
        
        // Visual Effect (single ID string)
        $wp_customize->add_setting('seazonify_settings[visual_effect]', array(
            'default' => '',
            'type' => 'option',
            'capability' => 'edit_theme_options',
            'transport' => 'refresh',
        ));
        
        $wp_customize->add_control('seazonify_visual_effect', array(
            'label' => __('Selected Visual Effect', 'seazonify'),
            'section' => 'seazonify_section',
            'settings' => 'seazonify_settings[visual_effect]',
            'type' => 'hidden',
        ));
        
        // Audio Effect (single ID string)
        $wp_customize->add_setting('seazonify_settings[audio_effect]', array(
            'default' => '',
            'type' => 'option',
            'capability' => 'edit_theme_options',
            'transport' => 'refresh',
        ));
        
        $wp_customize->add_control('seazonify_audio_effect', array(
            'label' => __('Selected Audio Effect', 'seazonify'),
            'section' => 'seazonify_section',
            'settings' => 'seazonify_settings[audio_effect]',
            'type' => 'hidden',
        ));
        
        // Combo Effect (single ID string)
        $wp_customize->add_setting('seazonify_settings[combo_effect]', array(
            'default' => '',
            'type' => 'option',
            'capability' => 'edit_theme_options',
            'transport' => 'refresh',
        ));
        
        $wp_customize->add_control('seazonify_combo_effect', array(
            'label' => __('Selected Combo Effect', 'seazonify'),
            'section' => 'seazonify_section',
            'settings' => 'seazonify_settings[combo_effect]',
            'type' => 'hidden',
        ));
        
        // Show Controller
        $wp_customize->add_setting('seazonify_settings[show_controller]', array(
            'default' => true,
            'type' => 'option',
            'capability' => 'edit_theme_options',
            'transport' => 'refresh',
        ));
        
        $wp_customize->add_control('seazonify_show_controller', array(
            'label' => __('Show Controller Button', 'seazonify'),
            'section' => 'seazonify_section',
            'settings' => 'seazonify_settings[show_controller]',
            'type' => 'checkbox',
        ));
    }
    
    /**
     * Enqueue preview scripts
     */
    public function enqueue_preview_scripts() {
        wp_enqueue_script(
            'seazonify-customizer-preview',
            SEAZONIFY_PLUGIN_URL . 'assets/js/customizer-preview.js',
            array('jquery', 'customize-preview'),
            SEAZONIFY_VERSION,
            true
        );
        
        wp_localize_script('seazonify-customizer-preview', 'seazonify_preview_data', array(
            'controller_url' => Seazonify_Effects::get_controller_url(),
            'effects_url'    => SEAZONIFY_EFFECTS_URL,
            'cdn_base'       => Seazonify_Effects::get_cdn_base_url(),
            'ajax_url'       => admin_url('admin-ajax.php'),
        ));
    }
    
    /**
     * Enqueue control scripts
     */
    public function enqueue_control_scripts() {
        wp_enqueue_style(
            'seazonify-customizer-controls',
            SEAZONIFY_PLUGIN_URL . 'assets/css/customizer-controls.css',
            array(),
            SEAZONIFY_VERSION
        );
        
        wp_enqueue_script(
            'seazonify-customizer-controls',
            SEAZONIFY_PLUGIN_URL . 'assets/js/customizer-controls.js',
            array('jquery', 'customize-controls'),
            time(),
            true
        );
        
        // We now load all effects via AJAX to ensure consistency and prevent heavy localization
        $effects = array('visual' => array(), 'audio' => array(), 'combos' => array());
        
        $visual_index = Seazonify_Effects::get_type_index('visual');
        $audio_index = Seazonify_Effects::get_type_index('audio');
        $combo_index = Seazonify_Effects::get_type_index('combo');

        $pagination = array(
            'visual' => (is_wp_error($visual_index) || empty($visual_index['files'])) ? 1 : count($visual_index['files']),
            'audio'  => (is_wp_error($audio_index) || empty($audio_index['files'])) ? 1 : count($audio_index['files']),
            'combos' => (is_wp_error($combo_index) || empty($combo_index['files'])) ? 1 : count($combo_index['files']),
        );
        
        wp_localize_script('seazonify-customizer-controls', 'seazonifyEffects', $effects);
        wp_localize_script('seazonify-customizer-controls', 'seazonifyPagination', $pagination);
        wp_localize_script('seazonify-customizer-controls', 'seazonifyAjax', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('seazonify_admin'),
            'logo_url' => SEAZONIFY_LOGO_URL,
        ));
    }
}

