<?php

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

use Elementor\Widget_Base;
use Elementor\Icons_Manager;

class Banae_Count_Down extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'banae_count_down';
	}

	/**
	 * Get widget title.
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return __( 'Count Down', 'banana-addons-for-elementor' );
	}

	/**
	 * Get widget icon.
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-countdown';
	}

	/**
	 * Get widget categories.
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'banana-addons-category' ];
	}

	/**
	 * Get widget keywords for search
	 * 
	 * @return string[]
	 */
	public function get_keywords() {
		return [ 'count', 'counter', 'watch', 'clock' ];
	}

	/**
	 * Enqueue dependend styles
	 * 
	 * Retrieve the list of style dependencies the widget requires.
	 * 
	 * @return string[]
	 */
	public function get_style_depends() {
		return [ 'banae-countdown-style' ];
	}

	/**
	 * Enqueue dependend script
	 * 
	 * Retrieve the list of style dependencies the widget requires.
	 * 
	 * @return string[]
	 */
	public function get_script_depends() {
		return [ 'jquery', 'banae-vendor-countdown-js', 'banae-countdown-js' ];
	}

	protected function is_dynamic_content(): bool {
		return false;
	}

	/**
	 * Register widget content controls
	 */
	protected function register_controls() {

		// Instantiate the control class
		require_once 'register-controls.php';

		// Call to the register controls method
		Banae_Count_Down_Control::add_register_controls( $this );

	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$end_active_type = ( isset( $settings['end_action_type'] ) && $settings['end_action_type'] ) ? esc_html( $settings['end_action_type'] ) : '';

		$event_type = [
			'event_type' => $end_active_type,
			'event_message' => ( $settings['end_action_message'] ) ? esc_html( $settings['end_action_message'] ) : 'Count Down Ended!',
			'url' => ( ! empty( $settings['end_action_url']['url'] ) ) ? esc_html( $settings['end_action_url']['url'] ) : 'https://example.com',
			'is_external' => ( ! empty( $settings['end_action_url']['is_external'] ) ) ? esc_html( $settings['end_action_url']['is_external'] ) : false,
		];

		if ( isset( $settings['cb_datetime'] ) && $settings['cb_datetime'] ) {
			$isoDate = ( new DateTime( esc_attr( $settings['cb_datetime'] ) ) )->format( DateTime::W3C ); // "2024-05-04T10:00:00+00:00"
		} else {
			$isoDate = ( new DateTime( 'now' ) )->format( DateTime::W3C ); // "2024-05-04T10:00:00+00:00"
		}

		?>
<div class="banae-countdown" data-target-date="<?php echo esc_attr( $isoDate ); ?>"
    data-action='<?php echo json_encode( $event_type ); ?>'>
    <div class="banae-countdown-item banae-tm-days">
        <div class="banae-cd-number banae-cd-days">00</div>
        <?php if ( isset( $settings['show_label_days'] ) && $settings['show_label_days'] === 'yes' ) { ?>
        <div class="banae-cd-label">
            <?php echo ( ! empty( $settings['label_days'] ) ) ? $settings['label_days'] : esc_html_e( 'Days', 'banana-addons-for-elementor' ); ?>
        </div>
        <?php } ?>
    </div>
    <div class="banae-countdown-item banae-tm-hours">
        <div class="banae-cd-number banae-cd-hours">00</div>
        <?php if ( isset( $settings['show_label_hours'] ) && $settings['show_label_hours'] === 'yes' ) { ?>
        <div class="banae-cd-label">
            <?php echo ( ! empty( $settings['label_hours'] ) ) ? $settings['label_hours'] : esc_html_e( 'Hours', 'banana-addons-for-elementor' ); ?>
        </div>
        <?php } ?>
    </div>
    <div class="banae-countdown-item banae-tm-minutes">
        <div class="banae-cd-number banae-cd-minutes">00</div>
        <?php if ( isset( $settings['show_label_minutes'] ) && $settings['show_label_minutes'] === 'yes' ) { ?>
        <div class="banae-cd-label">
            <?php echo ( ! empty( $settings['label_minutes'] ) ) ? $settings['label_minutes'] : esc_html_e( 'Minutes', 'banana-addons-for-elementor' ); ?>
        </div>
        <?php } ?>
    </div>
    <div class="banae-countdown-item banae-tm-seconds">
        <div class="banae-cd-number banae-cd-seconds">00</div>
        <?php if ( isset( $settings['show_label_seconds'] ) && $settings['show_label_seconds'] === 'yes' ) { ?>
        <div class="banae-cd-label">
            <?php echo ( ! empty( $settings['label_seconds'] ) ) ? $settings['label_seconds'] : esc_html_e( 'Seconds', 'banana-addons-for-elementor' ); ?>
        </div>
        <?php } ?>
    </div>
</div>
<?php
	}
}