<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Interactive_Links extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Section_Header;
	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-interactive-links',
			'title'          => 'Sifency Interactive Links',
			'icon'           => 'eicon-link',
			'category'       => array( 'sifency-creative-widgets' ),
			'keywords'       => array( 'interactive', 'links', 'sifency' ),
			'script_depends' => array( 'sifency-interactive-link-script' ),
			'style_depends'  => array( 'sifency-interactive-link-style', 'sifency-typo-style' ),
		);}
	protected function register_controls() {

		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Links & Layout', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Mode', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'split',
				'options' => array(
					'center'     => esc_html__( 'Layout 1: Center List (Side Images)', 'sifency-addons' ),
					'background' => esc_html__( 'Layout 2: Full Background Change', 'sifency-addons' ),
					'split'      => esc_html__( 'Layout 3: Split Screen (50/50)', 'sifency-addons' ),
				),
			)
		);

		// --- REPEATER ---
		$repeater = new Repeater();
		$repeater->add_control(
			'title',
			array(
				'label'   => esc_html__( 'Title', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Interactive Link',
			)
		);
		$repeater->add_control(
			'subtitle',
			array(
				'label'   => esc_html__( 'Subtitle / Meta', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => '01',
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label' => esc_html__( 'Link URL', 'sifency-addons' ),
				'type'  => Controls_Manager::URL,
			)
		);
		$repeater->add_control(
			'image',
			array(
				'label' => esc_html__( 'Reveal Image', 'sifency-addons' ),
				'type'  => Controls_Manager::MEDIA,
			)
		);
		$repeater->add_control(
			'img_position',
			array(
				'label'   => esc_html__( 'Image Position (Layout 1 Only)', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'left'  => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default' => 'right',
			)
		);
		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'List Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'title'        => 'Branding Design',
						'subtitle'     => 'Design',
						'img_position' => 'left',
					),
					array(
						'title'        => 'Web Development',
						'subtitle'     => 'Coding',
						'img_position' => 'right',
					),
					array(
						'title'        => 'Marketing',
						'subtitle'     => 'SEO',
						'img_position' => 'left',
					),
				),
				'title_field' => '{{{ title }}}',
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'      => 'thumbnail',
				'default'   => 'large',
				'separator' => 'before',
			)
		);
		$this->end_controls_section();

		// --- SETTINGS ---
		$this->start_controls_section(
			'section_settings',
			array(
				'label' => esc_html__( 'Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'split_direction',
			array(
				'label'     => esc_html__( 'Split Direction (Layout 3)', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'row',
				'options'   => array(
					'row'         => esc_html__( 'Image Right / Text Left', 'sifency-addons' ),
					'row-reverse' => esc_html__( 'Image Left / Text Right', 'sifency-addons' ),
				),
				'condition' => array( 'layout_type' => 'split' ),
			)
		);
		$this->add_control(
			'item_height',
			array(
				'label'     => esc_html__( 'Min Height (px)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 200,
						'max' => 800,
					),
				),
				'default'   => array( 'size' => 400 ),
				'selectors' => array( '{{WRAPPER}} .sifency-il-wrapper' => 'min-height: {{SIZE}}px;' ),
				'condition' => array( 'layout_type' => array( 'background', 'center' ) ),
			)
		);
		$this->add_control(
			'show_icon',
			array(
				'label'        => esc_html__( 'Show Icon', 'sifency-addons' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);
		$this->add_control(
			'item_icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'eicon-arrow-right',
					'library' => 'elementor',
				),
				'condition' => array(
					'show_icon' => 'yes',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'    => 'list_',
				'label'     => esc_html__( 'List Items Flexbox', 'sifency-addons' ),
				'selector'  => '.sifency-il-list',
				'condition' => array( 'layout_type' => 'center' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- STYLE: TEXT ---
		$this->start_controls_section(
			'style_text',
			array(
				'label' => esc_html__( 'List Items', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'item_padding',
			array(
				'label'      => 'Padding',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-il-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_control(
			'item_border_color',
			array(
				'label'     => 'Divider Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-il-item' => 'border-bottom-color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'show_title_underline',
			array(
				'label'        => esc_html__( 'Title Underline Animation', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'On', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Off', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'separator'    => 'before',
			)
		);

		$this->add_responsive_control(
			'underline_max_width',
			array(
				'label'      => esc_html__( 'Underline Max Width (%)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 100,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-il-title span::after' => 'max-width: {{SIZE}}%;' ),
				'condition'  => array( 'show_title_underline' => 'yes' ),
			)
		);
		$this->add_responsive_control(
			'content_width',
			array(
				'label'      => esc_html__( 'Content Width', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-il-layout-background .sifency-il-list' => 'width: {{SIZE}}%;' ),
				'condition'  => array( 'layout_type' => 'background' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Heading',
				'selector' => '.sifency-heading-style',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Subtitle',
				'selector' => '.sifency-content-style',
				'exclude'  => array( 'tabs', 'border', 'bg', 'shadow', 'stroke', 'radius', 'transition' ),
			)
		);

		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'       => 'abs_image_style',
				'label'        => 'Image',
				'selector'     => '.sifency-il-abs-img.active',
				'show_overlay' => false,
				'condition'    => array( 'layout_type' => 'center' ),
				'exclude'      => array( 'margin', 'tabs', 'transition' ),
				'selectors'    => array(
					'image'     => '.sifency-il-abs-img.active',
					'container' => '.sifency-il-abs-img.active',
					'dimension' => '.sifency-il-abs-img.active',
				),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'       => 'split_image_style',
				'label'        => 'Image Style',
				'selector'     => 'img.sifency-il-split-target.sifency-image',
				'selectors'    => array(
					'image'     => 'img.sifency-il-split-target.sifency-image',
					'container' => 'img.sifency-il-split-target.sifency-image',
					'dimension' => 'img.sifency-il-split-target.sifency-image',
				),
				'show_overlay' => false,
				'exclude'      => array( 'margin', 'opacity', 'tabs', 'transition' ),
				'condition'    => array( 'layout_type' => 'split' ),
			)
		);
		$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();

		if ( empty( $settings['items'] ) || ! is_array( $settings['items'] ) ) {
			return;
		}

		$this->add_render_attribute(
			'wrapper',
			'class',
			array(
				'sifency-il-wrapper',
				'sifency-il-layout-' . esc_attr( $settings['layout_type'] ),
			)
		);

		$extra_wrapper_classes = array(
			'sifency-il-wrapper',
			'sifency-il-layout-' . esc_attr( $settings['layout_type'] ),
		);

		$anim_manager = new \Sifency\Traits\Sifency_Addons_Animation_Manager( $settings );

		if ( 'split' === $settings['layout_type'] && ! empty( $settings['split_direction'] ) ) {
			$this->add_render_attribute(
				'wrapper',
				'style',
				'flex-direction:' . esc_attr( $settings['split_direction'] )
			);
		}
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" 
		<?php
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor widget rendering attributes
		echo wp_kses_post( $anim_manager->get_wrapper_attributes( $extra_wrapper_classes ) );
		?>
		>

			<?php
			if ( 'background' === $settings['layout_type'] ) :
				?>
				<div class="sifency-il-bg-container">
					<div class="sifency-il-bg-overlay"></div>

					<?php
					foreach ( $settings['items'] as $index => $item ) :

						$img_src = '';

						if ( ! empty( $item['image']['id'] ) ) {
							$img_src = wp_get_attachment_image_url( $item['image']['id'], 'full' );
						} elseif ( ! empty( $item['image']['url'] ) ) {
							$img_src = $item['image']['url'];
						}

						if ( empty( $img_src ) ) {
							continue;
						}
						?>
						<div
							class="sifency-il-bg-item <?php echo ( 0 === $index ) ? 'active' : ''; ?>"
							data-index="<?php echo esc_attr( $index ); ?>"
							style="background-image:url(<?php echo esc_url( $img_src ); ?>);">
						</div>
					<?php endforeach; ?>
				</div>
			<?php endif; ?>

			<?php
			if ( 'center' === $settings['layout_type'] ) :
				?>
				<div class="sifency-il-side-img left">
					<?php
					foreach ( $settings['items'] as $index => $item ) :

						if ( 'left' !== $item['img_position'] ) {
							continue;
						}

						$img_src = '';

						if ( ! empty( $item['image']['id'] ) ) {
							$img_src = wp_get_attachment_image_url( $item['image']['id'], 'large' );
						} elseif ( ! empty( $item['image']['url'] ) ) {
							$img_src = $item['image']['url'];
						}

						if ( empty( $img_src ) ) {
							continue;
						}
						?>
						<img
							src="<?php echo esc_url( $img_src ); ?>"
							class="sifency-il-abs-img <?php echo ( 0 === $index ) ? 'active' : ''; ?>"
							data-index="<?php echo esc_attr( $index ); ?>"
							alt="<?php echo esc_attr( $item['title'] ?: 'Image' ); ?>"
							loading="lazy">

					<?php endforeach; ?>
				</div>
			<?php endif; ?>

			<div class="sifency-il-list" role="list">
				<?php
				foreach ( $settings['items'] as $index => $item ) :

					$tag          = ! empty( $item['link']['url'] ) ? 'a' : 'div';
					$item_classes = array( 'sifency-il-item' );
					if ( 0 === $index ) {
						$item_classes[] = 'active';
					}

					$split_image_attr = '';
					if ( 'split' === $settings['layout_type'] ) {
						$img_url = '';
						if ( ! empty( $item['image']['id'] ) ) {
							$img_url = wp_get_attachment_image_url( $item['image']['id'], 'large' );
						} elseif ( ! empty( $item['image']['url'] ) ) {
							$img_url = $item['image']['url'];
						}
						if ( $img_url ) {
							$split_image_attr = ' data-image="' . esc_url( $img_url ) . '"';
						}
					}

					$link_attrs = '';
					if ( ! empty( $item['link']['url'] ) ) {
						$this->add_link_attributes( 'link_' . $index, $item['link'] );
						$link_attrs = $this->get_render_attribute_string( 'link_' . $index );
					}
					?>
					<<?php echo tag_escape( $tag ); ?>
					<?php
                    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor rendering attributes
					echo wp_kses_post( $anim_manager->get_item_attributes( $item_classes ) );
					?>
					data-index="<?php echo esc_attr( $index ); ?>" role="listitem" 
					<?php
                    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped slide split image attributes
					echo $split_image_attr;
					?>
					<?php
                    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped link attributes
					echo wp_kses_post( $link_attrs );
					?>
>
						<div class="sifency-il-text-wrap">
							<?php
								$underline_class = ( 'yes' === $settings['show_title_underline'] ) ? 'text_underline_animation' : '';
							?>
							<h3 class="sifency-il-title sifency-heading-style <?php echo esc_attr( $underline_class ); ?>">
								<span><?php echo esc_html( $item['title'] ); ?></span>
							</h3>

							<?php if ( ! empty( $item['subtitle'] ) ) : ?>
								<span class="sifency-il-sub sifency-content-style">
									<?php echo esc_html( $item['subtitle'] ); ?>
								</span>
							<?php endif; ?>
						</div>

						<?php if ( 'yes' === $settings['show_icon'] && ! empty( $settings['item_icon']['value'] ) ) : ?>
							<div class="sifency-il-icon">
								<?php
								\Elementor\Icons_Manager::render_icon(
									$settings['item_icon'],
									array(
										'aria-hidden' => 'true',
									)
								);
								?>
							</div>
						<?php endif; ?>

					</<?php echo tag_escape( $tag ); ?>>
				<?php endforeach; ?>
			</div>

			<?php

			if ( 'center' === $settings['layout_type'] ) :
				?>
				<div class="sifency-il-side-img right">
					<?php
					foreach ( $settings['items'] as $index => $item ) :

						if ( 'right' !== $item['img_position'] ) {
							continue;
						}

						$img_src = '';

						if ( ! empty( $item['image']['id'] ) ) {
							$img_src = wp_get_attachment_image_url( $item['image']['id'], 'large' );
						} elseif ( ! empty( $item['image']['url'] ) ) {
							$img_src = $item['image']['url'];
						}

						if ( empty( $img_src ) ) {
							continue;
						}
						?>
						<img
							src="<?php echo esc_url( $img_src ); ?>"
							class="sifency-il-abs-img sifency-image <?php echo ( 0 === $index ) ? 'active' : ''; ?>"
							data-index="<?php echo esc_attr( $index ); ?>"
							alt="<?php echo esc_attr( $item['title'] ?: 'Image' ); ?>"
							loading="lazy">

					<?php endforeach; ?>
				</div>
			<?php endif; ?>

			<?php
			if ( 'split' === $settings['layout_type'] ) :
				?>
				<div class="sifency-il-split-img-wrap sifency-image-wrapper">
					<img
						src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="
						class="sifency-il-split-target sifency-image"
						alt="<?php echo esc_attr( $item['title'] ?: 'Image' ); ?>">
						<div class="sifency-image-overlay"></div>

				</div>
			<?php endif; ?>

		</div>
		<?php
	}

}
