<?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_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Addons_Widget_Advanced_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_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-advanced-timeline',
			'title'          => 'Sifency Advanced Timeline',
			'icon'           => 'eicon-time-line',
			'category'       => 'sifency-business-widgets',
			'keywords'       => array( 'timeline', 'history', 'progress', 'event' ),
			'style_depends'  => array( 'sifency-advanced-timeline-style', 'sifency-icons', 'sifency-frontend-style', 'sifency-typo-style' ),
			'script_depends' => array( 'swiper', 'sifency-carousel-handler', 'sifency-advanced-timeline-script' ),
		);}
	protected function register_controls() {

		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Timeline Items', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'horizontal',
				'options' => array(
					'horizontal' => esc_html__( 'Horizontal Progress', 'sifency-addons' ),
					'cards'      => esc_html__( 'Card Slider', 'sifency-addons' ),
					'minimal'    => esc_html__( 'Minimal', 'sifency-addons' ),
				),
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'date_label',
			array(
				'label'       => esc_html__( 'Timeline Label (Year/Date)', 'sifency-addons' ),
				'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__( 'Standard Editor', 'sifency-addons' ),
					'template' => esc_html__( 'Elementor Template', 'sifency-addons' ),
				),
			)
		);

		// Editor Controls
		$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'   => 'Details about this event...',
				'condition' => array( 'content_source' => 'editor' ),
			)
		);
		$repeater->add_control(
			'btn_text',
			array(
				'label'     => 'Button Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Read More',
				'condition' => array( 'content_source' => 'editor' ),
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label'     => 'Link',
				'type'      => Controls_Manager::URL,
				'condition' => array( 'content_source' => 'editor' ),
			)
		);

		// Template Control
		$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' => '1997',
						'title'      => 'The Beginning',
					),
					array(
						'date_label' => '2009',
						'title'      => 'Expansion',
					),
					array(
						'date_label' => '2015',
						'title'      => 'Global Reach',
					),
					array(
						'date_label' => '2022',
						'title'      => 'Innovation',
					),
				),
				'title_field' => '{{{ date_label }}} - {{{ title }}}',
			)
		);

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

		$this->end_controls_section();

		$this->add_sifency_addons_slider_controls(
			array(
				'label'           => 'Carousel Settings',
				'slides_per_view' => array( 'default' => 1 ),
				'space_between'   => array( 'default' => 0 ),
			)
		);
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_tl_nav',
			array(
				'label' => 'Timeline Navigation Bar',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'pag_line_color',
			array(
				'label'     => 'Line Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-tl-line' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'active_line_color',
			array(
				'label'     => 'Active Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-tl-progress-bar' => 'background-color: {{VALUE}};',
					'{{WRAPPER}} .sifency-tl-dot.active'   => 'background-color: {{VALUE}}; border-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'line_heading',
			array(
				'label'     => esc_html__( 'Progress Line', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_responsive_control(
			'line_height',
			array(
				'label'     => esc_html__( 'Line Height', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 1,
						'max' => 10,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-tl-line'         => 'height: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-tl-progress-bar' => 'height: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-tl-dot'          => 'top: calc(50% - ({{SIZE}}{{UNIT}} / 2));',
				),
			)
		);
		$this->add_control(
			'line_color',
			array(
				'label'     => esc_html__( 'Default Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-tl-line' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'active_color',
			array(
				'label'     => esc_html__( 'Active Progress Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-tl-progress-bar' => 'background-color: {{VALUE}};',
					'{{WRAPPER}} .sifency-tl-dot.active'   => 'background-color: {{VALUE}}; border-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'line_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-tl-line, {{WRAPPER}} .sifency-tl-progress-bar' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'    => 'contet_wrapper_style',
				'label'     => 'Card Wrapper',
				'selector'  => '.sifency-tl-layout-cards .sifency-tl-std-content',
				'exclude'   => array( 'dimensions' ),
				'condition' => array( 'layout_type' => 'cards' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'item_style',
				'label'    => 'Content Wrapper',
				'selector' => '.sifency-tl-text-wapper',
				'exclude'  => array( 'dimensions' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'nav_item_style',
				'label'    => 'Nav Item',
				'selector' => '.sifency-tl-nav-item',
				'exclude'  => array( 'dimensions', 'transform', 'shadow', 'transition', 'bg', 'border', 'radius', 'tabs' ),
			)
		);
		$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_master_typography_controls(
			array(
				'prefix'          => 'nav_text_style',
				'label'           => 'Nav Text Style',
				'selector'        => '.sifency-tl-nav-item .sifency-tl-date',
				'hover_selector'  => '.sifency-tl-nav-item.active .sifency-tl-date',
				'exclude'         => array( 'gradient' ),
				'exclude'         => array( 'alignment', 'transition', 'box_model' ),
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Heading Style',
				'exclude'  => array( 'tabs', 'stroke', 'shadow' ),
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'tabs', 'shadow', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style',
				'label'          => 'Content Style',
				'selector'       => '.sifency-content-style',
				'color_only'     => true,
				'show_max_width' => true,
				'exclude'        => array( 'tabs', 'stroke', 'shadow', 'transition', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'meta_style',
				'label'      => 'Date Style',
				'selector'   => '.sifency-meta-style',
				'color_only' => true,
				'exclude'    => array( 'tabs', 'stroke', 'shadow', 'transition', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'     => '.sifency-tl-img img',
				'selectors'    => array(
					'image'      => '.sifency-tl-img img',
					'container'  => '.sifency-tl-img img',
					'dimensions' => '.sifency-tl-img img',
				),
				'show_overlay' => false,
				'exclude'      => array( 'transition', 'filters', 'opacity' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'exclude' => array( 'margin' ),
			)
		);
		$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 );

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

		$count          = ! empty( $settings['items'] ) ? count( $settings['items'] ) : 0;
		$slider_options = $this->get_sifency_swiper_options( $settings, $count );
		$this->add_render_attribute( 'slider_container', 'class', array( 'sifency-tl-swiper', 'swiper-container' ) );
		$this->add_render_attribute( 'slider_container', 'data-swiper-options', wp_json_encode( $slider_options ) );
		?>
		<?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' ) ); ?>>
			<div class="sifency-tl-nav-container">
				<div class="sifency-tl-line-wrapper">
					<div class="sifency-tl-line"></div>
					<div class="sifency-tl-progress-bar"></div>
				</div>
				<div class="sifency-tl-dots-wrapper">
					<?php
					if ( ! empty( $settings['items'] ) ) :
						foreach ( $settings['items'] as $index => $item ) :
							$is_active  = $index === 0 ? 'active' : '';
							$date_label = isset( $item['date_label'] ) ? $item['date_label'] : '';
							?>
						<div class="sifency-tl-nav-item <?php echo esc_attr( $is_active ); ?>" data-index="<?php echo esc_attr( $index ); ?>">
							<span class="sifency-tl-date"><?php echo esc_html( $date_label ); ?></span>
							<span class="sifency-tl-dot <?php echo esc_attr( $is_active ); ?>"></span>
						</div>
							<?php
					endforeach;
					endif;
					?>
				</div>
			</div>

			<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'slider_container' ) ); ?>>
				<div class="swiper-wrapper">
					<?php
					if ( ! empty( $settings['items'] ) ) :
						foreach ( $settings['items'] as $index => $item ) :

							$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'];
							}

							$title          = isset( $item['title'] ) ? $item['title'] : '';
							$date_label     = isset( $item['date_label'] ) ? $item['date_label'] : '';
							$desc           = isset( $item['desc'] ) ? $item['desc'] : '';
							$btn_text       = isset( $item['btn_text'] ) ? $item['btn_text'] : '';
							$link_url       = isset( $item['link']['url'] ) ? $item['link']['url'] : '#';
							$content_source = isset( $item['content_source'] ) ? $item['content_source'] : 'editor';
							$template_id    = isset( $item['template_id'] ) ? $item['template_id'] : '';
							?>
						<div class="swiper-slide sifency-tl-slide">
							<?php if ( 'template' === $content_source && ! empty( $template_id ) ) : ?>
								<div class="sifency-tl-template-content">
                                    <?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
									echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template_id );
									?>
								</div>
							
							<?php else : ?>
								<div class="sifency-tl-std-content">
									<?php if ( ! empty( $img_src ) ) : ?>
										<div class="sifency-tl-img sifency-image-style">
											<img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( $title ); ?>">
										</div>
									<?php endif; ?>
									
									<div class="sifency-tl-text sifency-tl-text-wapper">
										<span class="sifency-tl-meta-date sifency-meta-style"><?php echo esc_html( $date_label ); ?></span>
										<h3 class="sifency-tl-title sifency-heading-style"><?php echo esc_html( $title ); ?></h3>
										<div class="sifency-tl-desc sifency-content-style"><?php echo wp_kses_post( $desc ); ?></div>
										
										<?php if ( ! empty( $btn_text ) ) : ?>
											<a href="<?php echo esc_url( $link_url ); ?>" class="sifency-button-style">
												<?php echo esc_html( $btn_text ); ?> <i class="eicon-chevron-right"></i>
											</a>
										<?php endif; ?>
									</div>
								</div>
							<?php endif; ?>

						</div>
							<?php
					endforeach;
					endif;
					?>
				</div>
				<?php $this->render_sifency_addons_slider_pagination( $settings ); ?>
				<?php $this->render_sifency_addons_slider_nav( $settings ); ?>
			</div>
		</div>
		<?php
	}
}
