<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
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_Help_Controls;
class Sifency_Widget_Team_Social_Profiles extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'team';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-team-social-profiles',
			'title'         => 'Member Social Profiles',
			'icon'          => 'eicon-meta-data',
			'category'      => array( 'sifency-team-components' ),
			'style_depends' => array( 'sifency-theme-builder-style', 'sifency-icons' ),
			'keywords'      => array( 'team', 'social', 'icons', 'profile', 'repeater', 'meta', 'dynamic', 'sifency' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_icon_style',
			array(
				'label' => esc_html__( 'Icons', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$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-profiles' => 'justify-content: {{VALUE}};' ),
			)
		);
		$this->add_responsive_control(
			'icon_spacing',
			array(
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-social-profiles' => 'gap: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_icon_style_controls(
			array(
				'selector'        => '.sifency-social-icon',
				'show_dimensions' => true,
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id      = esc_attr( $context['dom_id'] );
		$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 = array(
					array(
						'member_icon'        => 'icon-facebook',
						'member_social_link' => '#',
					),
					array(
						'member_icon'        => 'icon-twitter',
						'member_social_link' => '#',
					),
					array(
						'member_icon'        => 'icon-instagram',
						'member_social_link' => '#',
					),
					array(
						'member_icon'        => 'icon-linkedin',
						'member_social_link' => '#',
					),
				);
			} else {
				return;
			}
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" 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 ), array() ); ?>>
						<i class="<?php echo esc_attr( $profile['member_icon'] ); ?>"></i>
					</a>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}
}
