<?php
/**
 * Social Media Icons Customizer Options
 *
 * @package Pawfect
 * @since 1.0.0
 */

// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

if (!class_exists('Pawfect_Options_Social_Media')):

/**
 * Class Pawfect_Options_Social_Media
 *
 * Handles social media icons customizer settings
 */
class Pawfect_Options_Social_Media
{
    /**
     * Add customizer options
     *
     * @param WP_Customize_Manager $wp_customize
     */
    public function pawfect_customizer_options($wp_customize)
    {
        /**
         * Section: Social Media Icons
         */
        $wp_customize->add_section('pawfect_social_media_section', array(
            'title' => esc_html__('Social Media Icons', 'pawfect'),
            'panel' => 'pawfect_theme_settings',
            'priority' => 35,
            'description' => esc_html__('Configure social media icons that will appear in the promotional popup and other locations throughout the theme.', 'pawfect'),
        ));

        // Enable/Disable Social Media Icons
        $wp_customize->add_setting('pawfect_social_media_enable', array(
            'default' => true,
            'sanitize_callback' => 'pawfect_sanitize_checkbox',
            'transport' => 'refresh',
        ));

        $wp_customize->add_control('pawfect_social_media_enable', array(
            'label' => esc_html__('Enable Social Media Icons', 'pawfect'),
            'description' => esc_html__('Toggle to show or hide social media icons throughout the theme.', 'pawfect'),
            'section' => 'pawfect_social_media_section',
            'type' => 'checkbox',
            'priority' => 10,
        ));

        // Social Media Icons Repeater using existing pawfect_Repeater control
        $wp_customize->add_setting('pawfect_social_media_icons', array(
            'default' => json_encode(array(
                array(
                    'id' => 'social-repeater-' . uniqid(),
                    'title' => esc_html__( 'Facebook', 'pawfect' ),
                    'link' => 'https://facebook.com',
                    'icon_value' => 'fab fa-facebook-f',
                    'choice' => 'customizer_repeater_icon'
                ),
                array(
                    'id' => 'social-repeater-' . uniqid(),
                    'title' => esc_html__( 'Twitter', 'pawfect' ),
                    'link' => 'https://twitter.com',
                    'icon_value' => 'fab fa-twitter',
                    'choice' => 'customizer_repeater_icon'
                ),
                array(
                    'id' => 'social-repeater-' . uniqid(),
                    'title' => esc_html__( 'Instagram', 'pawfect' ),
                    'link' => 'https://instagram.com',
                    'icon_value' => 'fab fa-instagram',
                    'choice' => 'customizer_repeater_icon'
                ),
                array(
                    'id' => 'social-repeater-' . uniqid(),
                    'title' => esc_html__( 'Pinterest', 'pawfect' ),
                    'link' => 'https://pinterest.com',
                    'icon_value' => 'fab fa-pinterest',
                    'choice' => 'customizer_repeater_icon'
                ) 
            )),
            'sanitize_callback' => 'pawfect_repeater_sanitize',
            'transport' => 'postMessage', // Changed to postMessage for selective refresh
        ));

        // Use the existing pawfect_Repeater control
        $wp_customize->add_control(new pawfect_Repeater($wp_customize, 'pawfect_social_media_icons', array(
            'label' => esc_html__('Social Media Platforms', 'pawfect'),
            'section' => 'pawfect_social_media_section',
            'add_field_label' => esc_html__('Add Social Platform', 'pawfect'),
            'item_name' => esc_html__('Social Platform', 'pawfect'),
            'priority' => 20,
            'customizer_repeater_title_control' => true,
            'customizer_repeater_link_control' => true,
            'customizer_repeater_icon_control' => true,
            'active_callback' => function() {
                return get_theme_mod('pawfect_social_media_enable', true);
            }
        )));

        // Add selective refresh for social media icons
        if (isset($wp_customize->selective_refresh)) {
            $wp_customize->selective_refresh->add_partial('pawfect_social_media_icons', array(
                'selector' => '.pawfect-social-icons, .social-media-icons, .header-social-icons, .footer-social-icons',
                'render_callback' => array($this, 'render_social_media_icons'),
                'container_inclusive' => false,
                'fallback_refresh' => true,
            ));
        }

    }

    /**
     * Render callback for social media icons selective refresh
     */
    public function render_social_media_icons() {
        // Get the social media icons from theme mod
        $social_icons = get_theme_mod('pawfect_social_media_icons', '');

        if (empty($social_icons)) {
            return '';
        }

        // Decode JSON if it's a string
        if (is_string($social_icons)) {
            $social_icons = json_decode($social_icons, true);
        }

        if (!is_array($social_icons) || empty($social_icons)) {
            return '';
        }

        // Generate the social media icons HTML
        $output = '<div class="pawfect-social-icons">';

        foreach ($social_icons as $icon) {
            if (!empty($icon['link']) && !empty($icon['icon_value'])) {
                $title = !empty($icon['title']) ? esc_attr($icon['title']) : '';
                $link = esc_url($icon['link']);
                $icon_class = esc_attr($icon['icon_value']);

                $output .= sprintf(
                    '<a href="%s" class="pawfect-social-icon" title="%s" target="_blank" rel="noopener noreferrer">
                        <i class="%s"></i>
                    </a>',
                    $link,
                    $title,
                    $icon_class
                );
            }
        }

        $output .= '</div>';

        return $output;
    }
}

endif;

/**
 * Sanitization function for repeater control (if not already defined)
 */
if (!function_exists('pawfect_repeater_sanitize')) {
    function pawfect_repeater_sanitize($input)
    {
        $input_decoded = json_decode($input, true);
        if (!empty($input_decoded)) {
            foreach ($input_decoded as $index => $item) {
                foreach ($item as $key => $value) {
                    if ($key === 'link') {
                        $input_decoded[$index][$key] = esc_url_raw($value);
                    } else {
                        $input_decoded[$index][$key] = sanitize_text_field($value);
                    }
                }
            }
            return json_encode($input_decoded);
        }
        return $input;
    }
}
