<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;

class Sifency_Rolling_Text extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-rolling-text',
			'title'          => 'Sifency Rolling Text (Hover)',
			'icon'           => 'eicon-animation-text',
			'category'       => array( 'sifency-typography-widgets' ),
			'script_depends' => array( 'gsap', 'sifency-rolling-text-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'rolling', 'text', 'hover', 'stagger', 'icon' ),
		);
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Rolling Texts', 'sifency-addons' ) )
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'button_text',
			array(
				'label'   => esc_html__( 'Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => esc_html__( 'START YOUR STORY', 'sifency-addons' ),
			)
		);

		$repeater->add_control(
			'button_link',
			array(
				'label'   => esc_html__( 'Link', 'sifency-addons' ),
				'type'    => Controls_Manager::URL,
				'default' => array( 'url' => '#' ),
			)
		);

		$repeater->add_control(
			'item_icon',
			array(
				'label' => esc_html__( 'Item Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);

		$this->add_control(
			'rolling_items',
			array(
				'label'       => esc_html__( 'Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'button_text' => 'START YOUR STORY',
						'button_link' => array( 'url' => '#' ),
					),
				),
				'title_field' => '{{{ button_text }}}',
			)
		);

		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'content_',
				'label'    => esc_html__( 'Items Flexbox', 'sifency-addons' ),
				'selector' => '.sifency-rolling-wrapper',
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'icon_',
				'label'    => esc_html__( 'Text & Icon', 'sifency-addons' ),
				'selector' => '.sifency-roll-link',
			)
		);
		$this->end_controls_section();

		// --- Icon Section (New) ---
		$this->start_controls_section(
			'section_icon_settings',
			array(
				'label' => esc_html__( 'Icon Settings', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'enable_icons',
			array(
				'label'        => esc_html__( 'Enable Icons', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'no',
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
			)
		);

		$this->add_control(
			'icon_type',
			array(
				'label'     => esc_html__( 'Icon Type', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'repeated' => esc_html__( 'Individual Item Icon', 'sifency-addons' ),
					'global'   => esc_html__( 'Global Icon for all', 'sifency-addons' ),
				),
				'default'   => 'repeated',
				'condition' => array( 'enable_icons' => 'yes' ),
			)
		);

		$this->add_control(
			'global_icon',
			array(
				'label'     => esc_html__( 'Global Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'condition' => array(
					'enable_icons' => 'yes',
					'icon_type'    => 'global',
				),
			)
		);

		$this->add_control(
			'icon_position',
			array(
				'label'     => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'before' => esc_html__( 'Before Text', 'sifency-addons' ),
					'after'  => esc_html__( 'After Text', 'sifency-addons' ),
				),
				'default'   => 'after',
				'condition' => array( 'enable_icons' => 'yes' ),
			)
		);

		$this->add_responsive_control(
			'icon_spacing',
			array(
				'label'      => esc_html__( 'Icon Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
					'em' => array(
						'min' => 0,
						'max' => 3,
					),
				),
				'default'    => array(
					'size' => 10,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-roll-link.icon-after .sifency-roll-icon' => 'margin-left: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-roll-link.icon-before .sifency-roll-icon' => 'margin-right: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array( 'enable_icons' => 'yes' ),
			)
		);

		$this->end_controls_section();

		// --- Settings Section ---
		$this->start_controls_section(
			'section_animation_settings',
			array( 'label' => esc_html__( 'Animation Settings', 'sifency-addons' ) )
		);

		$this->add_control(
			'stagger_speed',
			array(
				'label'       => esc_html__( 'Stagger Delay (s)', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 0.01,
						'max'  => 0.1,
						'step' => 0.01,
					),
				),
				'default'     => array( 'size' => 0.02 ),
				'description' => 'Delay between each letter rolling up.',
			)
		);

		$this->add_control(
			'anim_duration',
			array(
				'label'       => esc_html__( 'Animation Duration (s)', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 0.2,
						'max'  => 1,
						'step' => 0.1,
					),
				),
				'default'     => array( 'size' => 0.4 ),
				'description' => 'How fast the actual roll happens.',
			)
		);
		$this->add_control(
			'show_underline',
			array(
				'label'   => esc_html__( 'Show Underline', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_master_typography_controls(
			array(
				'selector'       => '.sifency-roll-container',
				'prefix'         => 'text_',
				'hover_selector' => '.sifency-roll-link:hover .sifency-roll-container',
			)
		);
		$this->add_sifency_addons_label_content_controls();
		$this->add_sifency_addons_help_controls();
		// --- Icon Style Section ---
		$this->add_sifency_addons_icon_style_controls(
			array(
				'selector'       => '.sifency-roll-icon',
				'hover_selector' => '.sifency-roll-link:hover .sifency-roll-icon',
			)
		);

		// --- Underline Style Section ---
		$this->start_controls_section(
			'section_underline_style',
			array(
				'label'     => esc_html__( 'Underline Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'show_underline' => 'yes',
				),
			)
		);

		$this->add_responsive_control(
			'line_thickness',
			array(
				'label'      => esc_html__( 'Thickness (Height)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min'  => 1,
						'max'  => 10,
						'step' => 1,
					),
				),
				'default'    => array(
					'size' => 1,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-roll-line' => 'height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'line_gap',
			array(
				'label'      => esc_html__( 'Gap from Text', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min'  => 0,
						'max'  => 30,
						'step' => 1,
					),
				),
				'default'    => array(
					'size' => 4,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-roll-line' => 'margin-top: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->start_controls_tabs( 'tabs_line_color_style' );

		// Normal State Tab
		$this->start_controls_tab( 'tab_line_normal', array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) ) );

		$this->add_control(
			'line_color_normal',
			array(
				'label'       => esc_html__( 'Line Color', 'sifency-addons' ),
				'type'        => Controls_Manager::COLOR,
				'description' => 'Leave blank to use the text color (currentColor).',
				'selectors'   => array(
					'{{WRAPPER}} .sifency-roll-line' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'line_opacity_normal',
			array(
				'label'     => esc_html__( 'Opacity', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.1,
					),
				),
				'default'   => array( 'size' => 0.3 ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-roll-line' => 'opacity: {{SIZE}};',
				),
			)
		);

		$this->end_controls_tab();

		// Hover State Tab
		$this->start_controls_tab( 'tab_line_hover', array( 'label' => esc_html__( 'Hover', 'sifency-addons' ) ) );

		$this->add_control(
			'line_color_hover',
			array(
				'label'     => esc_html__( 'Hover Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-roll-link:hover .sifency-roll-line' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'line_opacity_hover',
			array(
				'label'     => esc_html__( 'Hover Opacity', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.1,
					),
				),
				'default'   => array( 'size' => 1 ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-roll-link:hover .sifency-roll-line' => 'opacity: {{SIZE}};',
				),
			)
		);

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

		// Line Transition Speed
		$this->add_control(
			'line_transition',
			array(
				'label'     => esc_html__( 'Transition Speed (s)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 0.1,
						'max'  => 2,
						'step' => 0.1,
					),
				),
				'default'   => array( 'size' => 0.4 ),
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .sifency-roll-line' => 'transition: opacity {{SIZE}}s ease, background-color {{SIZE}}s ease;',
				),
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'label_',
				'label'          => esc_html__( 'Label Style', 'sifency-addons' ),
				'selector'       => '.sifency-label-style',
				'hover_selector' => '.sifency-roll-link:hover .sifency-label-style',
				'exclude'        => array( 'alignment', 'tabs', 'transition' ),
				'condition'      => array( 'show_label' => 'yes' ),
			)
		);
	}

	protected function render_dom( array $context ) {
		$settings   = $this->get_settings_for_display();
		$wrapper_id = esc_attr( $context['dom_id'] );
		$items      = $settings['rolling_items'];

		if ( empty( $items ) ) {
			return;
		}

		$config = array(
			'stagger'  => $settings['stagger_speed']['size'] ?? 0.02,
			'duration' => $settings['anim_duration']['size'] ?? 0.4,
		);

		// Icon settings for global use
		$enable_icons  = $settings['enable_icons'] === 'yes';
		$icon_type     = $settings['icon_type'] ?? 'repeated';
		$global_icon   = $settings['global_icon'] ?? array();
		$icon_position = $settings['icon_position'] ?? 'after';
		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 id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-rolling-wrapper" data-config='<?php echo esc_attr( wp_json_encode( $config ) ); ?>'>

			<?php
			foreach ( $items as $item ) :
				$this->add_render_attribute( 'link_' . $item['_id'], 'class', 'sifency-roll-link elementor-repeater-item-' . $item['_id'] );
				$this->add_render_attribute( 'link_' . $item['_id'], 'class', 'icon-' . $icon_position ); // Add icon position class

				if ( ! empty( $item['button_link']['url'] ) ) {
					$this->add_render_attribute( 'link_' . $item['_id'], 'href', esc_url( $item['button_link']['url'] ) );
				}
				?>
				<a <?php echo wp_kses_post( $this->get_render_attribute_string( 'link_' . $item['_id'] ) ); ?>>
					<?php
					if ( $enable_icons && $icon_position === 'before' ) :
						$icon_to_display = ( $icon_type === 'global' ) ? $global_icon : ( $item['item_icon'] ?? array() );
						if ( ! empty( $icon_to_display['value'] ) ) :
							?>
							<span class="sifency-roll-icon">
								<?php \Elementor\Icons_Manager::render_icon( $icon_to_display, array( 'aria-hidden' => 'true' ) ); ?>
							</span>
							<?php
						endif;
					endif;
					?>

					<div class="sifency-roll-container" data-text="<?php echo esc_attr( $item['button_text'] ); ?>"></div>

					<?php
					if ( $enable_icons && $icon_position === 'after' ) :
						$icon_to_display = ( $icon_type === 'global' ) ? $global_icon : ( $item['item_icon'] ?? array() );
						if ( ! empty( $icon_to_display['value'] ) ) :
							?>
							<span class="sifency-roll-icon">
								<?php \Elementor\Icons_Manager::render_icon( $icon_to_display, array( 'aria-hidden' => 'true' ) ); ?>
							</span>
							<?php
						endif;
					endif;
					?>

					<?php if ( $settings['show_underline'] === 'yes' ) : ?>
						<div class="sifency-roll-line"></div>
					<?php endif; ?>
				</a>
			<?php endforeach; ?>

		</div>
		<?php
	}
}
