<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
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\Icons_Manager;
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_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_News_Ticker extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-news-ticker',
			'title'          => 'Sifency News Ticker',
			'icon'           => 'eicon-code',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'swiper', 'sifency-news-ticker-script' ),
			'style_depends'  => array( 'sifency-news-ticker-style', 'sifency-typo-style', 'sifency-icon' ),
		);}

	protected function register_controls() {

		// --- CONTENT ---
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Ticker Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'classic',
				'options' => array(
					'classic' => esc_html__( '1. Classic Bordered', 'sifency-addons' ),
					'solid'   => esc_html__( '2. Solid Label (Blue)', 'sifency-addons' ),
					'soft'    => esc_html__( '3. Soft / Light', 'sifency-addons' ),
					'dark'    => esc_html__( '4. Dark Mode', 'sifency-addons' ),
					'pill'    => esc_html__( '5. Modern Pill', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'ticker_label',
			array(
				'label'   => esc_html__( 'Label Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Breaking News',
			)
		);
		$this->add_control(
			'show_label_icon',
			array(
				'label'        => esc_html__( 'Show Label Icon', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'no',
			)
		);
		$this->add_control(
			'label_icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-bolt',
					'library' => 'solid',
				),
				'condition' => array( 'show_label_icon' => 'yes' ),
			)
		);
		$this->add_control(
			'source',
			array(
				'label'     => esc_html__( 'Content Source', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'custom',
				'options'   => array(
					'custom' => esc_html__( 'Custom Text', 'sifency-addons' ),
					'posts'  => esc_html__( 'Recent Posts', 'sifency-addons' ),
				),
				'separator' => 'before',
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'text',
			array(
				'label'   => 'Text',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Important update goes here.',
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label' => 'Link',
				'type'  => Controls_Manager::URL,
			)
		);

		$this->add_control(
			'items',
			array(
				'label'     => esc_html__( 'News Items', 'sifency-addons' ),
				'type'      => Controls_Manager::REPEATER,
				'fields'    => $repeater->get_controls(),
				'default'   => array(
					array( 'text' => '15 Cozy Season Essentials For You' ),
					array( 'text' => 'Best Stocking Stuffers Guide' ),
					array( 'text' => 'Career and Finance Updates' ),
				),
				'condition' => array( 'source' => 'custom' ),
			)
		);
		$this->add_control(
			'posts_count',
			array(
				'label'     => 'Post Count',
				'type'      => Controls_Manager::NUMBER,
				'default'   => 5,
				'condition' => array( 'source' => 'posts' ),
			)
		);
		$this->end_controls_section();

		// --- SETTINGS ---
		$this->start_controls_section( 'section_settings', array( 'label' => 'Settings' ) );

		$this->add_control(
			'effect',
			array(
				'label'   => 'Animation Effect',
				'type'    => Controls_Manager::SELECT,
				'default' => 'slide',
				'options' => array(
					'slide' => 'Slide (Vertical)',
					'fade'  => 'Fade',
				),
			)
		);

		$this->add_control(
			'autoplay',
			array(
				'label'   => 'Autoplay',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'autoplay_speed',
			array(
				'label'     => 'Speed (ms)',
				'type'      => Controls_Manager::NUMBER,
				'default'   => 4000,
				'condition' => array( 'autoplay' => 'yes' ),
			)
		);

		$this->add_control(
			'show_arrows',
			array(
				'label'   => 'Show Arrows',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_close',
			array(
				'label'   => 'Show Close Button',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'style_separators',
			array(
				'label' => esc_html__( 'Separators & Borders', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'heading_label_border',
			array(
				'label' => esc_html__( 'Label Border', 'sifency-addons' ),
				'type'  => Controls_Manager::HEADING,
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'label_border_group',
				'selector' => '{{WRAPPER}} .sifency-nt-label',
			)
		);
		$this->add_control(
			'heading_controls_border',
			array(
				'label' => esc_html__( 'Controls Container Border', 'sifency-addons' ),
				'type'  => Controls_Manager::HEADING,
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'controls_border_group',
				'selector' => '{{WRAPPER}} .sifency-nt-controls',
			)
		);
		$this->add_control(
			'heading_sep_line',
			array(
				'label'     => esc_html__( 'Separator Line', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => array( 'show_close' => 'yes' ),
			)
		);
		$this->add_control(
			'sep_line_width',
			array(
				'label'      => esc_html__( 'Separator Width (px)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 10,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-nt-sep' => 'border-left-width: {{SIZE}}{{UNIT}}; border-left-style: solid;',
				),
				'condition'  => array( 'show_close' => 'yes' ),
			)
		);
		$this->add_control(
			'sep_line_color',
			array(
				'label'     => esc_html__( 'Separator Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-nt-sep' => 'border-left-color: {{VALUE}};',
				),
				'condition' => array( 'show_close' => 'yes' ),
			)
		);
		$this->add_responsive_control(
			'sep_line_height',
			array(
				'label'      => esc_html__( 'Separator Height (%)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'default'    => array(
					'size' => 50,
					'unit' => '%',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-nt-sep' => 'height: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array( 'show_close' => 'yes' ),
			)
		);
		$this->add_control(
			'label_bg_heading',
			array(
				'label'     => esc_html__( 'Left Label Wrapper', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'label_bg_group',
				'label'    => esc_html__( 'Label Background', 'sifency-addons' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .sifency-nt-label',
			)
		);
		$this->add_responsive_control(
			'label_padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-nt-label' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'label_inner_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-nt-label' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_control(
			'controls_bg_heading',
			array(
				'label'     => esc_html__( 'Arrow and close wrapper', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'controls_bg_group',
				'label'    => esc_html__( 'Controls Background', 'sifency-addons' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .sifency-nt-controls',
			)
		);
		$this->add_responsive_control(
			'controls_container_padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-nt-controls' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'controls_container_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-nt-controls' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- STYLES VIA TRAITS ---
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'nt_wrapper_style',
				'label'    => 'Ticker Wrapper',
				'selector' => '{{WRAPPER}} .sifency-nt-wrapper',
				'exclude'  => array( 'tabs', 'transitions' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'label_style',
				'label'    => 'Label Text',
				'selector' => '.sifency-label-style',
				'exclude'  => array( 'tabs', 'transitions', 'alignment' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'label_icon_style',
				'label'     => 'Label Icon',
				'selector'  => '{{WRAPPER}} .sifency-nt-label-icon i, {{WRAPPER}} .sifency-nt-label-icon svg',
				'condition' => array( 'show_label_icon' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'News Content',
				'selector' => '.sifency-content-style',
				'exclude'  => array( 'transitions', 'alignment', 'bg' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'nt_btn_style',
				'label'     => 'Arrow Buttons',
				'selector'  => '{{WRAPPER}} .sifency-nt-btn',
				'condition' => array( 'show_arrows' => 'yes' ),
			)
		);

		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'close_btn_style',
				'label'     => 'Close Button',
				'selector'  => '{{WRAPPER}} .sifency-nt-close',
				'condition' => array( 'show_close' => 'yes' ),
			)
		);

		$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 ) {
		$wrapper_id              = esc_attr( $context['dom_id'] );
		$settings                = $this->get_settings_for_display();
		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$news_items = array();

		if ( 'posts' === $settings['source'] ) {
			$query_args = array(
				'post_type'      => 'post',
				'posts_per_page' => $settings['posts_count'],
				'post_status'    => 'publish',
			);
			$query      = new \WP_Query( $query_args );
			if ( $query->have_posts() ) {
				while ( $query->have_posts() ) {
					$query->the_post();
					$news_items[] = array(
						'text' => get_the_title(),
						'url'  => get_permalink(),
					);
				}
				wp_reset_postdata();
			}
		} else {
			foreach ( $settings['items'] as $item ) {
				$news_items[] = array(
					'text' => $item['text'],
					'url'  => $item['link']['url'] ?? '',
				);
			}
		}

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

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-news-ticker-wrapper' );
		$this->add_render_attribute( 'container', 'class', array( 'sifency-nt-wrapper', 'sifency-nt-layout-' . $settings['layout_type'] ) );

		$js_settings = array(
			'autoplay' => $settings['autoplay'],
			'speed'    => $settings['autoplay_speed'],
			'effect'   => $settings['effect'],
		);
		$this->add_render_attribute( 'container', 'data-nt-settings', wp_json_encode( $js_settings ) );
		?>
		<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' ) . $animation_wrapper_attrs ); ?>>

				<div class="sifency-nt-label">
					<?php if ( 'yes' === $settings['show_label_icon'] && ! empty( $settings['label_icon']['value'] ) ) : ?>
						<span class="sifency-nt-label-icon">
							<?php Icons_Manager::render_icon( $settings['label_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</span>
					<?php endif; ?>

					<span class="sifency-label-style"><?php echo esc_html( $settings['ticker_label'] ); ?></span>
				</div>

				<div class="swiper sifency-nt-swiper">
					<div class="swiper-wrapper">
						<?php foreach ( $news_items as $item ) : ?>
							<div class="swiper-slide sifency-nt-slide">
								<div class="sifency-nt-content sifency-content-style">
									<?php if ( ! empty( $item['url'] ) ) : ?>
										<a href="<?php echo esc_url( $item['url'] ); ?>"><?php echo esc_html( $item['text'] ); ?></a>
									<?php else : ?>
										<span><?php echo esc_html( $item['text'] ); ?></span>
									<?php endif; ?>
								</div>
							</div>
						<?php endforeach; ?>
					</div>
				</div>

				<!-- Controls -->
				<div class="sifency-nt-controls">
					<?php if ( 'yes' === $settings['show_arrows'] ) : ?>
						<div class="sifency-nt-nav">
							<div class="sifency-nt-btn sifency-nt-prev"><i class="eicon-chevron-left"></i></div>
							<div class="sifency-nt-btn sifency-nt-next"><i class="eicon-chevron-right"></i></div>
						</div>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_close'] ) : ?>
						<div class="sifency-nt-sep"></div>
						<div class="sifency-nt-close"><i class="eicon-close"></i></div>
					<?php endif; ?>
				</div>

			</div>

		</div>
		<?php
	}
}
