<?php 
namespace UltraAddons\Widget;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_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\Icons_Manager;


if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * News Ticker Widget
 * Create excellent step by step visual diagram and instructions using this smart widget.
 * Credit: https://www.jqueryscript.net/animation/breaking-news-ticker.html
 * @since 1.1.0.7
 * @package UltraAddons
 * @author Saiful islam <codersaiful@gmail.com>
 * @author Rafiul <bmrafiul.alam@gmail.com>
 */
class News_Ticker extends Base{
	
	  public function __construct($data = [], $ultraaddons_args = null) {
        parent::__construct($data, $ultraaddons_args);

        //Naming of Args for News Ticker
        $ultraaddons_name           = 'NewsTicker';
        $js_file_url    = ULTRA_ADDONS_ASSETS . 'vendor/js/breaking-news-ticker.min.js';
        $dependency     =  ['jquery'];//['jquery'];
        $version        = ULTRA_ADDONS_VERSION;
        $in_footer  = true;

        wp_register_script( $ultraaddons_name, $js_file_url, $dependency, $version, $in_footer );
        wp_enqueue_script( $ultraaddons_name );


        //CSS file News Ticker
        wp_register_style('NewsTicker', ULTRA_ADDONS_ASSETS . 'vendor/css/breaking-news-ticker.css', array(), ULTRA_ADDONS_VERSION );
        wp_enqueue_style('NewsTicker' );

    }
	/**
     * By Saiful Islam
     * depend css for this widget
     * 
     * @return Array
     */
    public function get_style_depends() {
        return ['NewsTicker'];
    }

    /**
     * Retrieve the list of scripts the skill bar widget depended on.
     *
     * Used to set scripts dependencies required to run the widget.
     *
     * @since 1.0.9.2
     * @access public
     *
     * @return array Widget scripts dependencies.
     * @by Saiful
     */
    public function get_script_depends() {
		return [ 'jquery','NewsTicker' ];
    }

    /**
     * Get your widget name
     *
     * Retrieve oEmbed widget title.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string keywords
     */
    public function get_keywords() {
        return [ 'ultraaddons-elementor-lite', 'ua', 'step flow', 'step', 'flow' ];
    }
	
	 /**
     * Register widget controls.
     *
     * Adds different input fields to allow the user to change and customize the widget settings.
     *
     * @since 1.0.0
     * @access protected
     */
    protected function register_controls() {
        //For Content Section
        $this->ticker_content_controls();
        //For Design Section Style Tab
   
        //For Setting Control
        $this->ticker_settings_controls();
		
		//For Style Control
        $this->ticker_style_controls();
		//For Btn Control
        $this->ticker_btn_style_controls();
    }

    /**
     * Retrive setting for news tricker control
     * 
     * @author Saiful <codersaiful@gmail.com>
     *
     * @return void
     */
    protected function ticker_settings_controls(){
        $this->start_controls_section(
            'ticker_settings',
            [
                'label'     => esc_html__( 'Control Settings', 'ultraaddons-elementor-lite' ),
            ]
        );

        
		$this->add_control(
			'delayTimer',
			[
				'label' => __( 'Delay Timer', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::NUMBER,
				'min' => 2000,
				'max' => 6000,
				'step' => 100,
				'default' => 4000,
				'frontend_available' => true,
			]
		);
		$this->add_control(
			'scrollSpeed',
			[
				'label' => __( 'Scroll Speed', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::NUMBER,
				'min' => 2,
				'max' => 20,
				'step' => 2,
				'default' => 2,
				'frontend_available' => true,
			]
		);
		$this->add_control(
			'zIndex',
			[
				'label' => __( 'ZIndex', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::NUMBER,
				'min' => 99999,
				'max' => 999999,
				'step' => 1000,
				'default' => 99999,
				'frontend_available' => true,
			]
		);
		
		$this->add_control(
			'play',
			[
				'label' => __( 'Play', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => __( 'Yes', 'ultraaddons-elementor-lite' ),
				'label_off' => __( 'No', 'ultraaddons-elementor-lite' ),
				'return_value' => 'yes',
				'default' => 'yes',
				'frontend_available' => true,
			]
		);	
		$this->add_control(
			'stopOnHover',
			[
				'label' => __( 'Stop on Hover', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => __( 'Yes', 'ultraaddons-elementor-lite' ),
				'label_off' => __( 'No', 'ultraaddons-elementor-lite' ),
				'return_value' => 'yes',
				'default' => 'yes',
				'frontend_available' => true,
			]
		);
		$this->add_control(
			'show_controls',
			[
				'label' => __( 'Action Button', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => __( 'Yes', 'ultraaddons-elementor-lite' ),
				'label_off' => __( 'No', 'ultraaddons-elementor-lite' ),
				'return_value' => 'yes',
				'default' => 'yes',
			]
		);
		$this->add_control(
			'position',
			[
				'label' => __( 'Position', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'auto',
				'frontend_available' => true,
				'options' => [
					'auto'  => __( 'Default', 'ultraaddons-elementor-lite' ),
					'fixed-top' => __( 'Top', 'ultraaddons-elementor-lite' ),
					'fixed-bottom' => __( 'Bottom', 'ultraaddons-elementor-lite' ),
				],
			]
		);
		$this->add_control(
			'effect',
			[
				'label' => __( 'Effects', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'scroll',
				'frontend_available' => true,
				'options' => [
					'scroll'  => __( 'Scroll', 'ultraaddons-elementor-lite' ),
					'fade'  => __( 'Fade', 'ultraaddons-elementor-lite' ),
					'slide-down' => __( 'Slide Down', 'ultraaddons-elementor-lite' ),
					'slide-up' => __( 'Slide Up', 'ultraaddons-elementor-lite' ),
					'slide-right' => __( 'Slide Right', 'ultraaddons-elementor-lite' ),
					'slide-left' => __( 'Slide Left', 'ultraaddons-elementor-lite' ),
					'typography' => __( 'Typography', 'ultraaddons-elementor-lite' ),
				],
			]
		);
		$this->add_control(
			'direction',
			[
				'label' => __( 'Direction', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'ltr',
				'frontend_available' => true,
				'options' => [
					'rtl'  => __( 'RTL', 'ultraaddons-elementor-lite' ),
					'ltr' => __( 'LTR', 'ultraaddons-elementor-lite' ),
				],
			]
		);
		

        $this->end_controls_section();
    }

	/**
	 * Here should comment actually\
	 * 
	 * It's actually content control part
	 */
	protected function ticker_content_controls() {
		
        $this->start_controls_section(
		
            '_ua_news_ticker_content_tab',
            [
                'label'     => esc_html__( 'Content', 'ultraaddons-elementor-lite' ),
                'tab'       => Controls_Manager::TAB_CONTENT,
            ]
        );
		$this->add_control(
			'ticker_label', [
				'label' => __( 'Label', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::TEXT,
				'default' => __( 'News' , 'ultraaddons-elementor-lite' ),
				'label_block' => true,
			]
		);
		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'news_title', [
				'label' => __( 'Text', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::TEXTAREA,
				'default' => __( 'Lorem Ipsum simply dummy text of the printing and typesetting industry' , 'ultraaddons-elementor-lite' ),
				'label_block' => true,
			]
		);
		$repeater->add_control(
			'news_link',
			[
				'label' => __( 'Link', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::URL,
				'placeholder' => __( 'https://your-link.com', 'ultraaddons-elementor-lite' ),
				'show_external' => true,
				'default' => [
					'url' => '#',
					'is_external' => true,
					'nofollow' => true,
				],
			]
		);
		
		$this->add_control(
			'ticker_list',
			[
				'label' => __( 'News Ticker List', 'ultraaddons-elementor-lite' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'news_title' => __( '- Lorem Ipsum simply dummy text of the printing and typesetting industry', 'ultraaddons-elementor-lite' ),
					],
					[
						'news_title' => __( '- When an unknown printer took a galley of type and scrambled it to make a type specimen book', 'ultraaddons-elementor-lite' ),
					],
				],
				'title_field' => '{{{ news_title }}}',
			]
		);

		
	$this->end_controls_section();
	}
	protected function ticker_style_controls() {
		
        $this->start_controls_section(
            '_ua_news_ticker_content_style',
            [
                'label'     => esc_html__( 'Content', 'ultraaddons-elementor-lite' ),
                'tab'       => Controls_Manager::TAB_STYLE,
            ]
        );
		$this->add_control(
			'_ua_ticker_text_color', [
				'label' => __( 'Text Color', 'ultraaddons-elementor-lite' ),
				'type'      => Controls_Manager::COLOR,
				'default'=>'#333',
				'selectors' => [
						'{{WRAPPER}} .ua-news-ticker-wrap ul li a' => 'color: {{VALUE}};',
				],
				'separator'=> 'after'
			]
        ); 
		$this->add_control(
			'_ua_ticker_label_bg', [
				'label' => __( 'Label Background', 'ultraaddons-elementor-lite' ),
				'type'      => Controls_Manager::COLOR,
				'default'=>'#333',
				'selectors' => [
						'{{WRAPPER}} .bn-label' => 'background-color: {{VALUE}};',
				],
			]
        );
		$this->add_control(
			'_ua_ticker_label_color', [
				'label' => __( 'Label Text Color', 'ultraaddons-elementor-lite' ),
				'type'      => Controls_Manager::COLOR,
				'default'=>'#fff',
				'selectors' => [
						'{{WRAPPER}} .bn-label' => 'color: {{VALUE}};',
				],
				'separator'=> 'after'
			]
        ); 
		$this->add_control(
			'_ua_ticker_border_color', [
				'label' => __( 'Border Color', 'ultraaddons-elementor-lite' ),
				'type'      => Controls_Manager::COLOR,
				'default'=>'#333',
				'selectors' => [
						'{{WRAPPER}} .ua-news-ticker-wrap' => 'border: 1px solid {{VALUE}};',
				],
			]
        );
		
		$this->add_responsive_control(
			'_ua_ticker_border_radius',
			[
				'label'       => esc_html__( 'Box Radius', 'ultraaddons-elementor-lite' ),
				'type'        => Controls_Manager::DIMENSIONS,
				'size_units'  => [ 'px', '%' ],
				'placeholder' => [
					'top'    => '',
					'right'  => '',
					'bottom' => '',
					'left'   => '',
				],
				'selectors'   => [
					'{{WRAPPER}} .ua-news-ticker-wrap' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;',
				],
			]
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'ticker_box_shadow',
				'label' => __( 'Box Shadow', 'ultraaddons-elementor-lite' ),
				'selector' => '{{WRAPPER}} .ua-news-ticker-wrap',
			]
		);
		
		$this->add_control(
			'_ua_ticker_bg_color', [
				'label' => __( 'Background Color', 'ultraaddons-elementor-lite' ),
				'type'      => Controls_Manager::COLOR,
				'default'=>'#fff',
				'selectors' => [
						'{{WRAPPER}} .ua-news-ticker-wrap' => 'background-color: {{VALUE}};',
				],
				'separator'=> 'after'
			]
        );
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
					'name' => 'ticker_label_typography',
					'label' => 'Label Typography',
					'selector' => '{{WRAPPER}} .bn-label',

			]
        );
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
					'name' => 'ticker_typography',
					'label' => 'Ticker Typography',
					'selector' => '{{WRAPPER}} .bn-news ul li',

			]
        );
		
		$this->end_controls_section();
	}
	protected function ticker_btn_style_controls() {
		
        $this->start_controls_section(
            '_ua_ticker_btn_style',
            [
                'label'     => esc_html__( 'Action Button', 'ultraaddons-elementor-lite' ),
                'tab'       => Controls_Manager::TAB_STYLE,
            ]
        );
		$this->add_control(
			'_ua_ticker_btn_bg', [
				'label' => __( 'Control Color', 'ultraaddons-elementor-lite' ),
				'type'      => Controls_Manager::COLOR,
				'default'=>'#333',
				'selectors' => [
						'{{WRAPPER}} .bn-arrow:after, .bn-pause:after, .bn-pause:before' => 'border-color: {{VALUE}} !important;',
						'{{WRAPPER}} .bn-pause:after' => 'background-color: {{VALUE}} !important;',
						'{{WRAPPER}} .bn-pause:before' => 'background-color: {{VALUE}} !important;',
				],
			]
        );
		$this->add_control(
			'_ua_ticker_ctrl_bg', [
				'label' => __( 'Control Background', 'ultraaddons-elementor-lite' ),
				'type'      => Controls_Manager::COLOR,
				'default'=>'#eaeaea',
				'selectors' => [
						'{{WRAPPER}} .bn-controls button' => 'background-color: {{VALUE}};',
					],
			]
        );
		$this->add_control(
			'_ua_ticker_play_btn_bg', [
				'label' => __( 'Play Button Background', 'ultraaddons-elementor-lite' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
						'{{WRAPPER}} .bn-play::after' => 'border-left-color: {{VALUE}};',
					],
			]
        );
		$this->end_controls_section();
	}
	
	
   protected function render() {
		$settings 				=	$this->get_settings_for_display();
	?>
	<div class="ua-news-ticker-wrap">
	  <div class="bn-label"><?php echo esc_html( $settings['ticker_label'] ); ?></div>
	  <div class="bn-news">
		<?php
		
			if ( isset( $settings['ticker_list'] ) ) {
				echo '<ul>';
				foreach (  $settings['ticker_list'] as $ultraaddons_item ) {
					$target 	= $ultraaddons_item['news_link']['is_external'] ? ' target="_blank"' : '';
					$nofollow 	= $ultraaddons_item['news_link']['nofollow'] ? ' rel="nofollow"' : '';
					$url		= $ultraaddons_item['news_link']['url'];
					echo '<li class="news-tricker-element elementor-repeater-item-' . esc_attr( $ultraaddons_item['_id'] ) . '">';
					if( ! empty( $url ) ){
						echo '<a href="' . esc_url( $url ) . '"' . esc_attr( $target ) . esc_attr( $nofollow ) . '>'. esc_html( $ultraaddons_item['news_title'] ) .'</a>';
					}
					
					echo '</li>';
				}
				echo '</ul>';
			}
		?>
	  </div>
	  <?php if('yes'=== $settings['show_controls']): ?>
	  <div class="bn-controls">
		<button><span class="bn-arrow bn-prev"></span></button>
		<button><span class="bn-action"></span></button>
		<button><span class="bn-arrow bn-next"></span></button>
	  </div>
	  <?php endif;?>
	</div>
<?php }
}