<?php
namespace Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Tags\Site_Tags;

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
use Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Groups\Softech_Site_Group;
use Elementor\Controls_Manager;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class Softech_Current_Date_Time extends Tag {

    public function get_name() {
        return 'softech-current-date-time';
    }

    public function get_title() {
        return esc_html__( 'Current Date Time', 'softech-addons-for-elementor' );
    }

    public function get_group() {
        return Softech_Site_Group::get_name();
    }

    public function get_categories() {
        return [ Module::TEXT_CATEGORY ];
    }

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

        /* ---------------- DATE FORMAT ---------------- */
        $this->add_control(
            'date_format',
            [
                'label'   => esc_html__( 'Date Format', 'softech-addons-for-elementor' ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'site',
                'options' => [
                    'site'   => esc_html__( 'Site Default', 'softech-addons-for-elementor' ),
                    'dmy'    => 'd M Y (29 Jan 2026)',
                    'mdy'    => 'M d, Y (Jan 29, 2026)',
                    'ymd'    => 'Y-m-d (2026-01-29)',
                    'custom' => esc_html__( 'Custom', 'softech-addons-for-elementor' ),
                ],
            ]
        );

        $this->add_control(
            'custom_date_format',
            [
                'label'     => esc_html__( 'Custom Date Format', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::TEXT,
                'default'   => 'd M Y',
                'condition' => [
                    'date_format' => 'custom',
                ],
            ]
        );

        /* ---------------- TIME FORMAT ---------------- */
        $this->add_control(
            'time_format',
            [
                'label'   => esc_html__( 'Time Format', 'softech-addons-for-elementor' ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'site',
                'options' => [
                    'site'   => esc_html__( 'Site Default', 'softech-addons-for-elementor' ),
                    '12'     => 'h:i A (11:45 AM)',
                    '24'     => 'H:i (23:45)',
                    '24s'    => 'H:i:s (23:45:10)',
                    'custom' => esc_html__( 'Custom', 'softech-addons-for-elementor' ),
                ],
            ]
        );

        $this->add_control(
            'custom_time_format',
            [
                'label'     => esc_html__( 'Custom Time Format', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::TEXT,
                'default'   => 'h:i A',
                'condition' => [
                    'time_format' => 'custom',
                ],
            ]
        );
        
        $this->add_control(
            'display_mode',
            [
                'label'   => esc_html__( 'Display Mode', 'softech-addons-for-elementor' ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'inline',
                'options' => [
                    'inline'  => esc_html__( 'Inline', 'softech-addons-for-elementor' ),
                    'stacked' => esc_html__( 'Stacked', 'softech-addons-for-elementor' ),
                  
                ],
            ]
        );
        
        $this->add_control(
            'separator',
            [
                'label'     => esc_html__( 'Separator', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::TEXT,
                'default'   => '/',
                'condition' => [
                    'display_mode' => 'inline',
                ],
            ]
        );
        

        /* ---------------- TIMEZONE ---------------- */
        $this->add_control(
            'timezone',
            [
                'label'   => esc_html__( 'Timezone', 'softech-addons-for-elementor' ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'site',
                'options' => $this->get_timezone_options(),
            ]
        );
        
        
    }
    /* ---------------- DISPLAY OPTIONS ---------------- */



    /**
     * Get timezone options
     */
    private function get_timezone_options() {

        $timezones = [
            'site' => esc_html__( 'Site Default', 'softech-addons-for-elementor' ),
            'UTC'  => 'UTC',
        ];

        foreach ( timezone_identifiers_list() as $tz ) {
            $timezones[ $tz ] = $tz;
        }

        return $timezones;
    }

    /**
     * Render output
     */
 public function render() {

    $settings = $this->get_settings_for_display();

    /* ---------- TIMEZONE ---------- */
    if ( $settings['timezone'] === 'site' ) {
        $timezone = wp_timezone();
    } else {
        $timezone = new \DateTimeZone( $settings['timezone'] );
    }

    $date_time = new \DateTime( 'now', $timezone );

    /* ---------- DATE FORMAT ---------- */
    switch ( $settings['date_format'] ) {
        case 'dmy':
            $date_format = 'd M Y';
            break;
        case 'mdy':
            $date_format = 'M d, Y';
            break;
        case 'ymd':
            $date_format = 'Y-m-d';
            break;
        case 'custom':
            $date_format = ! empty( $settings['custom_date_format'] )
                ? $settings['custom_date_format']
                : 'd M Y';
            break;
        case 'site':
        default:
            $date_format = get_option( 'date_format' );
    }

    /* ---------- TIME FORMAT ---------- */
    switch ( $settings['time_format'] ) {
        case '12':
            $time_format = 'h:i A';
            break;
        case '24':
            $time_format = 'H:i';
            break;
        case '24s':
            $time_format = 'H:i:s';
            break;
        case 'custom':
            $time_format = ! empty( $settings['custom_time_format'] )
                ? $settings['custom_time_format']
                : 'h:i A';
            break;
        case 'site':
        default:
            $time_format = get_option( 'time_format' );
    }

    /* ---------- FINAL VALUES ---------- */
    $date = $date_time->format( $date_format );
    $time = $date_time->format( $time_format );
    $sep = ' ' . $settings['separator'] . ' ';
    
    /* ---------- DISPLAY MODE ---------- */
    switch ( $settings['display_mode'] ) {

        case 'stacked':
            echo esc_html( $date ) . '<br>' . esc_html( $time );
            break;

      

        case 'inline':
        default:
            echo esc_html( $date . $sep . $time );
            break;
    }
}

}
