<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
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_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Addons_Widget_Textual_Showcase extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-textual-showcase',
			'title'          => 'Sifency Textual Showcase',
			'icon'           => 'eicon-text-area',
			'category'       => 'sifency-business-widgets',
			'keywords'       => array( 'showcase', 'text', 'content', 'media', 'image' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-content-showcase-style' ),
			'script_depends' => array( 'sifency-content-showcase-script' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Showcase Content', 'sifency-addons' ),
			)
		);
		$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',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-ts-container' => 'text-align: {{VALUE}};',
				),
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'type',
			array(
				'label'   => esc_html__( 'Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'text',
				'options' => array(
					'text'  => esc_html__( 'Plain Text', 'sifency-addons' ),
					'media' => esc_html__( 'Project / Image', 'sifency-addons' ),
					'break' => esc_html__( 'Line Break', 'sifency-addons' ),
				),
			)
		);
		$repeater->add_control(
			'text_content',
			array(
				'label'     => esc_html__( 'Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => 'Project Name',
				'condition' => array( 'type' => array( 'text', 'media' ) ),
			)
		);
		$repeater->add_control(
			'image_normal',
			array(
				'label'     => esc_html__( 'Inline Image (Normal)', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'condition' => array( 'type' => 'media' ),
			)
		);
		$repeater->add_control(
			'image_hover',
			array(
				'label'       => esc_html__( 'Inline Image (Hover)', 'sifency-addons' ),
				'type'        => Controls_Manager::MEDIA,
				'condition'   => array( 'type' => 'media' ),
				'description' => esc_html__( 'Replaces the normal image when you hover over this item.', 'sifency-addons' ),
			)
		);
		$repeater->add_control(
			'image_cursor',
			array(
				'label'       => esc_html__( 'Cursor Image (Floating)', 'sifency-addons' ),
				'type'        => Controls_Manager::MEDIA,
				'condition'   => array( 'type' => 'media' ),
				'description' => esc_html__( 'Appears floating near the mouse cursor on hover.', 'sifency-addons' ),
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label'     => esc_html__( 'Link', 'sifency-addons' ),
				'type'      => Controls_Manager::URL,
				'condition' => array( 'type' => 'media' ),
			)
		);

		$repeater->add_control(
			'text_color_override',
			array(
				'label'     => esc_html__( 'Text Color Override', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} {{CURRENT_ITEM}}' => 'color: {{VALUE}}' ),
				'condition' => array( 'type' => array( 'text', 'media' ) ),
			)
		);
		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'Sentence Builder', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'type'         => 'text',
						'text_content' => 'See our',
					),
					array(
						'type'         => 'media',
						'text_content' => 'amazing work',
					),
					array(
						'type'         => 'text',
						'text_content' => 'here.',
					),
				),
				'title_field' => '{{{ type }}}: {{{ text_content }}}',
			)
		);

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

		// --- CURSOR SETTINGS ---
		$this->start_controls_section(
			'section_cursor',
			array(
				'label' => esc_html__( 'Cursor Controller', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'cursor_size',
			array(
				'label'     => esc_html__( 'Cursor Image Size (px)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 100,
						'max' => 600,
					),
				),
				'default'   => array( 'size' => 300 ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-ts-cursor-img' => 'width: {{SIZE}}px; height: auto;',
				),
			)
		);
		$this->add_control(
			'cursor_speed',
			array(
				'label'       => esc_html__( 'Follow Speed (s)', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.05,
					),
				),
				'default'     => array( 'size' => 0.1 ),
				'description' => 'Lower is faster (0.1 is standard).',
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'heading_style',
				'label'           => 'Typography',
				'selector'        => '.sifency-ts-item',
				'show_blend_mode' => true,
				'exclude'         => array( 'alignment', 'max_width', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'media_text_style',
				'label'           => 'Media Item Text',
				'selector'        => '.sifency-ts-media-item',
				'show_blend_mode' => true,
				'exclude'         => array( 'alignment', 'max_width', 'transition' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'       => 'image_style',
				'label'        => 'Image',
				'selector'     => '.sifency-ts-inline-img-wrap img',
				'selectors'    => array(
					'image'     => '.sifency-ts-inline-img-wrap img',
					'container' => '.sifency-ts-inline-img-wrap img',
					'dimension' => '.sifency-ts-inline-img-wrap img',
				),
				'show_overlay' => false,
				'exclude'      => array( 'transition', 'opacity', 'filters' ),
			)
		);

		$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();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_attrs = $animation_manager->get_wrapper_attributes_array( array( 'sifency-textual-showcase-wrapper' ) );
		$this->add_render_attribute( 'wrapper', $wrapper_attrs );
		$this->add_render_attribute( 'container', 'class', 'sifency-ts-container' );

		$js_config = array( 'speed' => $settings['cursor_speed']['size'] ?? 0.1 );
		$this->add_render_attribute( 'wrapper', 'data-ts-settings', wp_json_encode( $js_config ) );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			
			<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'container' ) ); ?>>
				<?php
				foreach ( $settings['items'] as $index => $item ) :
					$item_key = 'item_' . $index;

					// Build array of classes for this item
					$item_classes = array( 'sifency-ts-item' );
					if ( 'media' === $item['type'] ) {
						$item_classes[] = 'sifency-ts-media-item';
					}

					// Get animation item attributes merged with these classes
					$animation_item_attrs = $animation_manager->get_item_attributes( $item_classes );

					$img_normal_src = '';
					if ( ! empty( $item['image_normal']['id'] ) ) {
						$img_normal_src = Group_Control_Image_Size::get_attachment_image_src( $item['image_normal']['id'], 'thumbnail', $settings );
					} elseif ( ! empty( $item['image_normal']['url'] ) ) {
						$img_normal_src = $item['image_normal']['url'];
					}

					$img_hover_src = '';
					if ( ! empty( $item['image_hover']['id'] ) ) {
						$img_hover_src = Group_Control_Image_Size::get_attachment_image_src( $item['image_hover']['id'], 'thumbnail', $settings );
					} elseif ( ! empty( $item['image_hover']['url'] ) ) {
						$img_hover_src = $item['image_hover']['url'];
					}

					$img_cursor_src = '';
					if ( ! empty( $item['image_cursor']['id'] ) ) {
						$img_cursor_src = wp_get_attachment_image_url( $item['image_cursor']['id'], 'large' );
					} elseif ( ! empty( $item['image_cursor']['url'] ) ) {
						$img_cursor_src = $item['image_cursor']['url'];
					}
					?>
					
					<?php if ( 'break' === $item['type'] ) : ?>
						<div class="sifency-ts-break"></div>
					
					<?php elseif ( 'text' === $item['type'] ) : ?>
						<span <?php echo wp_kses_post( $animation_item_attrs ); ?>>
							<?php echo esc_html( $item['text_content'] ); ?>
						</span>

						<?php
					elseif ( 'media' === $item['type'] ) :
						if ( ! empty( $item['link']['url'] ) ) {
							$this->add_link_attributes( $item_key, $item['link'] );
						}

						if ( ! empty( $img_cursor_src ) ) {
							$this->add_render_attribute( $item_key, 'data-cursor-src', esc_url( $img_cursor_src ) );
						}
						?>
						<?php $tag = ! empty( $item['link']['url'] ) ? 'a' : 'span'; ?>
						<<?php echo wp_kses_post( $tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( $item_key ) ); ?> <?php echo wp_kses_post( $animation_item_attrs ); ?>>
							
							<?php echo esc_html( $item['text_content'] ); ?>

							<?php if ( ! empty( $img_normal_src ) ) : ?>
								<span class="sifency-ts-inline-img-wrap">
									<img src="<?php echo esc_url( $img_normal_src ); ?>" class="sifency-ts-img-normal" alt="project">
									
									<?php if ( ! empty( $img_hover_src ) ) : ?>
										<img src="<?php echo esc_url( $img_hover_src ); ?>" class="sifency-ts-img-hover" alt="project hover">
									<?php endif; ?>
								</span>
							<?php endif; ?>

						</<?php echo wp_kses_post( $tag ); ?>>

					<?php endif; ?>

				<?php endforeach; ?>
			</div>

			<div class="sifency-ts-cursor-follower">
				<img src="" class="sifency-ts-cursor-img" alt="">
			</div>

		</div>
		<?php
	}
}
