<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Addons_Widget_Countdown extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-countdown',
			'title'          => 'Sifency Countdown',
			'icon'           => 'eicon-countdown',
			'category'       => 'sifency-business-widgets',
			'keywords'       => array( 'countdown', 'timer', 'date', 'time' ),
			'style_depends'  => array( 'sifency-countdown-style', 'sifency-typo-style' ),
			'script_depends' => array( 'sifency-countdown-script' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'due_date',
			array(
				'label'       => esc_html__( 'Due Date', 'sifency-addons' ),
				'type'        => Controls_Manager::DATE_TIME,
				'default'     => wp_date( 'Y-m-d H:i', strtotime( '+1 month' ) ),
				'description' => esc_html__( 'Set the date and time to count down to.', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'View Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'square',
				'options' => array(
					'square'  => esc_html__( 'Square Blocks', 'sifency-addons' ),
					'circle'  => esc_html__( 'Modern Circles', 'sifency-addons' ),
					'minimal' => esc_html__( 'Minimal (Text Only)', 'sifency-addons' ),
					'card'    => esc_html__( 'Card Style', 'sifency-addons' ),
				),
			)
		);

		// Labels
		$this->add_control(
			'hr_labels',
			array(
				'type'  => Controls_Manager::DIVIDER,
				'label' => 'Labels',
			)
		);

		$this->add_control(
			'show_labels',
			array(
				'label'   => esc_html__( 'Show Labels', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->add_control(
			'label_days',
			array(
				'label'     => esc_html__( 'Days', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Days',
				'condition' => array( 'show_labels' => 'yes' ),
			)
		);
		$this->add_control(
			'label_hours',
			array(
				'label'     => esc_html__( 'Hours', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Hours',
				'condition' => array( 'show_labels' => 'yes' ),
			)
		);
		$this->add_control(
			'label_minutes',
			array(
				'label'     => esc_html__( 'Minutes', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Minutes',
				'condition' => array( 'show_labels' => 'yes' ),
			)
		);
		$this->add_control(
			'label_seconds',
			array(
				'label'     => esc_html__( 'Seconds', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Seconds',
				'condition' => array( 'show_labels' => 'yes' ),
			)
		);

		$this->add_control( 'hr_expire', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'expire_action',
			array(
				'label'   => esc_html__( 'Action After Expire', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'message',
				'options' => array(
					'message'  => esc_html__( 'Show Message', 'sifency-addons' ),
					'hide'     => esc_html__( 'Hide Timer', 'sifency-addons' ),
					'redirect' => esc_html__( 'Redirect URL', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'expire_message',
			array(
				'label'     => esc_html__( 'Message', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => '<p>The event has started!</p>',
				'condition' => array( 'expire_action' => 'message' ),
			)
		);
		$this->add_control(
			'expire_redirect_url',
			array(
				'label'       => esc_html__( 'Redirect URL', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'https://...',
				'condition'   => array( 'expire_action' => 'redirect' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_boxes',
			array(
				'label' => esc_html__( 'Boxes / Container', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'container_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-countdown' => 'justify-content: {{VALUE}};' ),
			)
		);

		$this->add_responsive_control(
			'box_gap',
			array(
				'label'     => esc_html__( 'Gap Between Items', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-countdown' => 'gap: {{SIZE}}px;' ),
			)
		);

		$this->add_responsive_control(
			'box_width',
			array(
				'label'     => esc_html__( 'Item Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 50,
						'max' => 300,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-countdown-item' => 'width: {{SIZE}}px; height: {{SIZE}}px;' ),
				'condition' => array( 'layout_type!' => 'minimal' ),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'      => 'box_bg',
				'selector'  => '{{WRAPPER}} .sifency-countdown-item',
				'condition' => array( 'layout_type!' => 'minimal' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'      => 'box_border',
				'selector'  => '{{WRAPPER}} .sifency-countdown-item',
				'condition' => array( 'layout_type!' => 'minimal' ),
			)
		);
		$this->add_responsive_control(
			'box_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-countdown-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'      => 'box_shadow',
				'selector'  => '{{WRAPPER}} .sifency-countdown-item',
				'condition' => array( 'layout_type!' => 'minimal' ),
			)
		);

		$this->end_controls_section();

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'number_style',
				'label'    => 'Digits',
				'selector' => '.sifency-number-style',
				'exclude'  => array( 'shadow', 'stroke', 'transition', 'tabs' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'label_style',
				'label'      => 'label',
				'selector'   => '.sifency-label-style',
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs' ),
				'color_only' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Message Text',
				'selector'   => '.sifency-content-style',
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
				'color_only' => true,
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id              = esc_attr( $context['dom_id'] );
		$settings                = $this->get_settings_for_display();
		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-countdown-wrapper' );

		$this->add_render_attribute( 'counter', 'class', 'sifency-countdown' );
		$this->add_render_attribute( 'counter', 'class', 'sifency-cd-layout-' . $settings['layout_type'] );

		$countdown_settings = array(
			'date'         => $settings['due_date'],
			'action'       => $settings['expire_action'],
			'redirect_url' => $settings['expire_redirect_url'],
		);
		$this->add_render_attribute( 'counter', 'data-cd-settings', wp_json_encode( $countdown_settings ) );

		$items = array(
			'days'    => $settings['label_days'],
			'hours'   => $settings['label_hours'],
			'minutes' => $settings['label_minutes'],
			'seconds' => $settings['label_seconds'],
		);

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_wrapper_attrs ); ?>>
			
			<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'counter' ) ); ?>>
				<?php
				foreach ( $items as $key => $label ) :
					$animation_item_attrs = $animation_manager->get_item_attributes();
					?>
				<div class="sifency-countdown-item sifency-cd-item-<?php echo esc_attr( $key ); ?>" <?php echo wp_kses_post( $animation_item_attrs ); ?>>
					<div class="sifency-cd-digit sifency-number-style" data-unit="<?php echo esc_attr( $key ); ?>">00</div>
					<?php if ( 'yes' === $settings['show_labels'] ) : ?>
						<div class="sifency-cd-label sifency-label-style"><?php echo esc_html( $label ); ?></div>
					<?php endif; ?>
				</div>
				<?php endforeach; ?>
			</div>

			<?php if ( 'message' === $settings['expire_action'] ) : ?>
				<div class="sifency-countdown-expire-msg sifency-content-style" style="display:none;">
					<?php echo wp_kses_post( $settings['expire_message'] ); ?>
				</div>
			<?php endif; ?>

		</div>
		<?php
	}
}
