<?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_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Text_Cycle extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Custom_CSS_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-text-cycle',
			'title'          => 'Sifency Text Cycle',
			'icon'           => 'eicon-text-area',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'gsap', 'sifency-text-cycle-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'text', 'cycle', 'animated headline', 'blur' ),
		);
	}

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

		$this->add_control(
			'prefix_text',
			array(
				'label'   => esc_html__( 'Prefix Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Lets', 'sifency-addons' ),
			)
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'word',
			array(
				'label'   => esc_html__( 'Animated Word', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'design', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'cycle_words',
			array(
				'label'       => esc_html__( 'Cycle Words', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'word' => 'design' ),
					array( 'word' => 'build' ),
					array( 'word' => 'create' ),
				),
				'title_field' => '{{{ word }}}',
			)
		);

		$this->add_control(
			'suffix_text',
			array(
				'label'   => esc_html__( 'Suffix Text (Optional)', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			)
		);

		$this->end_controls_section();

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

		$this->add_control(
			'pause_duration',
			array(
				'label'   => esc_html__( 'Pause Between Words (s)', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 0.5,
						'max'  => 5,
						'step' => 0.1,
					),
				),
				'default' => array( 'size' => 2 ),
			)
		);

		$this->add_control(
			'animation_speed',
			array(
				'label'   => esc_html__( 'Transition Speed (s)', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 0.2,
						'max'  => 2,
						'step' => 0.1,
					),
				),
				'default' => array( 'size' => 0.8 ),
			)
		);

		$this->add_control(
			'enable_blur',
			array(
				'label'       => esc_html__( 'Blur Effect', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => 'Words will blur while transitioning up/down.',
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'static_text_',
				'label'           => 'Static Text',
				'selector'        => '.sifency-cycle-static',
				'color_only'      => true,
				'show_blend_mode' => true,
				'exclude'         => array( 'tabs', 'transition', 'shadow', 'stroke', 'border', 'radius', 'bg', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'cycle_word_text_',
				'label'           => 'Cycle Text',
				'selector'        => '.sifency-cycle-word',
				'color_only'      => true,
				'show_blend_mode' => true,
				'exclude'         => array( 'tabs', 'transition', 'shadow', 'stroke', 'radius', 'bg', 'alignment' ),
			)
		);
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Alignment', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'center',
				'selectors' => array( '{{WRAPPER}} .sifency-cycle-container' => 'justify-content: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Custom_CSS_Controls();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	protected function render_dom( array $context ) {

		$settings   = $this->get_settings_for_display();
		$wrapper_id = ! empty( $context['dom_id'] ) ? $context['dom_id'] : '';
		$words      = $settings['cycle_words'];

		$this->sifency_addons_render_custom_css();

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

		$config = array(
			'pause' => $settings['pause_duration']['size'] ?? 2,
			'speed' => $settings['animation_speed']['size'] ?? 0.8,
			'blur'  => $settings['enable_blur'] === 'yes' ? 'true' : 'false',
		);

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_classes = array( 'sifency-cycle-wrapper' );

		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		if ( ! empty( $wrapper_id ) ) {
			$wrapper_attrs .= ' id="' . esc_attr( $wrapper_id ) . '"';
		}
		?>

		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>
			data-config="<?php echo esc_attr( wp_json_encode( $config ) ); ?>">

			<h2 class="sifency-cycle-container">

				<!-- Prefix -->
				<?php if ( ! empty( $settings['prefix_text'] ) ) : ?>
					<span class="sifency-cycle-static sifency-prefix">
						<?php echo esc_html( $settings['prefix_text'] ); ?>
					</span>
				<?php endif; ?>

				<!-- Animated Words -->
				<span class="sifency-cycle-words-wrap">
					<?php foreach ( $words as $index => $item ) : ?>
						<span class="sifency-cycle-word"
							data-index="<?php echo esc_attr( $index ); ?>">
							<?php echo esc_html( $item['word'] ); ?>
						</span>
					<?php endforeach; ?>
				</span>

				<!-- Suffix -->
				<?php if ( ! empty( $settings['suffix_text'] ) ) : ?>
					<span class="sifency-cycle-static sifency-suffix">
						<?php echo esc_html( $settings['suffix_text'] ); ?>
					</span>
				<?php endif; ?>

			</h2>

		</div>

		<?php
	}
}
