<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Timezone_Clock extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-timezone-clock',
			'title'          => 'Sifency Global Digital Clock',
			'icon'           => 'eicon-clock-o',
			'category'       => array( 'sifency-business-widgets' ),
			'script_depends' => array( 'sifency-clock-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'clock', 'time', 'digital', 'timezone', 'world' ),
		);
	}

	protected function register_controls() {
		// --- Content Section ---
		$this->start_controls_section(
			'section_clocks',
			array( 'label' => esc_html__( 'Timezone Clocks', 'sifency-addons' ) )
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'city_name',
			array(
				'label'       => esc_html__( 'City Label', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'New York', 'sifency-addons' ),
				'label_block' => true,
			)
		);

		// Predefined popular timezones + Option to type manually
		$repeater->add_control(
			'timezone',
			array(
				'label'   => esc_html__( 'Timezone', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'America/New_York',
				'options' => array(
					'America/New_York'    => 'New York (EST/EDT)',
					'Europe/London'       => 'London (GMT/BST)',
					'Asia/Tokyo'          => 'Tokyo (JST)',
					'Australia/Sydney'    => 'Sydney (AEST/AEDT)',
					'Europe/Paris'        => 'Paris (CET/CEST)',
					'Asia/Dubai'          => 'Dubai (GST)',
					'Asia/Dhaka'          => 'Dhaka (BST)',
					'America/Los_Angeles' => 'Los Angeles (PST/PDT)',
					'custom'              => 'Custom Timezone (Type Below)',
				),
			)
		);

		$repeater->add_control(
			'custom_timezone',
			array(
				'label'       => esc_html__( 'Custom Timezone ID', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'e.g., Europe/Berlin',
				'description' => 'Find valid TZ IDs at: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones',
				'condition'   => array( 'timezone' => 'custom' ),
			)
		);

		$this->add_control(
			'clock_items',
			array(
				'label'       => esc_html__( 'Clocks', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'city_name' => 'Tokyo',
						'timezone'  => 'Asia/Tokyo',
					),
					array(
						'city_name' => 'London',
						'timezone'  => 'Europe/London',
					),
					array(
						'city_name' => 'New York',
						'timezone'  => 'America/New_York',
					),
				),
				'title_field' => '{{{ city_name }}}',
			)
		);

		$this->end_controls_section();

		// --- Settings Section ---
		$this->start_controls_section(
			'section_format',
			array( 'label' => esc_html__( 'Time Format', 'sifency-addons' ) )
		);

		$this->add_control(
			'time_format',
			array(
				'label'   => esc_html__( '12/24 Hour Format', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => '12',
				'options' => array(
					'12' => '12-Hour (AM/PM)',
					'24' => '24-Hour (Military)',
				),
			)
		);

		$this->add_control(
			'show_seconds',
			array(
				'label'       => esc_html__( 'Show Seconds', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => 'Shows a blinking seconds counter.',
			)
		);

		$this->add_control(
			'blink_colon',
			array(
				'label'       => esc_html__( 'Blinking Colon (:)', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'no',
				'description' => 'Makes the colon between hours and minutes blink every second.',
			)
		);

		$this->end_controls_section();

		// --- Style Section ---
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Typography & Layout', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'alignment',
			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',
					),
				),
				'default'   => 'center',
				'selectors' => array( '{{WRAPPER}} .sifency-clock-wrapper' => 'justify-content: {{VALUE}};' ),
			)
		);

		$this->add_responsive_control(
			'gap',
			array(
				'label'      => esc_html__( 'Spacing Between Clocks', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em', 'vw' ),
				'default'    => array(
					'size' => 30,
					'unit' => 'px',
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-clock-wrapper' => 'gap: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_control(
			'heading_city_style',
			array(
				'label'     => esc_html__( 'City Name Style', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'city_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#888888',
				'selectors' => array( '{{WRAPPER}} .sifency-clock-city' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'city_typography',
				'selector' => '{{WRAPPER}} .sifency-clock-city',
			)
		);

		$this->add_control(
			'heading_time_style',
			array(
				'label'     => esc_html__( 'Time Style', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'time_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#111111',
				'selectors' => array( '{{WRAPPER}} .sifency-clock-time' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'time_typography',
				'selector' => '{{WRAPPER}} .sifency-clock-time',
			)
		);

		$this->end_controls_section();
	}

	protected function render_dom( array $context ) {
		$settings   = $this->get_settings_for_display();
		$wrapper_id = esc_attr( $context['dom_id'] );
		$clocks     = $settings['clock_items'];

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

		$format_12 = $settings['time_format'] === '12' ? 'true' : 'false';
		$show_sec  = $settings['show_seconds'] === 'yes' ? 'true' : 'false';

		// Add class for blinking colon if enabled
		$blink_class = $settings['blink_colon'] === 'yes' ? 'sifency-blink-active' : '';

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-clock-wrapper <?php echo esc_attr( $blink_class ); ?>" 
			data-12hr="<?php echo esc_attr( $format_12 ); ?>" 
			data-sec="<?php echo esc_attr( $show_sec ); ?>">
			
			<?php
			foreach ( $clocks as $clock ) :
				// Determine final timezone string
				$tz = $clock['timezone'] === 'custom' ? trim( $clock['custom_timezone'] ) : $clock['timezone'];
				?>
				<div class="sifency-clock-item elementor-repeater-item-<?php echo esc_attr( $clock['_id'] ); ?>" data-tz="<?php echo esc_attr( $tz ); ?>">
					
					<span class="sifency-clock-city"><?php echo esc_html( $clock['city_name'] ); ?></span>
					
					<span class="sifency-clock-time">
						<span class="sifency-c-hour">00</span><span class="sifency-c-colon">:</span><span class="sifency-c-min">00</span>
						
						<?php if ( $settings['show_seconds'] === 'yes' ) : ?>
							<span class="sifency-c-colon sifency-sec-colon">:</span><span class="sifency-c-sec">00</span>
						<?php endif; ?>
						
						<?php if ( $settings['time_format'] === '12' ) : ?>
							<span class="sifency-c-ampm">AM</span>
						<?php endif; ?>
					</span>

				</div>
			<?php endforeach; ?>

		</div>
		<?php
	}
}
