<?php
/**
 * Customize Icon Picker Control
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

if ( ! class_exists( 'WP_Customize_Control' ) ) {
    return;
}

/**
 * Icon Picker Control
 */
class Paws_And_Care_Customize_Icon_Picker_Control extends WP_Customize_Control {

    /**
     * Control type
     */
    public $type = 'icon-picker';

    /**
     * Enqueue scripts and styles
     */
    public function enqueue() {
        // Enqueue the icon picker CSS
        wp_enqueue_style(
            'paws-and-care-fontawesome-iconpicker',
            get_template_directory_uri() . '/inc/customizer/customizer-repeater/css/fontawesome-iconpicker.min.css',
            array(),
            '1.0.0'
        );

        // Enqueue the icon picker JS
        wp_enqueue_script(
            'paws-and-care-fontawesome-iconpicker',
            get_template_directory_uri() . '/inc/customizer/customizer-repeater/js/fontawesome-iconpicker.js',
            array( 'jquery' ),
            '1.0.0',
            true
        );
    }

    /**
     * Render the control
     */
    public function render_content() {
        $value = $this->value();
        
        // Determine the icon display class with proper FA7 prefix
        $icon_display_class = '';
        if ( ! empty( $value ) ) {
            // Check if it's a brand icon
            $brand_icons = array( 'fa-facebook', 'fa-facebook-f', 'fa-twitter', 'fa-x-twitter', 'fa-instagram', 'fa-linkedin', 'fa-linkedin-in', 'fa-youtube', 'fa-tiktok', 'fa-pinterest', 'fa-pinterest-p', 'fa-whatsapp', 'fa-telegram', 'fa-discord', 'fa-slack', 'fa-reddit', 'fa-github', 'fa-gitlab', 'fa-dribbble', 'fa-behance', 'fa-medium', 'fa-spotify', 'fa-soundcloud', 'fa-twitch', 'fa-vimeo', 'fa-snapchat', 'fa-skype', 'fa-google', 'fa-apple', 'fa-android', 'fa-windows', 'fa-linux', 'fa-paypal', 'fa-stripe', 'fa-amazon', 'fa-wordpress', 'fa-blogger', 'fa-tumblr', 'fa-flickr', 'fa-stack-overflow', 'fa-codepen', 'fa-bitbucket' );
            
            $is_brand = false;
            foreach ( $brand_icons as $brand ) {
                if ( strpos( $value, $brand ) !== false ) {
                    $is_brand = true;
                    break;
                }
            }
            
            // Remove any existing prefix
            $clean_icon = str_replace( array( 'fa-solid ', 'fa-brands ', 'fa-regular ', 'fa ' ), '', $value );
            $icon_display_class = ( $is_brand ? 'fa-brands ' : 'fa-solid ' ) . $clean_icon;
        }
        ?>
        <div class="paws-icon-picker-control">
            <?php if ( ! empty( $this->label ) ) : ?>
                <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
            <?php endif; ?>
            
            <?php if ( ! empty( $this->description ) ) : ?>
                <span class="description customize-control-description"><?php echo esc_html( $this->description ); ?></span>
            <?php endif; ?>
            
            <div class="input-group icp-container">
                <input 
                    data-placement="bottomRight" 
                    class="icp icp-auto" 
                    value="<?php echo esc_attr( $value ); ?>" 
                    type="text"
                    <?php $this->link(); ?>
                >
                <span class="input-group-addon">
                    <?php if ( ! empty( $icon_display_class ) ) : ?>
                        <i class="<?php echo esc_attr( $icon_display_class ); ?>"></i>
                    <?php endif; ?>
                </span>
            </div>
            <?php $this->render_icon_picker_popover(); ?>
        </div>
        <?php
    }

    /**
     * Render the icon picker popover
     */
    private function render_icon_picker_popover() {
        $icons_file = get_template_directory() . '/inc/customizer/customizer-repeater/inc/icons.php';
        if ( file_exists( $icons_file ) ) {
            include $icons_file;
        }
    }
}

