<?php
namespace ova_framework\Widgets;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Core\Schemes\Typography;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Image_Size;
use Elementor\Utils;

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

class ova_time_countdown extends Widget_Base {

	public function get_name() {
		return 'ova_time_countdown';
	}

	public function get_title() {
		return __( 'Countdown Timer', 'ova-framework' );
	}

	public function get_icon() {
		return 'fa fa-clock-o';
	}

	public function get_categories() {
		return [ 'ovatheme' ];
	}

	public function get_script_depends() {
		wp_enqueue_script( 'jquery-countdown-plugin', OVA_PLUGIN_URI.'/assets/libs/jquery-countdown/jquery.plugin.min.js', array(), null );
        wp_enqueue_script( 'jquery-countdown', OVA_PLUGIN_URI.'/assets/libs/jquery-countdown/jquery.countdown.min.js', array(), null );
        
        $language =  get_theme_mod( 'choice_language' );
        if ($language != '') {
            wp_enqueue_script( 'jquery-countdown-', OVA_PLUGIN_URI.'/assets/libs/jquery-countdown/jquery.countdown-'.$language.'.js', array(), null );
        }
		return [ 'script-elementor' ];
	}

	protected function _register_controls() {

		


		/*************** Section Countdouwn Offers ***************/
		$this->start_controls_section(
			'section_countdown',
			[
				'label' => __( 'Countdown', 'ova-framework' ),
			]
		);


			$this->add_control(
				'version',
				[
					'label' => __( 'Version', 'ova-framework' ),
					'type' => Controls_Manager::SELECT,
					'default' => 'version_1',
					'options' => [
						'version_1' => esc_html__( 'Version 1', 'ova-framework' ),
						'version_2' => esc_html__( 'Version 2', 'ova-framework' ),
					]
				]
			);

			$this->add_control(
				'due_date',
				[
					'label' => __( 'Due Date', 'ova-framework' ),
					'type' => Controls_Manager::DATE_TIME,
					'default' => date( 'Y-m-d H:i', strtotime( '+1 day' ) + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ),
					'description' => sprintf( __( 'Date set according to your timezone: %s.', 'ova-framework' ), Utils::get_timezone_string() ),
					'separator' => 'after',
				]
			);

			$this->add_control(
				'text_button',
				[
					'label' => __( 'Text Button', 'ova-framework' ),
					'default' => __( 'Participate Now', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::TEXT,
				]
			);

			$this->add_control(
				'link_button',
				[
					'label' => __( 'Link', 'ova-framework' ),
					'default' => '#',
					'type' => \Elementor\Controls_Manager::TEXT,
				]
			);

			$this->add_control(
				'target',
				[
					'label' => __( 'Open the link on', 'ova-framework' ),
					'default' => '_self',
					'type' => \Elementor\Controls_Manager::SELECT,
					'options' => [
						'_self' => esc_html__( 'The Same Window', 'ova-framework' ),
						'_blank' => esc_html__( 'A New Window', 'ova-framework' ),
					],
				]
			);

			$this->add_control(
				'number_countdown',
				[
					'label' => __( 'Number', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::HEADING,
				]
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'name' => 'number_countdown_typo',
					'selector' => '{{WRAPPER}} .ova_time_countdown .due_date .countdown-amount',
				]
			);

			$this->add_control(
				'number_countdown_color',
				[
					'label' => __( 'Color', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .due_date .countdown-section .countdown-amount' => 'color: {{VALUE}}',
					],
				]
			);


			$this->add_control(
				'label_countdown',
				[
					'label' => __( 'Label', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::HEADING,
					'separator' => 'before',
				]
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'name' => 'label_typo',
					'selector' => '{{WRAPPER}} .ova_time_countdown .due_date .countdown-period',
				]
			);

			$this->add_control(
				'period_color',
				[
					'label' => __( 'Color', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .due_date .countdown-period' => 'color: {{VALUE}}',
					],
				]
			);


			$this->add_control(
				'button_style',
				[
					'label' => __( 'Button', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::HEADING,
				]
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'name' => 'button_typo',
					'selector' => '{{WRAPPER}} .ova_time_countdown .ova-button a',
				]
			);

			$this->add_control(
				'button_color',
				[
					'label' => __( 'Color', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .ova-button a' => 'color: {{VALUE}}',
					],
				]
			);

			$this->add_control(
				'button_bg_color',
				[
					'label' => __( 'Background Color', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .ova-button a' => 'background-color: {{VALUE}}',
					],
				]
			);

			$this->add_control(
				'button_color_hover',
				[
					'label' => __( 'Color When Hovering', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .ova-button a:hover' => 'color: {{VALUE}}',
					],
				]
			);

			$this->add_control(
				'button_bg_color_hover',
				[
					'label' => __( 'Background Color When Hovering', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .ova-button a:hover' => 'background-color: {{VALUE}}',
					],
				]
			);


			$this->add_control(
				'label_background',
				[
					'label' => __( 'Background', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::HEADING,
					'separator' => 'before',
				]
			);

			$this->add_control(
				'background_time',
				[
					'label' => __( 'Background Color', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown' => 'background-color: {{VALUE}}',
					],
				]
			);



			$this->add_control(
				'show_days',
				[
					'label' => __( 'Days', 'ova-framework' ),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __( 'Show', 'ova-framework' ),
					'label_off' => __( 'Hide', 'ova-framework' ),
					'default' => 'yes',
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .due_date .countdown-section:nth-child(1)' => 'display: inline-block;',
					],
					'separator' => 'before',
				]
			);

			$this->add_control(
				'show_hours',
				[
					'label' => __( 'Hours', 'ova-framework' ),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __( 'Show', 'ova-framework' ),
					'label_off' => __( 'Hide', 'ova-framework' ),
					'default' => 'yes',
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .due_date .countdown-section:nth-child(2)' => 'display: inline-block;',
					],
				]
			);

			$this->add_control(
				'show_minutes',
				[
					'label' => __( 'Minutes', 'ova-framework' ),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __( 'Show', 'ova-framework' ),
					'label_off' => __( 'Hide', 'ova-framework' ),
					'default' => 'yes',
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .due_date .countdown-section:nth-child(3)' => 'display: inline-block;',
					],
				]
			);

			$this->add_control(
				'show_seconds',
				[
					'label' => __( 'Seconds', 'ova-framework' ),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __( 'Show', 'ova-framework' ),
					'label_off' => __( 'Hide', 'ova-framework' ),
					'default' => 'yes',
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .due_date .countdown-section:nth-child(4)' => 'display: inline-block;',
					],
				]
			);

			$this->add_responsive_control(
				'spacing_countdown',
				[
					'label' => __( 'Spacing', 'ova-framework' ),
					'type' => Controls_Manager::SLIDER,
					'size_units' => [ 'px', '%' ],
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 200,
							'step' => 1,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'default' => [
						'unit' => 'px',
					],
					'selectors' => [
						'{{WRAPPER}} .ova_time_countdown .countdown-section:not(:last-child) ' => 'margin-right: {{SIZE}}{{UNIT}};',
					],
				]
			);

			
		$this->end_controls_section();

		
	}
	protected function render() {
		$settings = $this->get_settings();
		$due_date = $settings['due_date'];
		$link = $settings['link_button'];
		$text_button = $settings['text_button'];
		$target = $settings['target'];

		$version = $settings['version'];
		?>
		<div class="ova_time_countdown <?php echo esc_attr( $version ) ?>">
			<div class="due_date" data-day="<?php echo $due_date; ?>"></div>
			<?php if( $version == 'version_1' ){ ?>
			<div class="ova-button">
				<a target="<?php echo esc_attr( $target ) ?>" class="second_font" href="<?php echo esc_url( $link ) ?>"><?php echo esc_html( $text_button ) ?></a>
			</div>
			<?php } ?>
		</div>
		<?php
	}
}