<?php
namespace Sifency\Traits;

if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Group_Control_Typography;
use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
trait Sifency_Addons_Themebuilder_Controls {
	use Sifency_Addons_Animation_Controls;

	/**
	 * Combined Theme Builder Controls
	 *
	 * @param array $args {
	 *     @type string $section_label     Custom label for the content section.
	 *     @type string $placeholder_text  Default HTML content for the placeholder.
	 *     @type bool   $show_placeholder  Whether to show or hide the placeholder section.
	 * }
	 */
	protected function add_sifency_addons_themebuilder_controls( $args = array() ) {
		$defaults = array(
			'section_label'     => esc_html__( 'Content Settings', 'sifency-addons' ),
			'placeholder_text'  => 'Your dynamic content will appear here. Please set up the dynamic source to see the content. HTML is allowed.',
			'show_placeholder'  => true,
			'show_date_section' => false,
			'show_time_section' => false,
		);
		$args     = wp_parse_args( $args, $defaults );
		if ( true === $args['show_placeholder'] ) {
			$this->start_controls_section(
				'section_content',
				array( 'label' => $args['section_label'] )
			);
			$this->add_control(
				'placeholder_text',
				array(
					'label'       => esc_html__( 'Placeholder', 'sifency-addons' ),
					'type'        => Controls_Manager::TEXTAREA,
					'default'     => $args['placeholder_text'],
					'description' => esc_html__( 'This content will be shown in the editor when the dynamic field is empty. HTML is allowed.', 'sifency-addons' ),
				)
			);
			$this->end_controls_section();
		}
		if ( true === $args['show_date_section'] ) {
			$this->start_controls_section( 'section_content_date', array( 'label' => $args['section_label'] ) );
			$this->add_control(
				'date_format',
				array(
					'label'   => esc_html__( 'Date Format', 'sifency-addons' ),
					'type'    => Controls_Manager::SELECT,
					'options' => array(
						'F j, Y' => wp_date( 'F j, Y' ),
						'Y-m-d'  => wp_date( 'Y-m-d' ),
						'm/d/Y'  => wp_date( 'm/d/Y' ),
						'd/m/Y'  => wp_date( 'd/m/Y' ),
						'D, M j' => wp_date( 'D, M j' ),
						'custom' => esc_html__( 'Custom', 'sifency-addons' ),
					),
					'default' => 'F j, Y',
				)
			);
			$this->add_control(
				'date_format_custom',
				array(
					'label'       => esc_html__( 'Custom Date Format', 'sifency-addons' ),
					'type'        => Controls_Manager::TEXT,
					'default'     => 'F j, Y',
					'description' => sprintf( '<a href="%s" target="_blank">%s</a>', 'https://wordpress.org/support/article/formatting-date-and-time/', esc_html__( 'Documentation on date and time formatting.', 'sifency-addons' ) ),
					'condition'   => array( 'date_format' => 'custom' ),
				)
			);

			$this->add_control(
				'placeholder_text',
				array(
					'label'       => esc_html__( 'Placeholder', 'sifency-addons' ),
					'type'        => Controls_Manager::TEXT,
					'default'     => wp_date( 'F j, Y' ),
					'description' => esc_html__( 'This text will be shown in the editor when the dynamic field is empty.', 'sifency-addons' ),
				)
			);

			$this->end_controls_section();
		}
		if ( true === $args['show_time_section'] ) {
			$this->start_controls_section( 'section_content_time', array( 'label' => $args['section_label'] ) );

			$this->add_control(
				'time_format',
				array(
					'label'   => esc_html__( 'Time Format', 'sifency-addons' ),
					'type'    => Controls_Manager::SELECT,
					'options' => array(
						'g:i a'  => wp_date( 'g:i a' ), // e.g., 9:30 am
						'g:i A'  => wp_date( 'g:i A' ), // e.g., 9:30 AM
						'H:i'    => wp_date( 'H:i' ),   // e.g., 09:30
						'custom' => esc_html__( 'Custom', 'sifency-addons' ),
					),
					'default' => 'g:i a',
				)
			);

			$this->add_control(
				'time_format_custom',
				array(
					'label'       => esc_html__( 'Custom Time Format', 'sifency-addons' ),
					'type'        => Controls_Manager::TEXT,
					'default'     => 'g:i a',
					'description' => sprintf( '<a href="%s" target="_blank">%s</a>', 'https://wordpress.org/support/article/formatting-date-and-time/', esc_html__( 'Documentation on formatting.', 'sifency-addons' ) ),
					'condition'   => array( 'time_format' => 'custom' ),
				)
			);

			$this->add_control(
				'placeholder_text',
				array(
					'label'   => esc_html__( 'Placeholder', 'sifency-addons' ),
					'type'    => Controls_Manager::TEXT,
					'default' => wp_date( 'g:i a' ),
				)
			);

			$this->end_controls_section();
		}
		$this->add_sifency_addons_label_content_controls();
		$this->add_sifency_addons_separator_content_controls();
		$this->add_sifency_addons_trio_text_style_controls();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	/**
	 * Common Theme Builder Data Processor & Renderer
	 */
	protected function render_sifency_tb_data( $data, $settings, $type = 'text' ) {
		$display_text = '';
		if ( ! empty( $data ) ) {
			if ( 'date' === $type ) {
				$format       = ( 'custom' === $settings['date_format'] ) ? $settings['date_format_custom'] : $settings['date_format'];
				$display_text = esc_html( date_i18n( $format, strtotime( $data ) ) );
			} elseif ( 'time' === $type ) {
				$format       = ( 'custom' === $settings['time_format'] ) ? $settings['time_format_custom'] : $settings['time_format'];
				$display_text = esc_html( date_i18n( $format, strtotime( $data ) ) );
			} else {
				$display_text = $data;
			}
		} elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$display_text = isset( $settings['placeholder_text'] ) ? $settings['placeholder_text'] : '';
		}
		if ( empty( $display_text ) ) {
			return;
		}
		$animation_manager = new \Sifency\Traits\Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );
		$this->render_sifency_addons_tb_meta( $display_text, $animation_attrs );
	}
}
