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

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

		wp_register_style( 'flipclock', plugin_dir_url( __DIR__ ) . 'assets/vendor/flipclock/flipclock.css', array(), '1.1a' );

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

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

	}

	public function get_script_depends() {

		wp_register_script( 'flipclock', plugin_dir_url( __DIR__ ) . 'assets/vendor/flipclock/flipclock.min.js', array( 'jquery' ), '1.1a', true );

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

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

	}

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

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

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

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

	protected function register_controls() {
		$this->start_controls_section(
			'section_flipclock',
			[
				'label' => __( 'FlipClock', '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' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'clockface',
			[
				'label'   => __( 'Clock Face', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'DailyCounter',
				'options' => [
					'DailyCounter'  => __( 'Days, Hours, Minutes', 'ignition-elementor-widgets' ),
					'HourlyCounter' => __( 'Hours, Minutes', 'ignition-elementor-widgets' ),
					'MinuteCounter' => __( 'Minutes', 'ignition-elementor-widgets' ),
				],
			]
		);

		$this->add_control(
			'toggle_seconds',
			[
				'label'        => __( 'Toggle Seconds', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'description'  => __( 'Hides Seconds. Only available when days are visible.', 'ignition-elementor-widgets' ),
				'label_on'     => __( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => [
					'clockface' => 'DailyCounter',
				],
			]
		);

		$this->add_control(
			'align',
			[
				'label'        => __( 'Alignment', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::CHOOSE,
				'options'      => [
					'left'   => [
						'title' => __( 'Left', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-left',
					],
					'center' => [
						'title' => __( 'Center', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-center',
					],
					'right'  => [
						'title' => __( 'Right', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-right',
					],
				],
				'prefix_class' => 'ignition-flipclock-align-',
				'default'      => '',
			]
		);

		$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'   => '#fff',
				'selectors' => [
					'{{WRAPPER}} div.ignition-flipclock-container' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			[
				'name'        => 'border',
				'label'       => __( 'Border', 'ignition-elementor-widgets' ),
				'placeholder' => '1px',
				'default'     => '1px',
				'selector'    => '{{WRAPPER}} .ignition-flipclock-container',
			]
		);

		$this->add_control(
			'border_radius',
			[
				'label'      => __( 'Border Radius', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} div.ignition-flipclock-container' => '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-flipclock-container',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'typography_style',
			[
				'label' => __( 'Label Typography', '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}} span.flip-clock-label',
			]
		);

		$this->add_control(
			'label_text_color',
			[
				'label'     => __( 'Text Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#000',
				'selectors' => [
					'{{WRAPPER}} span.flip-clock-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}} span.flip-clock-label' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$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'   => '#ccc',
				'selectors' => [
					'{{WRAPPER}} .flip-clock-wrapper ul li a div div.inn' => 'color: {{VALUE}};',
				],
				'separator' => 'before',
			]
		);

		$this->add_control(
			'digit_bg_color',
			[
				'label'     => __( 'Clock Background Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#333',
				'selectors' => [
					'{{WRAPPER}} .flip-clock-wrapper ul li a div div.inn' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'flipcard_separator',
			[
				'label'     => __( 'Clock Flipcard Separator Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => 'rgba(0, 0, 0, 0.4)',
				'selectors' => [
					'{{WRAPPER}} .flip-clock-wrapper ul li a div.up:after' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'clock_separator',
			[
				'label'     => __( 'Clock Separator Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#323434',
				'selectors' => [
					'{{WRAPPER}} .flip-clock-dot' => 'background: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'timeout_typography_style',
			[
				'label' => __( 'Timeout Text Typography', '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,
				'selectors' => [
					'{{WRAPPER}} div.message',
					'{{WRAPPER}} p.expired',
				]
			]
		);

		$this->add_control(
			'timeout_text_color',
			[
				'label'     => __( 'Text Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#000',
				'selectors' => [
					'{{WRAPPER}} div.message' => 'color: {{VALUE}};',
					'{{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}} div.message' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'{{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  = strtotime( $settings['time_out'] );
		$clockface = $settings['clockface'];

		if ( false === $time_out ) {
			return;
		}

		$label = 'yes' === $settings['toggle_labels'] ? '' : 'no-label';
		$now   = current_time( 'timestamp' );
		$diff  = $time_out - $now;

		// 'MinuteCounter' face doesn't play nice with anything more than 99:59, so fallback to Hourly counter instead.
		if ( 'MinuteCounter' === $clockface && $diff >= 6000 ) {
			$clockface = 'HourlyCounter';
		}
		?>
		<div id="flipclock-<?php echo esc_attr( $this->get_id() ); ?>" class="ignition-flipclock-container" data-time="<?php echo intval( $diff ); ?>" data-end-text="<?php echo esc_attr( $settings['end_text'] ); ?>" data-clockface="<?php echo esc_attr( $clockface ); ?>" data-seconds="<?php echo esc_attr( $settings['toggle_seconds'] ); ?>">
			<?php if ( $diff > 0 ) { ?>
				<div class="clock <?php echo esc_attr( $label ); ?>"></div>
				<div class="message"></div>
			<?php } else { ?>
				<p class="expired"><?php echo esc_html( $settings['end_text'] ); ?></p>
			<?php } ?>
		</div>
		<?php
	}

	protected function content_template() {}
}
