<?php
/** 
 * Base customizer options class.
 *
 * @package pawfect
 */

defined('ABSPATH') || exit;

/**
 * Class pawfect_Customize_Base_Option
 */
class pawfect_Customize_Base_Option
{

	/**
	 * Array to create customizer options.
	 */
	protected $elements = array();

	/**
	 * Active callback array provided in $elements array.
	 */
	protected $active_callback_old = array();

	/**
	 * Record the count of array provided for active callback.
	 */
	protected $ac_arr_count = 0;

	/**
	 * Setting's default values.
	 */
	protected $ac_default = array();

	/**
	 * Constructor.
	 */
	public function __construct()
	{
		// Remove automatic hook registration - let main customizer class handle this
		// add_action('customize_register', array($this, 'pawfect_customizer_options'));
		$this->elements = $this->elements();
	}

	/**
	 * Get menu options for dropdown.
	 */
	protected function get_menu_options()
	{
		$all_menus = get_terms(array(
			'taxonomy' => 'nav_menu',
			'hide_empty' => true,
		));
		
		$menu_options = array('none' => esc_html__('None', 'pawfect'));
		foreach ($all_menus as $menu_item) {
			$menu_options[$menu_item->slug] = esc_html($menu_item->name);
		}
		return $menu_options;
	}

	/**
	 * Register customizer options.
	 */
	public function pawfect_customizer_options($wp_customize)
	{
		foreach ($this->elements as $el_key => $el_data):

			// Check if this is a heading control (visual separator only)
			$is_heading_control = isset($el_data['control']['type']) && $el_data['control']['type'] === 'pawfect-heading';

			// Skip if upgrade control and Pro is active
			if (isset($el_data['control']['type']) && $el_data['control']['type'] === 'pawfect-upgrade' && apply_filters('pawfect_is_pro_active', false)) {
				continue;
			}

			// Skip if no control is defined, OR if it's not a heading control and no setting is defined
			if (!isset($el_data['control']) || (!$is_heading_control && !isset($el_data['setting']))) {
				continue;
			}

			// Add Setting (skip for heading controls, required for all others)
			if (!$is_heading_control) {
				$setting_args = $el_data['setting'];
				$wp_customize->add_setting($el_key, array(
					'default' => isset($setting_args['default']) ? $setting_args['default'] : '',
					'type' => isset($setting_args['type']) ? $setting_args['type'] : 'theme_mod',
					'capability' => 'edit_theme_options',
					'sanitize_callback' => isset($setting_args['sanitize_callback']) ? $setting_args['sanitize_callback'] : '',
					'transport' => isset($setting_args['transport']) ? $setting_args['transport'] : 'refresh',
				));
			}

			// Add Control
			$control_args = $el_data['control'];
			$control_type = $control_args['type'];
			$is_custom_control = isset($control_args['is_default_type']) && $control_args['is_default_type'];

			// Only set setting for non-heading controls
			if (!$is_heading_control) {
				$control_args['setting'] = $el_key;
			}

			// Handle active callback
			if (isset($control_args['active_callback']) && is_array($control_args['active_callback'])) {
				$this->active_callback_old[] = $control_args['active_callback'];
				
				if (isset($this->active_callback_old[$this->ac_arr_count][0]) && is_array($this->active_callback_old[$this->ac_arr_count][0])) {
					$cb_setting_id = $this->active_callback_old[$this->ac_arr_count][1];
					$setting = $wp_customize->get_setting($cb_setting_id);
					$this->ac_default[] = $setting ? $setting->default : '';
					$control_args['active_callback'] = array($this, 'evaluate');
					$this->ac_arr_count++;
				} else {
					unset($control_args['active_callback']);
				}
			}

			// Add control based on type
			if ($control_type === 'repeater' && class_exists('pawfect_Repeater')) {
				$wp_customize->add_control(new pawfect_Repeater($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'simple-radio-image' && class_exists('pawfect_Simple_Radio_Image_Control')) {
				$wp_customize->add_control(new pawfect_Simple_Radio_Image_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'radio_image' && class_exists('pawfect_Simple_Radio_Image_Control')) {
				$control_args['type'] = 'simple-radio-image';
				$wp_customize->add_control(new pawfect_Simple_Radio_Image_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'sortable' && class_exists('pawfect_Customize_Sortable_Control')) {
				$wp_customize->add_control(new pawfect_Customize_Sortable_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-blog-sortable') {
				if (class_exists('pawfect_Customize_Blog_Sortable_Control')) {
					$wp_customize->add_control(new pawfect_Customize_Blog_Sortable_Control($wp_customize, $el_key, $control_args));
				}
			} elseif ($control_type === 'pawfect-frontpage-sortable' && class_exists('pawfect_Customize_Frontpage_Sortable_Control')) {
				$wp_customize->add_control(new pawfect_Customize_Frontpage_Sortable_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-range-slider' && class_exists('pawfect_Customize_Range_Control')) {
				$wp_customize->add_control(new pawfect_Customize_Range_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-slider' && class_exists('pawfect_Customize_Slider_Control')) {
				unset($control_args['type']);
				$wp_customize->add_control(new pawfect_Customize_Slider_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-toggle' && class_exists('pawfect_Customize_Toggle_Control')) {
				unset($control_args['type']);
				$wp_customize->add_control(new pawfect_Customize_Toggle_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-responsive-range-slider' && class_exists('pawfect_Customize_Responsive_Range_Control')) {
				unset($control_args['type']);
				$wp_customize->add_control(new pawfect_Customize_Responsive_Range_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-palette' && class_exists('Pawfect_Customize_Palette_Control')) {
				unset($control_args['type']);
				$wp_customize->add_control(new Pawfect_Customize_Palette_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect_layout_manager' && class_exists('Pawfect_Customize_Layout_Control')) {
				unset($control_args['type']);
				$wp_customize->add_control(new Pawfect_Customize_Layout_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'radio-switch' && class_exists('pawfect_Radio_Switch_Control')) {
				$wp_customize->add_control(new pawfect_Radio_Switch_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-responsive-range') {
				// Force creation of responsive range control
				$wp_customize->add_control(new Pawfect_Responsive_Range_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-category' && class_exists('pawfect_Customize_Category_Control')) {
				// Handle category dropdown control
				$wp_customize->add_control(new pawfect_Customize_Category_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-heading' && class_exists('pawfect_Customize_Heading_Control')) {
				// Handle heading controls (visual separators) - no setting needed
				unset($control_args['type']);
				$wp_customize->add_control(new pawfect_Customize_Heading_Control($wp_customize, $el_key, $control_args));
			} elseif ($control_type === 'pawfect-upgrade' && class_exists('pawfect_Customize_Upgrade_Control')) {
				// Handle upgrade controls for pro features
				unset($control_args['type']);
				$wp_customize->add_control(new pawfect_Customize_Upgrade_Control($wp_customize, $el_key, $control_args));
			} elseif (!$is_custom_control) {
				unset($control_args['type']);
				$control_type_uc = implode('_', array_map('ucfirst', explode('_', $control_type)));
				$custom_class = 'pawfect_Customize_' . $control_type_uc . '_Control';
				$control_class = class_exists($custom_class) ? $custom_class : (class_exists('WP_Customize_' . ucfirst($control_type) . '_Control') ? 'WP_Customize_' . ucfirst($control_type) . '_Control' : 'WP_Customize_Control');
				$wp_customize->add_control(new $control_class($wp_customize, $el_key, $control_args));
			} else {
				$wp_customize->add_control($el_key, $control_args);
			}

		endforeach;
	}

	/**
	 * Evaluate active callback
	 */
	public function evaluate($control) {
		return true;
	}

	/**
	 * Check if a control should be active based on another control's value
	 *
	 * @param string $setting_id The setting ID to check
	 * @param mixed $expected_value The expected value
	 * @return bool
	 */
	public function is_control_value_active($setting_id, $expected_value) {
		$value = get_theme_mod($setting_id);
		return $value === $expected_value;
	}

}
