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

class Sifency_Widget_Share extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Button_Style_Controls;
	use Sifency_Icon_Style_Controls;

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

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

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

    public function get_categories() {
        return [ 'sifency-addons-widgets' ];
    }
    public function get_style_depends() {
	    return [ 'sifency-frontend-style' , 'sifency-icon' ];
    }

    /**
     * Helper function to get default social icons for the editor.
     * @return array
     */
    private function get_default_icons() {
        return [
            'facebook'  => [ 'value' => 'icon-facebook', 'library' => 'sifency-icons' ],
            'twitter'   => [ 'value' => 'icon-twitter', 'library' => 'sifency-icons' ],
            'linkedin'  => [ 'value' => 'icon-linkedin', 'library' => 'sifency-icons' ],
            'pinterest' => [ 'value' => 'icon-pinterest', 'library' => 'sifency-icons' ],
            'whatsapp'  => [ 'value' => 'icon-whatsapp', 'library' => 'sifency-icons' ],
            'email'     => [ 'value' => 'icon-mail', 'library' => 'sifency-icons' ],
        ];
    }

    protected function register_controls() {

        // --- Content Section ---
        $this->start_controls_section(
            'section_content',
            [ 'label' => esc_html__( 'Share Buttons', 'sifency-addons' ) ]
        );

        $repeater = new Repeater();

		$repeater->add_control(
			'social_network',
			[
				'label' => esc_html__( 'Social Network', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'facebook' => 'Facebook',
					'twitter' => 'Twitter',
					'linkedin' => 'LinkedIn',
					'pinterest' => 'Pinterest',
					'whatsapp' => 'WhatsApp',
					'email' => 'Email',
				],
				'default' => 'facebook',
			]
		);
        
        $repeater->add_control(
            'social_icon',
            [
                'label' => esc_html__( 'Social Icon', 'sifency-addons' ),
                'type' => Controls_Manager::ICONS,
                'default' => [
                    'value' => 'icon-facebook',
                    'library' => 'sifency-icons',
                ],
                'recommended' => $this->get_default_icons(),
            ]
        );

        $repeater->add_control(
			'custom_url',
			[
				'label' => esc_html__( 'Custom URL', 'sifency-addons' ),
				'type' => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'https://your-link.com', 'sifency-addons' ),
                'description' => esc_html__( 'Leave empty to share the current page URL.', 'sifency-addons' ),
                'dynamic' => [ 'active' => true ],
			]
		);
        $repeater->add_control(
			'custom_text',
			[
				'label' => esc_html__( 'Custom Share Text', 'sifency-addons' ),
				'type' => Controls_Manager::TEXT,
                'description' => esc_html__( 'For Twitter, WhatsApp, Email.', 'sifency-addons' ),
                'dynamic' => [ 'active' => true ],
			]
		);

        $default_icons = $this->get_default_icons();

        $this->add_control(
			'social_buttons',
			[
				'label' => esc_html__( 'Social Buttons', 'sifency-addons' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[ 'social_network' => 'facebook', 'social_icon' => $default_icons['facebook'] ],
					[ 'social_network' => 'linkedin', 'social_icon' => $default_icons['linkedin'] ],
				],
				'title_field' => '{{{ social_network.charAt(0).toUpperCase() + social_network.slice(1) }}}',
			]
		);
        $this->add_control(
            'view',
            [
                'label' => esc_html__( 'View', 'sifency-addons' ),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'icon' => 'Icon',
                    'text' => 'Text',
                    'icon-text' => 'Icon & Text',
                ],
                'default' => 'icon-text',
                'prefix_class' => 'sifency-share-buttons--view-',
                'separator' => 'before',
            ]
        );
        $this->end_controls_section();
        
		$this->add_sifency_button_style_controls();
		$this->add_sifency_icon_style_controls();
		$this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        if ( empty($settings['social_buttons']) ) {
            return;
        }
        $animation_manager = new Sifency_Animation_Manager($settings);
        
        $wrapper_classes = ['sifency-share-buttons'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php 
            
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php foreach ( $settings['social_buttons'] as $index => $item ) :
                $network_name = $item['social_network'];
                $url = empty($item['custom_url']) ? get_permalink() : $item['custom_url'];
                $text = empty($item['custom_text']) ? get_the_title() : $item['custom_text'];
                $share_url = '';

                switch ($network_name) {
                    case 'facebook': $share_url = 'https://www.facebook.com/sharer/sharer.php?u=' . urlencode($url); break;
                    case 'twitter': $share_url = 'https://twitter.com/intent/tweet?url=' . urlencode($url) . '&text=' . urlencode($text); break;
                    case 'linkedin': $share_url = 'https://www.linkedin.com/shareArticle?mini=true&url=' . urlencode($url); break;
                    case 'pinterest': $share_url = 'https://pinterest.com/pin/create/button/?url=' . urlencode($url); break;
                    case 'whatsapp': $share_url = 'https://api.whatsapp.com/send?text=' . urlencode($text . ' ' . $url); break;
                    case 'email': $share_url = 'mailto:?subject=' . urlencode($text) . '&body=' . urlencode($url); break;
                }

                $button_key = 'button_' . $item['_id'];
                $this->add_render_attribute($button_key, [
                    'href'   => $share_url,
                    'target' => '_blank',
                    'rel'    => 'noopener noreferrer',
                ], true);

                $item_classes = [
                    'sifency-button-style',
                    'sifency-share-button',
                    'sifency-share-button--' . $network_name,
                    'elementor-repeater-item-' . $item['_id'],
                ];

                $animation_item_attrs = $animation_manager->get_item_attributes($item_classes);
                ?>
                
                <a <?php echo wp_kses_post( $this->get_render_attribute_string($button_key)); ?> <?php echo wp_kses_post ( $animation_item_attrs); ?>>
                    <?php if ( ! empty( $item['social_icon']['value'] ) ) : ?>
                        <span class="sifency-share-button__icon">
                            <span class="sifency-icon-style">
                                <?php \Elementor\Icons_Manager::render_icon( $item['social_icon'], [ 'aria-hidden' => 'true' ] ); ?>
                            </span>
                        </span>
                    <?php endif; ?>
                    <span class="sifency-share-button__text">
                        <?php echo esc_html( ucfirst($network_name) ); ?>
                    </span>
                </a>
            <?php endforeach; ?>
        </div>
        <?php
    }
}