<?php
/**
 * Event countdown SiteOrigin widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Event Countdown
Description: Fixed-date countdown to an event — set end date and time, drop on any page.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'SiteOrigin_Widget', false ) ) {
	return;
}

/**
 * Rsmspark_Content_Widgets_Event_Countdown_Widget
 */
class Rsmspark_Content_Widgets_Event_Countdown_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'event-countdown',
			__( 'RSM Event Countdown', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Count down to a fixed date and time with days, hours, minutes, and seconds.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-clock',
				'has_preview' => true,
				'keywords'    => array( 'countdown', 'timer', 'event', 'deadline', 'RSM' ),
			),
			array(),
			array(),
			plugin_dir_path( __FILE__ )
		);
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		return Rsmspark_Content_Widgets_SiteOrigin::prepend_import_export_form(
			'event_countdown',
			array(
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Countdown', 'rsm-content-widgets' ),
				'fields' => array(
					'title' => array(
						'type'  => 'text',
						'label' => __( 'Title (optional)', 'rsm-content-widgets' ),
					),
					'end_date' => array(
						'type'        => 'text',
						'label'       => __( 'End date', 'rsm-content-widgets' ),
						'description' => __( 'Format: YYYY-MM-DD (site timezone).', 'rsm-content-widgets' ),
					),
					'end_time' => array(
						'type'        => 'text',
						'label'       => __( 'End time', 'rsm-content-widgets' ),
						'default'     => '00:00:00',
						'description' => __( 'Format: HH:MM or HH:MM:SS (site timezone).', 'rsm-content-widgets' ),
					),
					'expired_message' => array(
						'type'        => 'text',
						'label'       => __( 'Expired message', 'rsm-content-widgets' ),
						'default'     => __( 'Countdown expired', 'rsm-content-widgets' ),
						'description' => __( 'Shown when the target date/time has passed (unless hidden).', 'rsm-content-widgets' ),
					),
					'hide_when_expired' => array(
						'type'    => 'checkbox',
						'label'   => __( 'Hide widget when expired', 'rsm-content-widgets' ),
						'default' => false,
					),
				),
			),
			'appearance' => array(
				'type'   => 'section',
				'label'  => __( 'Appearance', 'rsm-content-widgets' ),
				'fields' => array(
					'background_color' => array(
						'type'  => 'color',
						'label' => __( 'Background color', 'rsm-content-widgets' ),
					),
					'text_color' => array(
						'type'  => 'color',
						'label' => __( 'Countdown text color', 'rsm-content-widgets' ),
					),
					'title_color' => array(
						'type'        => 'color',
						'label'       => __( 'Title color', 'rsm-content-widgets' ),
						'description' => __( 'Headline above the timer. Empty inherits the countdown text color.', 'rsm-content-widgets' ),
					),
				),
			),
		)
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_dir( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_style_name( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_name( $instance ) {
		unset( $instance );
		return 'event-countdown.tpl';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @param array<string, mixed> $args     Args.
	 * @return array<string, mixed>
	 */
	public function get_template_variables( $instance, $args ) {
		unset( $args );
		$data = Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'content' );
		$data['appearance'] = Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'appearance' );

		return Rsmspark_Content_Widgets_Widget_Event_Countdown::prepare_template_vars(
			Rsmspark_Content_Widgets_Widget_Event_Countdown::prepare_from_instance( $data )
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_admin_scripts() {
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_frontend_scripts( $instance ) {
		unset( $instance );
		Rsmspark_Content_Widgets_Assets::enqueue_for_widget( Rsmspark_Content_Widgets_Widget_Event_Countdown::REGISTRY_ID );
	}
}

if ( function_exists( 'siteorigin_widget_register' ) ) {
	siteorigin_widget_register( 'event-countdown', __FILE__, 'Rsmspark_Content_Widgets_Event_Countdown_Widget' );
}
