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

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

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

class Softech_Post_Time extends Tag {
    
    public function get_name() {
        return 'softech-post-time';
    }
    
    public function get_title() {
        return esc_html__( 'Post Time', 'softech-addons-for-elementor' );
    }
    
    public function get_group() {
        return Softech_Post_Group::get_name();
    }
    
    public function get_categories() {
        return [ Module::TEXT_CATEGORY ];
    }
    
    protected function register_controls() {
        $this->add_control(
            'time_format',
            [
                'label' => esc_html__( 'Time Format', 'softech-addons-for-elementor' ),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'default' => esc_html__( 'Default', 'softech-addons-for-elementor' ),
                    'g:i a'   => date_i18n( 'g:i a' ),
                    'g:i A'   => date_i18n( 'g:i A' ),
                    'H:i'     => date_i18n( 'H:i' ),
                    'custom'  => esc_html__( 'Custom', 'softech-addons-for-elementor' ),
                ],
                'default' => 'default',
            ]
        );
        
        $this->add_control(
            'custom_format',
            [
                'label' => esc_html__( 'Custom Format', 'softech-addons-for-elementor' ),
                'type' => Controls_Manager::TEXT,
                'placeholder' => 'e.g. H:i:s',
                'condition' => [
                    'time_format' => 'custom',
                ],
            ]
        );
    }
    
    public function render() {
        $format = $this->get_settings( 'time_format' );
        $custom = $this->get_settings( 'custom_format' );
        
        if ( $format === 'default' ) {
            echo esc_html( get_the_time() );
        } elseif ( $format === 'custom' && ! empty( $custom ) ) {
            echo esc_html( get_the_time( $custom ) );
        } else {
            echo esc_html( get_the_time( $format ) );
        }
    }
}