<?php
if ( ! class_exists( '\Elementor\Plugin' ) ) {
	return;
}

use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use \Elementor\Group_Control_Image_Size;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Css_Filter;
use Sifency\Traits\Sifency_Addons_Marquee_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;

class Sifency_Widget_Marquee extends Widget_Base {
	use Sifency_Addons_Marquee_Controls;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_Custom_CSS_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-widget-text-slider',
			'title'          => 'Sifency Marquee',
			'icon'           => 'eicon-post-slider',
			'category'       => 'sifency-typography-widgets',
			'keywords'       => array( 'marquee', 'text', 'slider', 'animation' ),
			'script_depends' => array( 'sifency-frontend-script', 'sifency-marquee-script' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-frontend-style', 'sifency-icon', 'sifency-marquee-style' ),
		);}
	protected function _register_controls() {
		$this->add_Sifency_Addons_Marquee_Controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'text_item',
				'label'    => 'Repeted Item',
				'selector' => '.sifency-addons-sifency-addons-marquee-content',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'heading_style',
				'show_blend_mode' => true,
				'label'           => 'Content Style',
				'selector'        => '.sifency-heading-style',
				'exclude'         => array( 'alignment', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style',
				'label'     => 'Separator text Style',
				'selector'  => '.sifency-label-style',
				'condition' => array( 'separator_type' => 'text' ),
				'exclude'   => array( 'alignment', 'transition', 'tabs' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'    => 'marquee_image',
				'label'     => 'Image Style',
				'selector'  => '.sifency-item-image img',
				'selectors' => array(
					'image'      => '.sifency-item-image img',
					'container'  => '.sifency-item-image img',
					'dimensions' => '.sifency-item-image img',
				),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'    => 'marquee_separator_image',
				'label'     => 'Separator Image',
				'selector'  => '.marquee-separator img',
				'condition' => array(
					'separator_type'   => 'image',
					'separator_image!' => '',
				),
				'exclude'   => array( 'tabs', 'transition', 'object_fit', 'filters', 'shadow' ),
				'selectors' => array(
					'image'      => '.marquee-separator img',
					'container'  => '.marquee-separator img',
					'dimensions' => '.marquee-separator img',
				),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'sep_icon',
				'label'     => 'Separator Icon Style',
				'selector'  => '.marquee-separator i, .marquee-separator svg',
				'condition' => array(
					'separator_type'  => 'icon',
					'separator_icon!' => '',
				),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_sifency_addons_custom_css_controls();
	}

	protected function render() {
		$this->sifency_addons_render_custom_css();
		$settings = $this->get_settings_for_display();

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

		$animation_target = $settings['sifency_animation_target'] ?? 'wrapper';
		$stagger_delay    = isset( $settings['sifency_animation_stagger']['size'] ) ? (int) $settings['sifency_animation_stagger']['size'] : 0;

		// Use the manager to merge our base wrapper class with animation classes
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes( array( 'sifency-text-slider-wrapper' ) );

		$this->add_render_attribute( 'child_container', 'class', 'sifency-text-slider-child' );
		if ( ! empty( $settings['child_class'] ) ) {
			$this->add_render_attribute( 'child_container', 'class', $settings['child_class'] );
		}

		$direction = $settings['slider_direction'] ?? 'left';

		$pause_class = ( 'yes' === ( $settings['pause_on_hover'] ?? '' ) ) ? 'marquee-pause-on-hover' : '';
		$this->add_render_attribute( 'marquee_inner', 'class', array( 'marquee', $pause_class ) );
		$this->add_render_attribute( 'marquee_inner', 'data-direction', $direction );

		$speed = ! empty( $settings['speed']['size'] ) ? $settings['speed']['size'] : 20;
		$this->add_render_attribute( 'marquee_inner', 'data-speed', $speed );

		if ( ! empty( $settings['loop_count'] ) && (int) $settings['loop_count'] > 0 ) {
			$this->add_render_attribute( 'marquee_inner', 'data-loop-count', $settings['loop_count'] );
		}

		$flex_direction = in_array( $direction, array( 'top', 'bottom' ), true ) ? 'column' : 'row';

		?>
		<div <?php echo $animation_wrapper_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
			
			<div class="sifency-marquee-container">
				
				<?php if ( ! empty( $settings['show_main_divider'] ) && 'yes' === $settings['show_main_divider'] ) : ?>
					<div class="sifency-typo-divider"></div>
				<?php endif; ?>

				<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'child_container' ) ); ?>>
					<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'marquee_inner' ) ); ?>>
						<div class="sifency-addons-marquee-content-wrapper">
							<ul class="sifency-addons-marquee-content" style="flex-direction: <?php echo esc_attr( $flex_direction ); ?>;">
								<?php
								if ( ! empty( $settings['text_list'] ) ) {
									$count = count( $settings['text_list'] );
									foreach ( $settings['text_list'] as $index => $item ) {
										 $item_key = 'item_' . $index;

										 $item_classes = array( 'sifency-addons-sifency-addons-marquee-content' );
										if ( ! empty( $item['item_class'] ) ) {
											$item_classes[] = $item['item_class'];
										}

										 $animation_item_attrs = $animation_manager->get_item_attributes( $item_classes );

										if ( ! empty( $item['item_id'] ) ) {
											$this->add_render_attribute( $item_key, 'id', $item['item_id'] );
										}

										if ( 'child' === $animation_target && $stagger_delay > 0 ) {
											$delay = $index * $stagger_delay;
											$this->add_render_attribute( $item_key, 'style', '--animation-delay: ' . $delay . 'ms' );
										}

										$has_link = false;
										$link_key = 'link_' . $index;
										if ( ! empty( $item['item_link']['url'] ) ) {
											$has_link = true;
											$this->add_link_attributes( $link_key, $item['item_link'] );
										}

										?>
										 <li 
										 <?php
											 echo $animation_item_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
											 // Render other attributes like style, id, etc. (excluding class which is already in $animation_item_attrs)
											 $all_item_attrs = $this->get_render_attributes( $item_key );
											if ( is_array( $all_item_attrs ) ) {
												foreach ( $all_item_attrs as $name => $value ) {
													if ( 'class' !== $name ) {
														echo ' ' . esc_attr( $name ) . '="' . esc_attr( is_array( $value ) ? implode( ' ', $value ) : $value ) . '"';
													}
												}
											}
											?>
											>
											<?php if ( $has_link ) : ?>
												<a <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) ); ?>>
											<?php endif; ?>

											<span class="marquee-inner-text sifency-heading-style">
												<?php
												if ( ! empty( $item['item_image']['url'] ) ) {
                                                    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
													$image_html = \Elementor\Group_Control_Image_Size::get_attachment_image_html( $item, 'item_image' );
													if ( $image_html ) {
														echo '<div class="sifency-item-image">' . wp_kses_post( $image_html ) . '</div>';
													}
												}
												echo wp_kses_post( $item['text'] );
												?>
											</span>

											<?php if ( $has_link ) : ?>
												</a>
											<?php endif; ?>

											<?php
											if ( 'none' !== ( $settings['separator_type'] ?? 'none' ) ) {
												$separator_content = '';
												$show_separator    = false;

												switch ( $settings['separator_type'] ) {
													case 'icon':
														if ( ! empty( $settings['separator_icon']['value'] ) ) {
															ob_start();
															\Elementor\Icons_Manager::render_icon( $settings['separator_icon'], array( 'aria-hidden' => 'true' ) );
															$separator_content = ob_get_clean();
															$show_separator    = true;
														}
														break;

													case 'text':
														if ( ! empty( $settings['separator_text'] ) ) {
															$separator_content = wp_kses_post( $settings['separator_text'] );
															$show_separator    = true;
														}
														break;

													case 'image':
														if ( ! empty( $settings['separator_image']['url'] ) ) {
															$separator_content = '<img src="' . esc_url( $settings['separator_image']['url'] ) . '" alt="' . esc_attr__( 'Separator', 'sifency-addons' ) . '">';
															$show_separator    = true;
														}
														break;
												}

												if ( $show_separator && ( $index < $count - 1 || $count > 1 ) ) {
													echo '<span class="marquee-separator sifency-label-style">' . wp_kses_post( $separator_content ) . '</span>';
												}
											}
											?>
										</li>
										<?php
									}
								}
								?>
							</ul>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
