<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_EDD_Meta_Info extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'edd';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-edd-meta-info',
			'title'         => 'EDD Meta Info',
			'icon'          => 'eicon-info-circle-o',
			'category'      => array( 'sifency-edd-components' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-edd-tb-style' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Meta Items', 'sifency-addons' ),
			)
		);

		// --- Category ---
		$this->add_control(
			'show_cat',
			array(
				'label'   => 'Show Category',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'cat_icon',
			array(
				'label'     => 'Icon',
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'show_cat' => 'yes' ),
			)
		);
		$this->add_control(
			'cat_label',
			array(
				'label'     => 'Label',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Category:',
				'condition' => array( 'show_cat' => 'yes' ),
			)
		);

		// --- Version ---
		$this->add_control(
			'show_ver',
			array(
				'label'     => 'Show Version',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'separator' => 'before',
			)
		);
		$this->add_control(
			'ver_icon',
			array(
				'label'     => 'Icon',
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'show_ver' => 'yes' ),
			)
		);
		$this->add_control(
			'ver_label',
			array(
				'label'     => 'Label',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Version:',
				'condition' => array( 'show_ver' => 'yes' ),
			)
		);

		// --- Updated ---
		$this->add_control(
			'show_date',
			array(
				'label'     => 'Show Last Update',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'separator' => 'before',
			)
		);
		$this->add_control(
			'date_icon',
			array(
				'label'     => 'Icon',
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'show_date' => 'yes' ),
			)
		);
		$this->add_control(
			'date_label',
			array(
				'label'     => 'Label',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Last Updated:',
				'condition' => array( 'show_date' => 'yes' ),
			)
		);
		$this->add_control(
			'date_format',
			array(
				'label'       => 'Date Format',
				'type'        => Controls_Manager::TEXT,
				'default'     => 'F j, Y',
				'description' => 'e.g. F j, Y (July 4, 2023)',
				'condition'   => array( 'show_date' => 'yes' ),
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// ---------------------------------------------------------------------
		// STYLE: Layout
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_style_layout',
			array(
				'label' => esc_html__( 'Layout', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'layout_direction',
			array(
				'label'     => esc_html__( 'Direction', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'row'    => array(
						'title' => 'Inline',
						'icon'  => 'eicon-h-align-left',
					),
					'column' => array(
						'title' => 'List',
						'icon'  => 'eicon-v-align-top',
					),
				),
				'default'   => 'column',
				'selectors' => array(
					'{{WRAPPER}} .sifency-edd-meta-list' => 'flex-direction: {{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'gap',
			array(
				'label'     => esc_html__( 'Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
				),
				'default'   => array(
					'size' => 10,
					'unit' => 'px',
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-edd-meta-list' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'align_items',
			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'   => 'flex-start',
				'selectors' => array(
					'{{WRAPPER}} .sifency-edd-meta-list' => 'align-items: {{VALUE}};',
					'{{WRAPPER}} .sifency-edd-meta-list[style*="flex-direction: row"]' => 'justify-content: {{VALUE}}; align-items: center;',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_content',
			array(
				'label' => esc_html__( 'Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'meta_typography',
				'selector' => '{{WRAPPER}} .sifency-meta-item',
			)
		);
		$this->add_control(
			'text_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#666',
				'selectors' => array( '{{WRAPPER}} .sifency-meta-item' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'link_color',
			array(
				'label'     => 'Link Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#2271b1',
				'selectors' => array( '{{WRAPPER}} .sifency-meta-item a' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'label_color',
			array(
				'label'     => 'Label Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#333',
				'selectors' => array( '{{WRAPPER}} .sifency-meta-label' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'icon_color',
			array(
				'label'     => 'Icon Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#2271b1',
				'selectors' => array( '{{WRAPPER}} .sifency-meta-icon' => 'color: {{VALUE}};' ),
				'separator' => 'before',
			)
		);

		$this->add_responsive_control(
			'icon_size',
			array(
				'label'     => 'Icon Size',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-meta-icon i' => 'font-size: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();
		$post_id    = get_the_ID();

		$cat_list = get_the_term_list( $post_id, 'download_category', '', ', ', '' );
		$cat_list = ( ! is_wp_error( $cat_list ) ) ? $cat_list : '';

		$version = get_post_meta( $post_id, 'digital_version', true );

		$raw_date       = get_post_meta( $post_id, 'digital_last_updated', true );
		$formatted_date = '';

		if ( ! empty( $raw_date ) ) {
			if ( is_numeric( $raw_date ) ) {
				$formatted_date = esc_html( date_i18n( $settings['date_format'], $raw_date ) );
			} else {
				$timestamp = strtotime( $raw_date );
				if ( $timestamp ) {
					$formatted_date = esc_html( date_i18n( $settings['date_format'], $timestamp ) );
				} else {
					$formatted_date = $raw_date;
				}
			}
		} elseif ( empty( $raw_date ) ) {
			$formatted_date = get_the_modified_date( $settings['date_format'] );
		}

		// Animation
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array( 'sifency-edd-meta-list' ) );

		?>
		<ul id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $animation_attrs ); ?>>
			
			<!-- Category -->
			<?php if ( 'yes' === $settings['show_cat'] && ! empty( $cat_list ) ) : ?>
				<li class="sifency-meta-item">
					<?php if ( ! empty( $settings['cat_icon']['value'] ) ) : ?>
						<span class="sifency-meta-icon"><?php Icons_Manager::render_icon( $settings['cat_icon'], array( 'aria-hidden' => 'true' ) ); ?></span>
					<?php endif; ?>
					
					<?php if ( ! empty( $settings['cat_label'] ) ) : ?>
						<span class="sifency-meta-label"><?php echo esc_html( $settings['cat_label'] ); ?></span>
					<?php endif; ?>
					
					<span class="sifency-meta-value"><?php echo wp_kses_post( $cat_list ); ?></span>
				</li>
			<?php endif; ?>

			<!-- Version -->
			<?php if ( 'yes' === $settings['show_ver'] && ! empty( $version ) ) : ?>
				<li class="sifency-meta-item">
					<?php if ( ! empty( $settings['ver_icon']['value'] ) ) : ?>
						<span class="sifency-meta-icon"><?php Icons_Manager::render_icon( $settings['ver_icon'], array( 'aria-hidden' => 'true' ) ); ?></span>
					<?php endif; ?>
					
					<?php if ( ! empty( $settings['ver_label'] ) ) : ?>
						<span class="sifency-meta-label"><?php echo esc_html( $settings['ver_label'] ); ?></span>
					<?php endif; ?>
					
					<span class="sifency-meta-value"><?php echo esc_html( $version ); ?></span>
				</li>
			<?php endif; ?>

			<!-- Last Update -->
			<?php if ( 'yes' === $settings['show_date'] && ! empty( $formatted_date ) ) : ?>
				<li class="sifency-meta-item">
					<?php if ( ! empty( $settings['date_icon']['value'] ) ) : ?>
						<span class="sifency-meta-icon"><?php Icons_Manager::render_icon( $settings['date_icon'], array( 'aria-hidden' => 'true' ) ); ?></span>
					<?php endif; ?>
					
					<?php if ( ! empty( $settings['date_label'] ) ) : ?>
						<span class="sifency-meta-label"><?php echo esc_html( $settings['date_label'] ); ?></span>
					<?php endif; ?>
					
					<span class="sifency-meta-value"><?php echo esc_html( $formatted_date ); ?></span>
				</li>
			<?php endif; ?>

		</ul>
		<?php
	}
}
