
<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography; // Although not for icons, good to have for potential tooltips
use Elementor\Group_Control_Border;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Team_Social_Profiles extends Widget_Base {
	use Sifency_Animation_Controls;

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

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

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

	public function get_categories() {
		return [ 'sifency-team-components' ];
	}

    public function get_style_depends() {
	    return [ 'sifency-theme-builder-style', 'elementor-frontend' , 'sifency-icons' ];
    }

	public function get_keywords() {
		return [ 'team', 'social', 'icons', 'profile', 'repeater', 'meta', 'dynamic', 'sifency' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_icon_style',
			[
				'label' => esc_html__( 'Icons', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$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-profiles' => 'justify-content: {{VALUE}};' ],
			]
		);

		$this->add_responsive_control(
			'icon_size',
			[
				'label'      => esc_html__( 'Size', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px' ],
				'range'      => [ 'px' => [ 'min' => 6, 'max' => 100 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-social-icon' => 'font-size: {{SIZE}}{{UNIT}};' ],
			]
		);

		$this->add_responsive_control(
			'icon_padding',
			[
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em' ],
				'selectors'  => [ '{{WRAPPER}} .sifency-social-icon' => 'padding: {{SIZE}}{{UNIT}};' ],
			]
		);

		$this->add_responsive_control(
			'icon_spacing',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px' ],
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 100 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-social-profiles' => 'gap: {{SIZE}}{{UNIT}};' ],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'icon_border',
				'selector' => '{{WRAPPER}} .sifency-social-icon',
			]
		);

		$this->add_responsive_control(
			'icon_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [ '{{WRAPPER}} .sifency-social-icon' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
			]
		);

		$this->start_controls_tabs( 'icon_colors' );
		$this->start_controls_tab(
			'icon_color_normal',
			[
				'label' => esc_html__( 'Normal', 'sifency-addons' ),
			]
		);
		$this->add_control(
			'icon_color',
			[
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-social-icon' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_control(
			'icon_bg_color',
			[
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-social-icon' => 'background-color: {{VALUE}};' ],
			]
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'icon_color_hover',
			[
				'label' => esc_html__( 'Hover', 'sifency-addons' ),
			]
		);
		$this->add_control(
			'icon_hover_color',
			[
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-social-icon:hover' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_control(
			'icon_hover_bg_color',
			[
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-social-icon:hover' => 'background-color: {{VALUE}};' ],
			]
		);
		$this->add_control(
			'icon_hover_border_color',
			[
				'label'     => esc_html__( 'Border Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-social-icon:hover' => 'border-color: {{VALUE}};' ],
			]
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$social_profiles = sifency_get_field( 'member_social_icons' );
		if ( empty( $social_profiles ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$social_profiles = [
					[ 'member_icon' => 'fab fa-facebook-f', 'member_social_link' => '#' ],
					[ 'member_icon' => 'fab fa-twitter', 'member_social_link' => '#' ],
					[ 'member_icon' => 'fab fa-instagram', 'member_social_link' => '#' ],
					[ 'member_icon' => 'fab fa-linkedin-in', 'member_social_link' => '#' ],
				];
			} else {
				return; // Render nothing on live site if empty
			}
		}

		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );

		?>
		<div class="sifency-social-profiles-wrapper" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<div class="sifency-social-profiles">
				<?php
				foreach ( $social_profiles as $profile ) :
					if ( empty( $profile['member_icon'] ) || empty( $profile['member_social_link'] ) ) {
						continue;
					}
					$link_key = 'link_' . esc_attr( $profile['member_icon'] );
					$this->add_render_attribute( $link_key, 'class', 'sifency-social-icon' );
					$this->add_render_attribute( $link_key, 'href', esc_url( $profile['member_social_link'] ) );
					$this->add_render_attribute( $link_key, 'target', '_blank' );
					$this->add_render_attribute( $link_key, 'rel', 'noopener noreferrer' );
				?>
					<a <?php echo wp_kses ( $this->get_render_attribute_string( $link_key ), [] ); ?>>
						<i class="<?php echo esc_attr( $profile['member_icon'] ); ?>"></i>
					</a>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}
}