<?php
/**
 * Admin Settings for Product Compare Kav
 */

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

class ProductCompareKavAdmin {
    
    private static $instance = null;
    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance; 
    }
    
    private function __construct() {
        add_action('admin_menu', array($this, 'add_admin_menu'));
        add_action('admin_init', array($this, 'init_settings'));
    }
    
    public function add_admin_menu() {
        add_submenu_page(
            'woocommerce',
            __('Product Compare Settings', 'kavehTheme'),
            __('Product Compare', 'kavehTheme'),
            'manage_woocommerce',
            'product-compare-kav',
            array($this, 'admin_page')
        );
    }
    
    public function init_settings() {
        register_setting('product_compare_kav_settings', 'product_compare_kav_options', array(
            'sanitize_callback' => array($this, 'sanitize_options')
        ));
        
        add_settings_section(
            'product_compare_kav_general',
            __('General Settings', 'kavehTheme'),
            array($this, 'general_section_callback'),
            'product_compare_kav_settings'
        );
        
        add_settings_field(
            'max_products',
            __('Maximum Products to Compare', 'kavehTheme'),
            array($this, 'max_products_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'show_on_shop',
            __('Show Compare Button on Shop Page', 'kavehTheme'),
            array($this, 'show_on_shop_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'show_on_single',
            __('Show Compare Button on Single Product', 'kavehTheme'),
            array($this, 'show_on_single_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'compare_attributes',
            __('Attributes to Compare', 'kavehTheme'),
            array($this, 'compare_attributes_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'button_style',
            __('Button Style', 'kavehTheme'),
            array($this, 'button_style_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'button_text',
            __('Button Text', 'kavehTheme'),
            array($this, 'button_text_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'button_position',
            __('Button Position in Product Loop', 'kavehTheme'),
            array($this, 'button_position_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'button_display_mode',
            __('Button Display Mode', 'kavehTheme'),
            array($this, 'button_display_mode_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'button_background_color',
            __('Button Background Color', 'kavehTheme'),
            array($this, 'button_background_color_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'button_text_color',
            __('Button Text Color', 'kavehTheme'),
            array($this, 'button_text_color_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'button_border_radius',
            __('Button Border Radius (px)', 'kavehTheme'),
            array($this, 'button_border_radius_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        
        add_settings_field(
            'button_padding',
            __('Button Padding (px)', 'kavehTheme'),
            array($this, 'button_padding_callback'),
            'product_compare_kav_settings',
            'product_compare_kav_general'
        );
        

    }
    
    public function admin_page() {
        ?>
        <div class="wrap">
            <h1><?php _e('Product Compare Settings', 'kavehTheme'); ?></h1>
            
            <div class="notice notice-info">
                <p>
                    <strong><?php _e('Shortcodes:', 'kavehTheme'); ?></strong>
                </p>
                <p>
                    <strong><?php _e('Compare Table:', 'kavehTheme'); ?></strong> 
                    <code>[product_compare_kav]</code><br>
                    <?php _e('Use this shortcode to display the comparison table on any page or post.', 'kavehTheme'); ?>
                </p>
                <p>
                    <strong><?php _e('Compare Button:', 'kavehTheme'); ?></strong> 
                    <code>[compare_button id="123"]</code><br>
                    <?php _e('Use this shortcode to display a compare button for a specific product.', 'kavehTheme'); ?>
                </p>
                <p>
                    <strong><?php _e('Advanced Button Options:', 'kavehTheme'); ?></strong><br>
                    <code>[compare_button id="123" color="#ffffff" background_color="#ff0000" radius="10" tag="a" class="my-custom-class"]</code><br>
                    <?php _e('Shortcode styling options override admin settings. Available options:', 'kavehTheme'); ?>
                    <ul style="margin-left: 20px;">
                        <li><strong>id</strong>: <?php _e('Product ID (required)', 'kavehTheme'); ?></li>
                        <li><strong>color</strong>: <?php _e('Text color (hex code)', 'kavehTheme'); ?></li>
                        <li><strong>background_color</strong>: <?php _e('Background color (hex code)', 'kavehTheme'); ?></li>
                        <li><strong>radius</strong>: <?php _e('Border radius in pixels', 'kavehTheme'); ?></li>
                        <li><strong>tag</strong>: <?php _e('HTML tag (button, a, div, span)', 'kavehTheme'); ?></li>
                        <li><strong>class</strong>: <?php _e('Additional CSS classes', 'kavehTheme'); ?></li>
                        <li><strong>style</strong>: <?php _e('Button style (icon_text, icon_only, text_only)', 'kavehTheme'); ?></li>
                        <li><strong>text_add</strong>: <?php _e('Custom add text', 'kavehTheme'); ?></li>
                        <li><strong>text_remove</strong>: <?php _e('Custom remove text', 'kavehTheme'); ?></li>
                    </ul>
                </p>

            </div>
            
            <form method="post" action="options.php">
                <?php
                settings_fields('product_compare_kav_settings');
                do_settings_sections('product_compare_kav_settings');
                submit_button();
                ?>
            </form>
            
            <div class="card" style="margin-top: 20px;">
                <h2><?php _e('Plugin Information', 'kavehTheme'); ?></h2>
                <table class="form-table">
                    <tr>
                        <th><?php _e('Version:', 'kavehTheme'); ?></th>
                        <td><?php echo PRODUCT_COMPARE_KAV_VERSION; ?></td>
                    </tr>
                    <tr>
                        <th><?php _e('Plugin Directory:', 'kavehTheme'); ?></th>
                        <td><code><?php echo PRODUCT_COMPARE_KAV_PLUGIN_DIR; ?></code></td>
                    </tr>
                    <tr>
                        <th><?php _e('Assets URL:', 'kavehTheme'); ?></th>
                        <td><code><?php echo PRODUCT_COMPARE_KAV_ASSETS_URL; ?></code></td>
                    </tr>
                </table>
            </div>
            
            <div class="card" style="margin-top: 20px;">
                <h2><?php _e('How to Use', 'kavehTheme'); ?></h2>
                <h3><?php _e('Getting Started', 'kavehTheme'); ?></h3>
                <p><?php _e('1. Configure the settings above according to your preferences.', 'kavehTheme'); ?></p>
                <p><?php _e('2. The plugin automatically creates a "Product Compare" page with the shortcode. No manual setup needed!', 'kavehTheme'); ?></p>
                <p><?php _e('3. The compare buttons will automatically appear on your product pages based on the position and display mode settings.', 'kavehTheme'); ?></p>
                <p><?php _e('4. You can also manually add compare buttons using:', 'kavehTheme'); ?> <code>[compare_button id="PRODUCT_ID"]</code></p>
                <p><strong><?php _e('Note:', 'kavehTheme'); ?></strong> <?php _e('The compare page is automatically managed - if deleted, it will be recreated. Look for the "Product Compare Page" label in your Pages list.', 'kavehTheme'); ?></p>
            </div>
            
            <div class="card" style="margin-top: 20px;">
                <h2><?php _e('Template Override', 'kavehTheme'); ?></h2>
                <p><?php _e('You can override plugin templates by copying them to your theme:', 'kavehTheme'); ?></p>
                <ul>
                    <li><code>your-theme/product-compare-kav/compare-table.php</code></li>
                    <li><code>your-theme/product-compare-kav/compare-modal.php</code></li>
                </ul>
            </div>
        </div>
        <?php
    }
    
    public function general_section_callback() {
        echo '<p>' . __('Configure the basic settings for the product comparison feature.', 'kavehTheme') . '</p>';
    }
    
    public function max_products_callback() {
        $options = get_option('product_compare_kav_options');
        $value = isset($options['max_products']) ? $options['max_products'] : 4;
        echo '<input type="number" name="product_compare_kav_options[max_products]" value="' . esc_attr($value) . '" min="2" max="10" />';
        echo '<p class="description">' . __('Maximum number of products that can be compared at once (2-10).', 'kavehTheme') . '</p>';
    }
    
    public function show_on_shop_callback() {
        $options = get_option('product_compare_kav_options');
        $checked = isset($options['show_on_shop']) ? $options['show_on_shop'] : 1;
        echo '<input type="checkbox" name="product_compare_kav_options[show_on_shop]" value="1" ' . checked(1, $checked, false) . ' />';
        echo '<label>' . __('Show compare button on shop and archive pages', 'kavehTheme') . '</label>';
    }
    
    public function show_on_single_callback() {
        $options = get_option('product_compare_kav_options');
        $checked = isset($options['show_on_single']) ? $options['show_on_single'] : 1;
        echo '<input type="checkbox" name="product_compare_kav_options[show_on_single]" value="1" ' . checked(1, $checked, false) . ' />';
        echo '<label>' . __('Show compare button on single product pages', 'kavehTheme') . '</label>';
    }
    
    public function compare_attributes_callback() {
        $options = get_option('product_compare_kav_options');
        $attributes = isset($options['compare_attributes']) ? $options['compare_attributes'] : array();
        
        // Get all product attributes
        $product_attributes = wc_get_attribute_taxonomies();
        
        echo '<div style="max-height: 200px; overflow-y: auto; border: 1px solid #ddd; padding: 10px;">';
        
        // Default attributes
        $default_attributes = array(
            'weight' => __('Weight', 'kavehTheme'),
            'dimensions' => __('Dimensions', 'kavehTheme'),
            'sku' => __('SKU', 'kavehTheme'),
        );
        
        foreach ($default_attributes as $key => $label) {
            $checked = in_array($key, $attributes);
            echo '<label style="display: block; margin-bottom: 5px;">';
            echo '<input type="checkbox" name="product_compare_kav_options[compare_attributes][]" value="' . esc_attr($key) . '" ' . checked(true, $checked, false) . ' />';
            echo ' ' . esc_html($label);
            echo '</label>';
        }
        
        // Custom attributes
        foreach ($product_attributes as $attribute) {
            $key = 'pa_' . $attribute->attribute_name;
            $checked = in_array($key, $attributes);
            echo '<label style="display: block; margin-bottom: 5px;">';
            echo '<input type="checkbox" name="product_compare_kav_options[compare_attributes][]" value="' . esc_attr($key) . '" ' . checked(true, $checked, false) . ' />';
            echo ' ' . esc_html($attribute->attribute_label);
            echo '</label>';
        }
        
        echo '</div>';
        echo '<p class="description">' . __('Select which attributes to show in the comparison table.', 'kavehTheme') . '</p>';
    }
    
    public function button_style_callback() {
        $options = get_option('product_compare_kav_options');
        $value = isset($options['button_style']) ? $options['button_style'] : 'icon_text';
        
        echo '<select name="product_compare_kav_options[button_style]">';
        echo '<option value="icon_text" ' . selected('icon_text', $value, false) . '>' . __('Icon + Text', 'kavehTheme') . '</option>';
        echo '<option value="icon_only" ' . selected('icon_only', $value, false) . '>' . __('Icon Only', 'kavehTheme') . '</option>';
        echo '<option value="text_only" ' . selected('text_only', $value, false) . '>' . __('Text Only', 'kavehTheme') . '</option>';
        echo '</select>';
        echo '<p class="description">' . __('Choose how the compare button should be displayed.', 'kavehTheme') . '</p>';
    }
    
    public function button_text_callback() {
        $options = get_option('product_compare_kav_options');
        $add_text = isset($options['button_text_add']) ? $options['button_text_add'] : __('Add to compare', 'kavehTheme');
        $remove_text = isset($options['button_text_remove']) ? $options['button_text_remove'] : __('Remove from compare', 'kavehTheme');
        
        echo '<table>';
        echo '<tr><td><label>' . __('Add Text:', 'kavehTheme') . '</label></td>';
        echo '<td><input type="text" name="product_compare_kav_options[button_text_add]" value="' . esc_attr($add_text) . '" /></td></tr>';
        echo '<tr><td><label>' . __('Remove Text:', 'kavehTheme') . '</label></td>';
        echo '<td><input type="text" name="product_compare_kav_options[button_text_remove]" value="' . esc_attr($remove_text) . '" /></td></tr>';
        echo '</table>';
        echo '<p class="description">' . __('Customize the button text for add and remove actions.', 'kavehTheme') . '</p>';
    }
    
    public function button_position_callback() {
        $options = get_option('product_compare_kav_options');
        $value = isset($options['button_position']) ? $options['button_position'] : 'after_add_to_cart';
        
        echo '<select name="product_compare_kav_options[button_position]">';
        echo '<option value="before_title" ' . selected('before_title', $value, false) . '>' . __('Before Product Title', 'kavehTheme') . '</option>';
        echo '<option value="after_title" ' . selected('after_title', $value, false) . '>' . __('After Product Title', 'kavehTheme') . '</option>';
        echo '<option value="before_price" ' . selected('before_price', $value, false) . '>' . __('Before Price', 'kavehTheme') . '</option>';
        echo '<option value="after_price" ' . selected('after_price', $value, false) . '>' . __('After Price', 'kavehTheme') . '</option>';
        echo '<option value="before_add_to_cart" ' . selected('before_add_to_cart', $value, false) . '>' . __('Before Add to Cart', 'kavehTheme') . '</option>';
        echo '<option value="after_add_to_cart" ' . selected('after_add_to_cart', $value, false) . '>' . __('After Add to Cart', 'kavehTheme') . '</option>';
        echo '</select>';
        echo '<p class="description">' . __('Choose where the compare button should appear in product loops (only applies when automatic mode is enabled).', 'kavehTheme') . '</p>';
    }
    
    public function button_display_mode_callback() {
        $options = get_option('product_compare_kav_options');
        $value = isset($options['button_display_mode']) ? $options['button_display_mode'] : 'automatic';
        
        echo '<select name="product_compare_kav_options[button_display_mode]">';
        echo '<option value="automatic" ' . selected('automatic', $value, false) . '>' . __('Automatic - Show on all product loops', 'kavehTheme') . '</option>';
        echo '<option value="manual" ' . selected('manual', $value, false) . '>' . __('Manual - Use shortcode or hooks only', 'kavehTheme') . '</option>';
        echo '</select>';
        echo '<p class="description">' . __('Choose how compare buttons are displayed. Automatic mode shows buttons based on position settings. Manual mode requires shortcode [compare_button] or theme integration.', 'kavehTheme') . '</p>';
    }
    
    public function button_background_color_callback() {
        $options = get_option('product_compare_kav_options');
        $value = isset($options['button_background_color']) ? $options['button_background_color'] : '#007cba';
        
        echo '<input type="color" name="product_compare_kav_options[button_background_color]" value="' . esc_attr($value) . '" />';
        echo '<p class="description">' . __('Choose the background color for compare buttons.', 'kavehTheme') . '</p>';
    }
    
    public function button_text_color_callback() {
        $options = get_option('product_compare_kav_options');
        $value = isset($options['button_text_color']) ? $options['button_text_color'] : '#ffffff';
        
        echo '<input type="color" name="product_compare_kav_options[button_text_color]" value="' . esc_attr($value) . '" />';
        echo '<p class="description">' . __('Choose the text color for compare buttons.', 'kavehTheme') . '</p>';
    }
    
    public function button_border_radius_callback() {
        $options = get_option('product_compare_kav_options');
        $value = isset($options['button_border_radius']) ? $options['button_border_radius'] : '4';
        
        echo '<input type="number" name="product_compare_kav_options[button_border_radius]" value="' . esc_attr($value) . '" min="0" max="50" step="1" />';
        echo '<span> px</span>';
        echo '<p class="description">' . __('Set the border radius for compare buttons (0-50px).', 'kavehTheme') . '</p>';
    }
    
    public function button_padding_callback() {
        $options = get_option('product_compare_kav_options');
        $value = isset($options['button_padding']) ? $options['button_padding'] : '8';
        
        echo '<input type="number" name="product_compare_kav_options[button_padding]" value="' . esc_attr($value) . '" min="0" max="30" step="1" />';
        echo '<span> px</span>';
        echo '<p class="description">' . __('Set the padding for compare buttons (0-30px).', 'kavehTheme') . '</p>';
    }
    

    
    public function sanitize_options($input) {
        $sanitized = array();
        
        if (isset($input['max_products'])) {
            $sanitized['max_products'] = absint($input['max_products']);
        }
        
        if (isset($input['show_on_shop'])) {
            $sanitized['show_on_shop'] = 1;
        }
        
        if (isset($input['show_on_single'])) {
            $sanitized['show_on_single'] = 1;
        }
        
        if (isset($input['compare_attributes']) && is_array($input['compare_attributes'])) {
            $sanitized['compare_attributes'] = array_map('sanitize_text_field', $input['compare_attributes']);
        }
        
        if (isset($input['button_style'])) {
            $allowed_styles = array('icon_text', 'icon_only', 'text_only');
            $sanitized['button_style'] = in_array($input['button_style'], $allowed_styles) ? $input['button_style'] : 'icon_text';
        }
        
        if (isset($input['button_text_add'])) {
            $sanitized['button_text_add'] = sanitize_text_field($input['button_text_add']);
        }
        
        if (isset($input['button_text_remove'])) {
            $sanitized['button_text_remove'] = sanitize_text_field($input['button_text_remove']);
        }
        
        if (isset($input['button_position'])) {
            $allowed_positions = array('before_title', 'after_title', 'before_price', 'after_price', 'before_add_to_cart', 'after_add_to_cart');
            $sanitized['button_position'] = in_array($input['button_position'], $allowed_positions) ? $input['button_position'] : 'after_add_to_cart';
        }
        
        if (isset($input['button_display_mode'])) {
            $allowed_modes = array('automatic', 'manual');
            $sanitized['button_display_mode'] = in_array($input['button_display_mode'], $allowed_modes) ? $input['button_display_mode'] : 'automatic';
        }
        
        if (isset($input['button_background_color'])) {
            $sanitized['button_background_color'] = sanitize_hex_color($input['button_background_color']);
        }
        
        if (isset($input['button_text_color'])) {
            $sanitized['button_text_color'] = sanitize_hex_color($input['button_text_color']);
        }
        
        if (isset($input['button_border_radius'])) {
            $sanitized['button_border_radius'] = absint($input['button_border_radius']);
            if ($sanitized['button_border_radius'] > 50) {
                $sanitized['button_border_radius'] = 50;
            }
        }
        
        if (isset($input['button_padding'])) {
            $sanitized['button_padding'] = absint($input['button_padding']);
            if ($sanitized['button_padding'] > 30) {
                $sanitized['button_padding'] = 30;
            }
        }
        

        
        return $sanitized;
    }
    
    public static function get_option($key, $default = null) {
        $options = get_option('product_compare_kav_options', array());
        return isset($options[$key]) ? $options[$key] : $default;
    }
}