<?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_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
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_Slider_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Addons_Widget_Timeline extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-timeline-pro',
			'title'          => 'Sifency Timeline',
			'icon'           => 'eicon-history',
			'category'       => 'sifency-business-widgets',
			'keywords'       => array( 'timeline', 'history', 'progress', 'event' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-timeline-style' ),
			'script_depends' => array( 'swiper', 'sifency-timeline-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_config',
			array( 'label' => esc_html__( 'Layout & Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'horizontal-slider',
				'options' => array(
					// 'horizontal-slider' => esc_html__( '1. Horizontal Slider (Standard)', 'sifency-addons' ),
					// 'horizontal-cards'  => esc_html__( '2. Horizontal Cards', 'sifency-addons' ),
					// 'step-process'      => esc_html__( '3. Step Process (Numbers)', 'sifency-addons' ),
					// 'image-focus'       => esc_html__( '4. Image Focused Slider', 'sifency-addons' ),
					'vertical-center' => esc_html__( '5. Vertical Center (Zig-Zag)', 'sifency-addons' ),
					// 'vertical-right'    => esc_html__( '6. Vertical Right (Classic)', 'sifency-addons' ),
					// 'vertical-cards'    => esc_html__( '7. Vertical Boxed Cards', 'sifency-addons' ),
					// 'minimal-list'      => esc_html__( '8. Minimal List', 'sifency-addons' ),
					// 'hover-expand'      => esc_html__( '9. Hover Expand', 'sifency-addons' ),
					// 'dark-neon'         => esc_html__( '10. Dark / Neon Mode', 'sifency-addons' ),
				),
			)
		);
		$this->add_responsive_control(
			'items_per_view',
			array(
				'label'     => esc_html__( 'Visible Items (Slider)', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '3',
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
				),
				'condition' => array( 'layout_type' => array( 'horizontal-slider', 'horizontal-cards', 'step-process', 'image-focus' ) ),
			)
		);

		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'flex_',
				'label'    => esc_html__( 'Flex Content Layout', 'sifency-addons' ),
				'selector' => '.sifency-tl-content',
			)
		);
		$this->end_controls_section();

		// --- ITEMS SECTION ---
		$this->start_controls_section(
			'section_items',
			array( 'label' => esc_html__( 'Timeline Items', 'sifency-addons' ) )
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'date_label',
			array(
				'label'       => 'Year / Date',
				'type'        => Controls_Manager::TEXT,
				'default'     => '2023',
				'label_block' => true,
			)
		);

		$repeater->add_control(
			'content_source',
			array(
				'label'   => esc_html__( 'Content Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'editor',
				'options' => array(
					'editor'   => esc_html__( 'Editor', 'sifency-addons' ),
					'template' => esc_html__( 'Elementor Template', 'sifency-addons' ),
				),
			)
		);

		// Standard Editor
		$repeater->add_control(
			'title',
			array(
				'label'     => 'Title',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Event Title',
				'condition' => array( 'content_source' => 'editor' ),
			)
		);
		$repeater->add_control(
			'image',
			array(
				'label'     => 'Image',
				'type'      => Controls_Manager::MEDIA,
				'condition' => array( 'content_source' => 'editor' ),
			)
		);
		$repeater->add_control(
			'desc',
			array(
				'label'     => 'Description',
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => 'Lorem ipsum dolor sit amet.',
				'condition' => array( 'content_source' => 'editor' ),
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label'     => 'Link',
				'type'      => Controls_Manager::URL,
				'condition' => array( 'content_source' => 'editor' ),
			)
		);

		// Template
		$repeater->add_control(
			'template_id',
			array(
				'label'     => esc_html__( 'Select Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '0',
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array( 'content_source' => 'template' ),
			)
		);
		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'Events', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'date_label' => '1990',
						'title'      => 'Foundation',
					),
					array(
						'date_label' => '2005',
						'title'      => 'First Branch',
					),
					array(
						'date_label' => '2015',
						'title'      => 'Global Expansion',
					),
				),
				'title_field' => '{{{ date_label }}} - {{{ title }}}',
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'thumbnail',
				'default' => 'medium_large',
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'item_style',
				'label'    => 'Content Wrapper',
				'selector' => '.sifency-tl-content',
				'exclude'  => array( 'dimensions', 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'          => 'dot_style',
				'label'           => 'Dot',
				'selector'        => '.sifency-tl-dot',
				'hover_selector'  => '.sifency-tl-nav-item.active .sifency-tl-dot',
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
				'exclude'         => array( 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'line_style',
				'label'    => 'Line',
				'selector' => '.sifency-tl-center-line',
				'exclude'  => array( 'dimensions', 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'nav_item_style',
				'label'    => 'Marker Item',
				'selector' => '.sifency-tl-marker-col',
				'exclude'  => array( 'dimensions', 'transform', 'shadow', 'transition', 'bg', 'border', 'radius', 'tabs' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Heading Style',
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'bg', 'radius', 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Content Style',
				'selector'   => '.sifency-content-style',
				'color_only' => true,
				'exclude'    => array( 'bg', 'radius', 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'meta_style',
				'label'      => 'Date Style',
				'selector'   => '.sifency-meta-style',
				'color_only' => true,
				'exclude'    => array( 'bg', 'radius', 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'nav_text_style',
				'label'          => 'Nav Date',
				'color_only'     => true,
				'selector'       => '.sifency-tl-marker-col .sifency-tl-date',
				'hover_selector' => '.sifency-tl-marker-col .sifency-tl-date',
				'exclude'        => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'dimensions_selector' => '.sifency-tl-img',
				'selector'            => '.sifency-tl-img',
				'exclude'             => array( 'tabs', 'transition' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls( array() );
		$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 );

		$is_slider = in_array( $settings['layout_type'], array( 'horizontal-slider', 'horizontal-cards', 'step-process', 'image-focus' ) );

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-timeline-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-tl-layout-' . $settings['layout_type'] );

		if ( $is_slider ) {
			$js_settings = array(
				'type'            => 'slider',
				'per_view'        => $settings['items_per_view'],
				'per_view_tablet' => $settings['items_per_view_tablet'] ?? 2,
				'per_view_mobile' => $settings['items_per_view_mobile'] ?? 1,
			);
			$this->add_render_attribute( 'wrapper', 'data-tl-settings', wp_json_encode( $js_settings ) );
		}

		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>

			<?php if ( $is_slider ) : ?>
				<div class="sifency-tl-navigation">
					<div class="sifency-tl-line-bg"><div class="sifency-tl-progress-bar"></div></div>
					<div class="swiper-button-prev sifency-tl-arrow"><i class="eicon-chevron-left"></i></div>
					<div class="swiper-button-next sifency-tl-arrow"><i class="eicon-chevron-right"></i></div>
				</div>

				<div class="swiper sifency-tl-swiper">
					<div class="swiper-wrapper">
						<?php foreach ( $settings['items'] as $index => $item ) : ?>
							<div class="swiper-slide sifency-tl-item">
								<?php $this->render_item_content( $item, $settings ); ?>
								<div class="sifency-tl-dot-marker">
									<?php if ( $settings['layout_type'] === 'step-process' ) : ?>
										<span class="sifency-tl-step-num"><?php echo esc_html( $index + 1 ); ?></span>
									<?php else : ?>
										<div class="sifency-tl-dot"></div>
									<?php endif; ?>
									<div class="sifency-tl-date"><?php echo esc_html( $item['date_label'] ); ?></div>
								</div>
							</div>
						<?php endforeach; ?>
					</div>
				</div>

			<?php else : ?>
				<div class="sifency-tl-static-container">
					<div class="sifency-tl-center-line"></div>
					<?php foreach ( $settings['items'] as $index => $item ) : ?>
						<div class="sifency-tl-item">
							
							<div class="sifency-tl-marker-col">
								<div class="sifency-tl-date"><?php echo esc_html( $item['date_label'] ); ?></div>
								<div class="sifency-tl-dot"></div>
							</div>

							<div class="sifency-tl-content-col">
								<?php $this->render_item_content( $item, $settings ); ?>
							</div>

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

		</div>
		<?php
	}

	protected function render_item_content( $item, $settings ) {
		?>
		<div class="sifency-tl-content">
			<?php if ( 'template' === $item['content_source'] && ! empty( $item['template_id'] ) ) : ?>
                <?php  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
				echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $item['template_id'] );
				?>
			<?php else : ?>
				<?php
				$img_src = '';
				if ( ! empty( $item['image']['id'] ) ) {
					$img_src = Group_Control_Image_Size::get_attachment_image_src( $item['image']['id'], 'thumbnail', $settings );
				} elseif ( ! empty( $item['image']['url'] ) ) {
					$img_src = $item['image']['url'];
				}
				?>
				
				<?php if ( $img_src ) : ?>
					<div class="sifency-tl-img">
						<img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( $item['title'] ); ?>">
					</div>
				<?php endif; ?>

				<div class="sifency-tl-text-wrap">
					<div class="sifency-tl-date sifency-meta-style"><?php echo esc_html( $item['date_label'] ); ?></div>
					<h3 class="sifency-tl-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h3>
					<div class="sifency-tl-desc sifency-content-style"><?php echo wp_kses_post( $item['desc'] ); ?></div>
					<?php if ( ! empty( $item['link']['url'] ) ) : ?>
						<a href="<?php echo esc_url( $item['link']['url'] ); ?>" class="sifency-button-style">Read More <i class="eicon-arrow-right"></i></a>
					<?php endif; ?>
				</div>
			<?php endif; ?>
		</div>
		<?php
	}
}
