<?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\Group_Control_Box_Shadow;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Social extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-social',
			'title'         => 'Sifency Social Icons',
			'icon'          => 'eicon-social-icons',
			'category'      => 'sifency-addons-widgets',
			'keywords'      => array( 'social', 'icons', 'share', 'networks' ),
			'style_depends' => array( 'sifency-frontend-style', 'sifency-component-style', 'sifency-icon' ),
		);}
	protected function register_controls() {
		// --- Content Section ---
		$this->start_controls_section(
			'section_social_icons',
			array( 'label' => esc_html__( 'Social Icons', 'sifency-addons' ) )
		);

		$repeater = new Repeater();
		$repeater->add_control(
			'social_icon',
			array(
				'label'   => esc_html__( 'Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'fab fa-facebook-f',
					'library' => 'fa-brands',
				),
			)
		);

		$repeater->add_control(
			'custom_text',
			array(
				'label'   => esc_html__( 'Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Facebook', 'sifency-addons' ),
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label' => esc_html__( 'Link', 'sifency-addons' ),
				'type'  => Controls_Manager::URL,
			)
		);
		$this->add_control(
			'social_icon_list',
			array(
				'label'       => esc_html__( 'Social Icons', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'social_icon' => array(
							'value'   => 'icon-facebook',
							'library' => 'sifency-icons',
						),
						'custom_text' => 'Facebook',
					),
					array(
						'social_icon' => array(
							'value'   => 'icon-linkedin',
							'library' => 'sifency-icons',
						),
						'custom_text' => 'LinkedIn',
					),
				),
				'title_field' => '<# var icon = elementor.helpers.getSocialNetworkNameFromIcon( social_icon ); #><i class="{{ social_icon.value }}" aria-hidden="true"></i> {{{ custom_text || icon || "Social Icon" }}}',
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'        => esc_html__( 'Layout Type', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'icon',
				'options'      => array(
					'icon'      => esc_html__( 'Only Icon', 'sifency-addons' ),
					'text'      => esc_html__( 'Only Text', 'sifency-addons' ),
					'icon_text' => esc_html__( 'Icon + Text', 'sifency-addons' ),
				),
				'prefix_class' => 'sifency-social-layout-',
			)
		);
		$this->add_control(
			'shape',
			array(
				'label'        => esc_html__( 'Shape', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'circle',
				'options'      => array(
					'square'  => esc_html__( 'Square', 'sifency-addons' ),
					'rounded' => esc_html__( 'Rounded', 'sifency-addons' ),
					'circle'  => esc_html__( 'Circle', 'sifency-addons' ),
				),
				'prefix_class' => 'elementor-shape-',
			)
		);
		$this->add_responsive_control(
			'align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-social-icons' => 'justify-content: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_label_content_controls();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style_social_container',
			array(
				'label' => esc_html__( 'Container', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'gap',
			array(
				'label'     => esc_html__( 'Gap Between Items', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-social-icons' => 'gap: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'item_padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-social-icon' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'container_width',
			array(
				'label'      => esc_html__( 'Size (Width)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 20,
						'max' => 300,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-social-icon' => 'width: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'container_height',
			array(
				'label'      => esc_html__( 'Size (Height)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 20,
						'max' => 300,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-social-icon' => 'height: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'selector'  => '.elementor-widget-container',
				'condition' => array( 'show_label' => 'yes' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_style_colors',
			array(
				'label' => esc_html__( 'Icon & Text Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'social_icon_size',
			array(
				'label'     => esc_html__( 'Icon Font Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-social-icon i'   => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-social-icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
				'condition' => array( 'layout_type!' => 'text' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'      => 'text_typography',
				'selector'  => '{{WRAPPER}} .sifency-share-text',
				'condition' => array( 'layout_type!' => 'icon' ),
			)
		);

		$this->start_controls_tabs( 'tabs_social_style' );

		$this->start_controls_tab( 'tab_social_normal', array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) ) );

		$this->add_control(
			'icon_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-social-icon'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-social-icon i'   => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-social-icon svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-social-icon' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab( 'tab_social_hover', array( 'label' => esc_html__( 'Hover', 'sifency-addons' ) ) );

		$this->add_control(
			'icon_color_hover',
			array(
				'label'     => esc_html__( 'Hover Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-social-icon:hover' => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-social-icon:hover i' => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-social-icon:hover svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'bg_color_hover',
			array(
				'label'     => esc_html__( 'Background Hover Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-social-icon:hover' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'hover_animation',
			array(
				'label' => esc_html__( 'Hover Animation', 'sifency-addons' ),
				'type'  => Controls_Manager::HOVER_ANIMATION,
			)
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			'border_radius',
			array(
				'label'      => esc_html__( 'Custom Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-social-icon' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;',
				),
				'separator'  => 'before',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'border',
				'selector' => '{{WRAPPER}} .sifency-social-icon',
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'icon_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-social-icon',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style_',
				'label'     => esc_html__( 'Label Style', 'sifency-addons' ),
				'selector'  => '.sifency-label-style',
				'exclude'   => array( 'alignment', 'tabs', 'transition' ),
				'condition' => array( 'show_label' => 'yes' ),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( $settings['social_icon_list'] ) ) {
			return;
		}
		$animation_manager     = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes       = array( 'sifency-social-icons' );
		$wrapper_attrs         = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		$hover_animation_class = ! empty( $settings['hover_animation'] ) ? 'elementor-animation-' . $settings['hover_animation'] : '';
		?>
		 <?php if ( 'yes' === $settings['show_label'] && ! empty( $settings['label_text'] ) ) : ?>
			<span class="sifency-label-style">
					<?php echo esc_html( $settings['label_text'] ); ?>
			</span>
		<?php endif; ?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>


			<?php
			foreach ( $settings['social_icon_list'] as $index => $item ) :
				$item_key = $this->get_repeater_setting_key( 'social_icon', 'social_icon_list', $index );
				$this->add_render_attribute(
					$item_key,
					'class',
					array(
						'sifency-social-icon',
						'sifency-icon-style',
						$hover_animation_class,
					)
				);

				if ( ! empty( $item['link']['url'] ) ) {
					$this->add_link_attributes( $item_key, $item['link'] );
				}

				$animation_item_attrs = $animation_manager->get_item_attributes();
				if ( ! empty( $animation_item_attrs ) ) {
					if ( preg_match( '/class="([^"]+)"/', $animation_item_attrs, $class_match ) ) {
						$this->add_render_attribute( $item_key, 'class', $class_match[1] );
					}
					if ( preg_match_all( '/(data-[^=]+)="([^"]+)"/', $animation_item_attrs, $data_matches ) ) {
						foreach ( $data_matches[1] as $i => $attr_name ) {
							$this->add_render_attribute( $item_key, $attr_name, $data_matches[2][ $i ] );
						}
					}
				}
				?>
				<a <?php echo wp_kses_post( $this->get_render_attribute_string( $item_key ) ); ?>>
					
					<?php if ( 'text' !== $settings['layout_type'] ) : ?>
						<span class="sifency-social-icon-inner">
							<?php Icons_Manager::render_icon( $item['social_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</span>
					<?php endif; ?>

					<?php if ( 'icon' !== $settings['layout_type'] && ! empty( $item['custom_text'] ) ) : ?>
						<span class="sifency-share-text text_underline_animation">
							<span><?php echo esc_html( $item['custom_text'] ); ?></span>
						</span>
					<?php endif; ?>

				</a>
			<?php endforeach; ?>
		</div>
		<?php
	}
}
