<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Repeater;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Widget_Share extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-share',
			'title'          => 'Sifency Social Share',
			'icon'           => 'eicon-share',
			'category'       => 'sifency-addons-widgets',
			'script_depends' => array( 'sifency-share-script' ),
			'style_depends'  => array( 'sifency-frontend-style', 'sifency-typo-style', 'sifency-icon' ),
		);}
	private function get_default_icons() {
		return array(
			'facebook'  => array(
				'value'   => 'fab fa-facebook-f',
				'library' => 'fa-brands',
			),
			'twitter'   => array(
				'value'   => 'fab fa-twitter',
				'library' => 'fa-brands',
			),
			'linkedin'  => array(
				'value'   => 'fab fa-linkedin-in',
				'library' => 'fa-brands',
			),
			'pinterest' => array(
				'value'   => 'fab fa-pinterest-p',
				'library' => 'fa-brands',
			),
			'whatsapp'  => array(
				'value'   => 'fab fa-whatsapp',
				'library' => 'fa-brands',
			),
			'email'     => array(
				'value'   => 'fas fa-envelope',
				'library' => 'fa-solid',
			),
			'reddit'    => array(
				'value'   => 'fab fa-reddit-alien',
				'library' => 'fa-brands',
			),
			'telegram'  => array(
				'value'   => 'fab fa-telegram-plane',
				'library' => 'fa-brands',
			),
		);
	}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Share Buttons', 'sifency-addons' ) )
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'social_network',
			array(
				'label'   => esc_html__( 'Social Network', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'facebook'  => 'Facebook',
					'twitter'   => 'Twitter (X)',
					'linkedin'  => 'LinkedIn',
					'pinterest' => 'Pinterest',
					'whatsapp'  => 'WhatsApp',
					'reddit'    => 'Reddit',
					'telegram'  => 'Telegram',
					'email'     => 'Email',
				),
				'default' => 'facebook',
			)
		);

		$repeater->add_control(
			'social_icon',
			array(
				'label' => esc_html__( 'Social Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);
		$repeater->add_control(
			'custom_text',
			array(
				'label'       => esc_html__( 'Custom Label', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'Default',
			)
		);

		$default_icons = $this->get_default_icons();

		$this->add_control(
			'social_buttons',
			array(
				'label'       => esc_html__( 'Social Buttons', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'social_network' => 'facebook',
						'social_icon'    => $default_icons['facebook'],
					),
					array(
						'social_network' => 'twitter',
						'social_icon'    => $default_icons['twitter'],
					),
					array(
						'social_network' => 'linkedin',
						'social_icon'    => $default_icons['linkedin'],
					),
					array(
						'social_network' => 'pinterest',
						'social_icon'    => $default_icons['pinterest'],
					),
					array(
						'social_network' => 'telegram',
						'social_icon'    => $default_icons['telegram'],
					),
					array(
						'social_network' => 'whatsapp',
						'social_icon'    => $default_icons['whatsapp'],
					),
					array(
						'social_network' => 'email',
						'social_icon'    => $default_icons['email'],
					),
					array(
						'social_network' => 'reddit',
						'social_icon'    => $default_icons['reddit'],
					),
				),
				'title_field' => '{{{ social_network.charAt(0).toUpperCase() + social_network.slice(1) }}}',
			)
		);
		$this->add_control(
			'view',
			array(
				'label'        => esc_html__( 'View Mode', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'icon'         => 'Icon Only',
					'text'         => 'Text Only',
					'icon-text'    => 'Icon & Text',
					'hover-reveal' => 'Hover Reveal (Compact)',
				),
				'default'      => 'icon-text',
				'prefix_class' => 'sifency-share-view-',
			)
		);
		$this->add_control(
			'trigger_icon',
			array(
				'label'     => esc_html__( 'Main Share Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-share-alt',
					'library' => 'fa-solid',
				),
				'condition' => array( 'view' => 'hover-reveal' ),
			)
		);

		$this->add_control(
			'trigger_text',
			array(
				'label'     => esc_html__( 'Main Label', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Share',
				'condition' => array( 'view' => 'hover-reveal' ),
			)
		);
		$this->add_control(
			'skin',
			array(
				'label'        => esc_html__( 'Skin', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'flat'     => 'Flat (Solid)',
					'gradient' => 'Gradient',
					'framed'   => 'Framed (Border)',
					'minimal'  => 'Minimal (Clean)',
					'boxed'    => 'Boxed (Large)',
				),
				'default'      => 'flat',
				'prefix_class' => 'sifency-share-skin-',
			)
		);
		$this->add_control(
			'shape',
			array(
				'label'        => esc_html__( 'Shape', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'square'  => 'Square',
					'rounded' => 'Rounded',
					'circle'  => 'Circle',
				),
				'default'      => 'rounded',
				'prefix_class' => 'sifency-share-shape-',
			)
		);

		$this->add_responsive_control(
			'columns',
			array(
				'label'        => esc_html__( 'Columns', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'auto',
				'options'      => array(
					'auto' => 'Auto Width',
					'1'    => '1 Column',
					'2'    => '2 Columns',
					'3'    => '3 Columns',
					'4'    => '4 Columns',
					'6'    => '6 Columns',
				),
				'prefix_class' => 'sifency-share-cols-',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- STYLE SECTION ---
		$this->start_controls_section(
			'section_style_general',
			array(
				'label' => esc_html__( 'Buttons Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'button_gap',
			array(
				'label'     => esc_html__( 'Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 10 ),
				'selectors' => array( '{{WRAPPER}} .sifency-share-buttons' => 'gap: {{SIZE}}px;' ),
			)
		);
		$this->add_control(
			'color_source',
			array(
				'label'        => esc_html__( 'Color Source', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'official' => 'Official Brand Colors',
					'custom'   => 'Custom Colors',
				),
				'default'      => 'official',
				'prefix_class' => 'sifency-share-color-',
			)
		);

		$this->start_controls_tabs( 'tabs_button_style' );

		$this->start_controls_tab( 'tab_btn_normal', array( 'label' => 'Normal' ) );

		$this->add_control(
			'custom_primary_color',
			array(
				'label'     => esc_html__( 'Primary Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'condition' => array( 'color_source' => 'custom' ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-share-button' => 'background-color: {{VALUE}}; border-color: {{VALUE}};',
					'{{WRAPPER}}.sifency-share-skin-framed .sifency-share-button' => 'color: {{VALUE}}; background-color: transparent; border-color: {{VALUE}};',
					'{{WRAPPER}}.sifency-share-skin-minimal .sifency-share-button' => 'color: {{VALUE}}; background-color: transparent;',
				),
			)
		);
		$this->add_control(
			'custom_icon_color',
			array(
				'label'     => esc_html__( 'Icon/Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'condition' => array( 'color_source' => 'custom' ),
				'selectors' => array( '{{WRAPPER}} .sifency-share-button' => 'color: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab( 'tab_btn_hover', array( 'label' => 'Hover' ) );

		$this->add_control(
			'custom_primary_color_hover',
			array(
				'label'     => esc_html__( 'Primary Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'condition' => array( 'color_source' => 'custom' ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-share-button:hover' => 'background-color: {{VALUE}}; border-color: {{VALUE}};',
					'{{WRAPPER}}.sifency-share-skin-framed .sifency-share-button:hover' => 'background-color: {{VALUE}}; color: #fff;',
				),
			)
		);
		$this->add_control(
			'custom_icon_color_hover',
			array(
				'label'     => esc_html__( 'Icon/Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'condition' => array( 'color_source' => 'custom' ),
				'selectors' => array( '{{WRAPPER}} .sifency-share-button:hover' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'official_hover_animation',
			array(
				'label'        => esc_html__( 'Hover Animation', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'none'    => 'None',
					'lighten' => 'Lighten',
					'darken'  => 'Darken',
					'slide'   => 'Slide Up',
					'grow'    => 'Grow',
				),
				'default'      => 'darken',
				'condition'    => array( 'color_source' => 'official' ),
				'prefix_class' => 'sifency-share-hover-',
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'      => 'typography',
				'selector'  => '{{WRAPPER}} .sifency-share-button',
				'separator' => 'before',
			)
		);
		$this->add_responsive_control(
			'button_size',
			array(
				'label'     => esc_html__( 'Button Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 0.5,
						'max'  => 2,
						'step' => 0.1,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-share-button' => 'font-size: {{SIZE}}em;' ),
			)
		);
		$this->add_responsive_control(
			'icon_size',
			array(
				'label'      => esc_html__( 'Icon Size', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-share-icon' => 'font-size: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'button_padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-share-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-share-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				'condition'  => array( 'shape' => 'rounded' ),
			)
		);
		$this->end_controls_section();

		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'    => 'share_trigger_',
				'label'     => 'Trigger Button Style',
				'selector'  => '.sifency-share-trigger',
				'condition' => array( 'view' => 'hover-reveal' ),
			)
		);
		$this->start_controls_section(
			'section_dropdown_pos',
			array(
				'label'     => esc_html__( 'Dropdown Position', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'view' => 'hover-reveal' ),
			)
		);
		$this->add_responsive_control(
			'dropdown_offset_x',
			array(
				'label'      => esc_html__( 'Offset X', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -200,
						'max' => 200,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-share-buttons' => 'transform: translate({{SIZE}}{{UNIT}}, {{dropdown_offset_y.SIZE}}{{dropdown_offset_y.UNIT}});',
				),
			)
		);
		$this->add_responsive_control(
			'dropdown_offset_y',
			array(
				'label'      => esc_html__( 'Offset Y', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'default'    => array(
					'size' => 10,
					'unit' => 'px',
				),
				'range'      => array(
					'px' => array(
						'min' => -200,
						'max' => 200,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-share-buttons' => 'transform: translate({{dropdown_offset_x.SIZE}}{{dropdown_offset_x.UNIT}}, {{SIZE}}{{UNIT}});',
				),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		if ( empty( $settings['social_buttons'] ) ) {
			return;
		}
		echo '<div class="sifency-share-wrapper">';
		if ( 'hover-reveal' === $settings['view'] ) {
			?>
			<div class="sifency-share-trigger sifency-button-style">
				<span class="sifency-trigger-icon">
					<?php \Elementor\Icons_Manager::render_icon( $settings['trigger_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				</span>
				<?php if ( ! empty( $settings['trigger_text'] ) ) : ?>
					<span class="sifency-trigger-text"><?php echo esc_html( $settings['trigger_text'] ); ?></span>
				<?php endif; ?>
			</div>
			<?php
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-share-buttons' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		// Current Page Data
		$page_url   = get_permalink();
		$page_title = get_the_title();

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
			foreach ( $settings['social_buttons'] as $index => $item ) :
				$network = $item['social_network'];

				// Logic for URL/Text
				$share_link    = '';
				$url_to_share  = ! empty( $item['custom_url'] ) ? $item['custom_url'] : $page_url;
				$text_to_share = ! empty( $item['custom_text'] ) ? $item['custom_text'] : $page_title;
				$button_label  = ! empty( $item['custom_text'] ) && 'text' === $settings['view'] ? $item['custom_text'] : ucfirst( $network );

				// Build Share URLs
				switch ( $network ) {
					case 'facebook':
						$share_link = 'https://www.facebook.com/sharer/sharer.php?u=' . urlencode( $url_to_share );
						break;
					case 'twitter':
						$share_link = 'https://twitter.com/intent/tweet?url=' . urlencode( $url_to_share ) . '&text=' . urlencode( $text_to_share );
						break;
					case 'linkedin':
						$share_link = 'https://www.linkedin.com/shareArticle?mini=true&url=' . urlencode( $url_to_share ) . '&title=' . urlencode( $text_to_share );
						break;
					case 'pinterest':
						// Needs image usually, simplified here
						$share_link = 'https://pinterest.com/pin/create/button/?url=' . urlencode( $url_to_share ) . '&description=' . urlencode( $text_to_share );
						break;
					case 'whatsapp':
						$share_link = 'https://api.whatsapp.com/send?text=' . urlencode( $text_to_share . ' ' . $url_to_share );
						break;
					case 'reddit':
						$share_link = 'https://www.reddit.com/submit?url=' . urlencode( $url_to_share ) . '&title=' . urlencode( $text_to_share );
						break;
					case 'telegram':
						$share_link = 'https://t.me/share/url?url=' . urlencode( $url_to_share ) . '&text=' . urlencode( $text_to_share );
						break;
					case 'email':
						$share_link = 'mailto:?subject=' . urlencode( $text_to_share ) . '&body=' . urlencode( $url_to_share );
						break;
				}

				$button_key = 'button_' . $item['_id'];

				// Add Classes
				$this->add_render_attribute(
					$button_key,
					'class',
					array(
						'sifency-share-button',
						'sifency-share-network-' . $network,
						'elementor-repeater-item-' . $item['_id'],
					)
				);

				// JS Popup Data Attributes
				$this->add_render_attribute(
					$button_key,
					array(
						'href'    => $share_link,
						'onclick' => "window.open(this.href, 'share-window', 'left=20,top=20,width=600,height=500,toolbar=0,resizable=1'); return false;",
						'role'    => 'button',
						'title'   => 'Share on ' . ucfirst( $network ),
					)
				);

				// Animation
				$animation_item_attrs = $animation_manager->get_item_attributes();
				?>
				
				<a <?php echo wp_kses_post( $this->get_render_attribute_string( $button_key ) ); ?> <?php echo wp_kses_post( $animation_item_attrs ); ?>>
								
					<?php if ( 'text' !== $settings['view'] ) : ?>
						<span class="sifency-share-icon">
							<?php
							if ( ! empty( $item['social_icon']['value'] ) ) {
								\Elementor\Icons_Manager::render_icon( $item['social_icon'], array( 'aria-hidden' => 'true' ) );
							}
							?>
						</span>
					<?php endif; ?>

					<?php if ( 'icon' !== $settings['view'] ) : ?>
						<span class="sifency-share-text">
							<?php echo esc_html( $button_label ); ?>
						</span>
					<?php endif; ?>

				</a>
			<?php endforeach; ?>
		</div>
		</div> <!-- End Wrapper -->
		<?php
	}
}
