<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Icon_Style_Controls;

class Sifency_Widget_Social extends Widget_Base {
    use Sifency_Animation_Controls;
        use Sifency_Icon_Style_Controls;

    public function get_name() {
        return 'sifency-social';
    }

    public function get_title() {
        return __('Sifency Social Icons', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-social-icons';
    }

    public function get_categories() {
        return ['sifency-addons-widgets'];
    }

    public function get_keywords() {
        return ['social', 'icons', 'links', 'media'];
    }


    public function get_style_depends() {
	    return [ 'sifency-frontend-style', 'sifency-component-style' , 'sifency-icon' ];
    }
     
    protected function register_controls() {
        // --- Content Section ---
        $this->start_controls_section(
            'section_social_icons',
            [ 'label' => esc_html__( 'Social Icons', 'sifency-addons' ) ]
        );

        $repeater = new Repeater();
        $repeater->add_control(
            'social_icon',
            [
                'label' => esc_html__( 'Icon', 'sifency-addons' ),
                'type' => Controls_Manager::ICONS,
                'default' => [ 'value' => 'fab fa-wordpress', 'library' => 'sifency-icons' ],
            ]
        );
        $repeater->add_control(
            'link',
            [ 'label' => esc_html__( 'Link', 'sifency-addons' ), 'type' => Controls_Manager::URL, 'default' => [ 'is_external' => 'true' ] ]
        );
        $this->add_control(
            'social_icon_list',
            [
                'label' => esc_html__( 'Social Icons', 'sifency-addons' ),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [
                    [ 'social_icon' => [ 'value' => 'icon-facebook', 'library' => 'sifency-icons' ] ],
                    [ 'social_icon' => [ 'value' => 'icon-linkedin', 'library' => 'sifency-icons' ] ],
                    [ 'social_icon' => [ 'value' => 'icon-instagram', 'library' => 'sifency-icons' ] ],
                ],
                'title_field' => '<# var icon = elementor.helpers.getSocialNetworkNameFromIcon( social_icon ); #><i class="{{ social_icon.value }}" aria-hidden="true"></i> {{{ icon || "Social Icon" }}}',
            ]
        );
        $this->add_control(
            'shape',
            [ 'label' => esc_html__( 'Shape', 'sifency-addons' ), 'type' => Controls_Manager::SELECT, 'default' => 'circle', 'options' => [ 'rounded' => esc_html__( 'Rounded', 'sifency-addons' ), 'square' => esc_html__( 'Square', 'sifency-addons' ), 'circle' => esc_html__( 'Circle', 'sifency-addons' ) ], 'prefix_class' => 'elementor-shape-' ]
        );
        $this->add_responsive_control(
			'align',
			[
				'label' => esc_html__( 'Alignment', 'sifency-addons' ), 'type' => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-h-align-left' ],
					'center' => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-h-align-center' ],
					'right' => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-h-align-right' ],
				],
				'selectors' => [ '{{WRAPPER}} .sifency-social-icons' => 'justify-content: {{VALUE}};' ],
			]
		);
        $this->end_controls_section();

        // --- Style Section ---
        $this->start_controls_section(
            'section_style_social_icon',
            [ 'label' => esc_html__( 'Icon', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->add_responsive_control(
            'gap',
            [ 'label' => esc_html__( 'Gap', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-social-icons' => 'gap: {{SIZE}}{{UNIT}};' ] ]
        );
        $this->add_responsive_control(
            'social_icon_container_width',
            [
                'label' => esc_html__('Icon Container Width', 'sifency-addons'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', '%'],
                'range' => [
                    'px' => ['min' => 20, 'max' => 200],
                    '%' => ['min' => 10, 'max' => 100],
                ],
                'default' => [
                    'size' => 48,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-social-icon' => 'width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_responsive_control(
            'social_icon_container_height',
            [
                'label' => esc_html__('Icon Container Height', 'sifency-addons'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', '%'],
                'range' => [
                    'px' => ['min' => 20, 'max' => 200],
                    '%' => ['min' => 10, 'max' => 100],
                ],
                'default' => [
                    'size' => 48,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-social-icon' => 'height: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-social-icon i' => 'line-height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->end_controls_section();

        $this->add_sifency_icon_style_controls();
        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();

        if ( empty($settings['social_icon_list']) ) {
            return;
        }
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-social-icons'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php foreach ( $settings['social_icon_list'] as $index => $item ) :
                $repeater_key = $this->get_repeater_setting_key('social-icon', 'social_icon_list', $index);
                $this->add_render_attribute( $repeater_key, 'class', 'sifency-social-icon sifency-icon-style' );
                if ( ! empty( $item['link']['url'] ) ) {
                    $this->add_link_attributes( $repeater_key, $item['link'] );
                }

                $animation_item_attrs_string = $animation_manager->get_item_attributes([]);
                if ( ! empty($animation_item_attrs_string) ) {
                    preg_match('/class="([^"]+)"/', $animation_item_attrs_string, $class_matches);
                    if (isset($class_matches[1])) {
                        $this->add_render_attribute($repeater_key, 'class', $class_matches[1]);
                    }
                    preg_match_all('/(data-[^=]+="[^"]+")/', $animation_item_attrs_string, $data_matches);
                    if (isset($data_matches[1])) {
                        foreach($data_matches[1] as $attr) {
                            list($key, $value) = explode('=', $attr);
                            $this->add_render_attribute($repeater_key, $key, trim($value, '"'));
                        }
                    }
                }
                ?>
                <a <?php echo wp_kses_post ($this->get_render_attribute_string($repeater_key)); ?>>
                    <?php \Elementor\Icons_Manager::render_icon( $item['social_icon'], [ 'aria-hidden' => 'true' ] ); ?>
                </a>
            <?php endforeach; ?>
        </div>
        <?php
    }
}