<?php
namespace CSSIgniter\Ignition\Elementor\Widget;

class Countdown extends \Elementor\Widget_Base {
	public function get_style_depends() {

		wp_register_style( 'ignition-elementor-countdown', plugin_dir_url( __DIR__ ) . 'assets/css/countdown.min.css', array(), IGNITION_ELEMENTOR_WIDGETS_VERSION );

		return [
			'ignition-elementor-countdown',
		];

	}

	public function get_script_depends() {

		wp_register_script( 'ignition-elementor-countdown', plugin_dir_url( __DIR__ ) . 'assets/js/countdown.min.js', array( 'jquery' ), IGNITION_ELEMENTOR_WIDGETS_VERSION, true );

		return [
			'ignition-elementor-countdown',
		];

	}

	public function get_name() {
		return 'ignition-countdown';
	}

	public function get_title() {
		return __( 'CountDown', 'ignition-elementor-widgets' );
	}

	public function get_icon() {
		return 'eicon-countdown';
	}

	public function get_categories() {
		return [ 'ignition-elementor-widgets' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_countdown',
			[
				'label' => __( 'CountDown', 'ignition-elementor-widgets' ),
			]
		);

		$this->add_control(
			'time_out',
			[
				'label' => __( 'Time out', 'ignition-elementor-widgets' ),
				'type'  => \Elementor\Controls_Manager::DATE_TIME,
			]
		);

		$this->add_control(
			'end_text',
			[
				'label'       => __( 'Countdown over text', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => __( 'The countdown is over!', 'ignition-elementor-widgets' ),
				'placeholder' => __( 'Text displayed after countdown ends.', 'ignition-elementor-widgets' ),
			]
		);

		$this->add_control(
			'toggle_labels',
			[
				'label'        => __( 'Toggle Labels', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'show',
				'default'      => 'show',
			]
		);

		$this->add_control(
			'toggle_days',
			[
				'label'        => __( 'Toggle Days', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'show',
				'default'      => 'show',
			]
		);

		$this->add_control(
			'toggle_hours',
			[
				'label'        => __( 'Toggle Hours', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'show',
				'default'      => 'show',
			]
		);

		$this->add_control(
			'toggle_minutes',
			[
				'label'        => __( 'Toggle Minutes', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'show',
				'default'      => 'show',
			]
		);

		$this->add_control(
			'toggle_seconds',
			[
				'label'        => __( 'Toggle Seconds', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'show',
				'default'      => 'show',
			]
		);

		$this->add_control(
			'view',
			[
				'label'   => __( 'View', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::HIDDEN,
				'default' => 'traditional',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'module_style',
			[
				'label' => __( 'Module Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'background_color',
			[
				'label'     => __( 'Background Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'scheme'    => [
					'type'  => \Elementor\Core\Schemes\Color::get_type(),
					'value' => \Elementor\Core\Schemes\Color::COLOR_4,
				],
				'default'   => '#000',
				'selectors' => [
					'{{WRAPPER}} div.ignition-elementor-countdown-item' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			[
				'name'     => 'border',
				'label'    => __( 'Border', 'ignition-elementor-widgets' ),
				'selector' => '{{WRAPPER}} .ignition-elementor-countdown-item',
			]
		);

		$this->add_control(
			'border_radius',
			[
				'label'      => __( 'Border Radius', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} div.ignition-elementor-countdown-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'label_box_shadow',
				'selector' => '{{WRAPPER}} .ignition-elementor-countdown-item',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'digit_style',
			[
				'label' => __( 'Digit Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'digit_color',
			[
				'label'     => __( 'Clock Digit Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#fff',
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-countdown-number' => 'color: {{VALUE}};',
				],
				'separator' => 'before',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'     => 'digit_typography',
				'label'    => __( 'Typography', 'ignition-elementor-widgets' ),
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_4,
				'selector' => '{{WRAPPER}} p.ignition-elementor-countdown-number',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'typography_style',
			[
				'label' => __( 'Label Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'     => 'typography',
				'label'    => __( 'Typography', 'ignition-elementor-widgets' ),
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_4,
				'selector' => '{{WRAPPER}} p.ignition-elementor-countdown-label',
			]
		);

		$this->add_control(
			'label_text_color',
			[
				'label'     => __( 'Text Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#fff',
				'selectors' => [
					'{{WRAPPER}} p.ignition-elementor-countdown-label' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'text_padding',
			[
				'label'      => __( 'Text Padding', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors'  => [
					'{{WRAPPER}} p.ignition-elementor-countdown-label' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'timeout_typography_style',
			[
				'label' => __( 'Timeout Text Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'      => 'timeout_typography',
				'label'     => __( 'Typography', 'ignition-elementor-widgets' ),
				'scheme'    => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_4,
				'selector' => '{{WRAPPER}} p.expired',
			]
		);

		$this->add_control(
			'timeout_text_color',
			[
				'label'     => __( 'Text Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#000',
				'selectors' => [
					'{{WRAPPER}} p.expired'   => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'timeout_text_padding',
			[
				'label'      => __( 'Text Padding', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors'  => [
					'{{WRAPPER}} p.expired'   => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings();
		$time_out = str_replace( ' ', 'T', $settings['time_out'] );

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

		$diff         = strtotime( $settings['time_out'] ) - current_time( 'timestamp' );
		$expired_text = $settings['end_text'];
		$label        = $settings['toggle_labels'] ? true : false;
		$days         = $settings['toggle_days'] ? true : false;
		$hours        = $settings['toggle_hours'] ? true : false;
		$minutes      = $settings['toggle_minutes'] ? true : false;
		$seconds      = $settings['toggle_seconds'] ? true : false;

		?>
		<div class="ignition-elementor-countdown" data-date="<?php echo esc_attr( $time_out ); ?>">
			<div class="ignition-elementor-countdown-wrap">
				<?php if ( $diff > 0 ) { ?>
					<?php if ( $days ) : ?>
						<div class="ignition-elementor-countdown-item">
							<p class="ignition-elementor-countdown-number ignition-elementor-countdown-days"></p>
							<?php if ( true === $label ) : ?>
								<p class="ignition-elementor-countdown-label ignition-elementor-countdown-label-days"><?php esc_html_e( 'Days', 'ignition-elementor-widgets' ); ?></p>
							<?php endif; ?>
						</div>
					<?php endif; ?>
					<?php if ( $hours ) : ?>
						<div class="ignition-elementor-countdown-item">
							<p class="ignition-elementor-countdown-number ignition-elementor-countdown-hours"></p>
							<?php if ( true === $label ) : ?>
								<p class="ignition-elementor-countdown-label ignition-elementor-countdown-label-hours"><?php esc_html_e( 'Hours', 'ignition-elementor-widgets' ); ?></p>
							<?php endif; ?>
						</div>
					<?php endif; ?>
					<?php if ( $minutes ) : ?>
						<div class="ignition-elementor-countdown-item">
							<p class="ignition-elementor-countdown-number ignition-elementor-countdown-minutes"></p>
							<?php if ( true === $label ) : ?>
								<p class="ignition-elementor-countdown-label ignition-elementor-countdown-label-minutes"><?php esc_html_e( 'Minutes', 'ignition-elementor-widgets' ); ?></p>
							<?php endif; ?>
						</div>
					<?php endif; ?>
					<?php if ( $seconds ) : ?>
						<div class="ignition-elementor-countdown-item">
							<p class="ignition-elementor-countdown-number ignition-elementor-countdown-seconds"></p>
							<?php if ( true === $label ) : ?>
								<p class="ignition-elementor-countdown-label ignition-elementor-countdown-label-seconds"><?php esc_html_e( 'Seconds', 'ignition-elementor-widgets' ); ?></p>
							<?php endif; ?>
						</div>
					<?php endif; ?>
				<?php } else { ?>
					<p class="expired"><?php echo esc_html( $expired_text ); ?></p>
				<?php } ?>
			</div>
		</div>
		<?php
	}

	protected function content_template() {}
}
