<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Postmetadata extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'post-metadata',
			'title'         => 'Sifency Post Meta',
			'icon'          => 'eicon-meta-data',
			'category'      => array( 'sifency-post-components' ),
			'keywords'      => array( 'heading', 'title', 'text', 'dynamic', 'sifency' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-frontend-style', 'sifency-icon', 'sifency-post-single-style' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Meta Data', 'sifency-addons' ) )
		);
		$repeater = new \Elementor\Repeater();
		$repeater->add_control(
			'type',
			array(
				'label'   => 'Type',
				'type'    => Controls_Manager::SELECT,
				'default' => 'date',
				'options' => array(
					'author'   => 'Author',
					'date'     => 'Date',
					'time'     => 'Time',
					'comments' => 'Comments',
					'terms'    => 'Terms',
				),
			)
		);
		$repeater->add_control(
			'date_format',
			array(
				'label'     => 'Date Format',
				'type'      => Controls_Manager::SELECT,
				'default'   => 'default',
				'options'   => array(
					'default' => 'Default',
					'F j, Y'  => 'F j, Y',
					'Y-m-d'   => 'Y-m-d',
					'm/d/Y'   => 'm/d/Y',
					'd/m/Y'   => 'd/m/Y',
				),
				'condition' => array( 'type' => 'date' ),
			)
		);
		$repeater->add_control(
			'taxonomy',
			array(
				'label'     => 'Taxonomy',
				'type'      => Controls_Manager::SELECT,
				'default'   => 'category',
				'options'   => array(
					'category' => 'category',
					'post_tag' => 'Tags',
				),
				'condition' => array( 'type' => 'terms' ),
			)
		);
		$repeater->add_control(
			'icon',
			array(
				'label' => 'Icon',
				'type'  => Controls_Manager::ICONS,
			)
		);
		$this->add_control(
			'meta_list',
			array(
				'label'       => 'Meta List',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'type' => 'author',
						'icon' => array(
							'value'   => 'icon-user',
							'library' => 'sifency-icons',
						),
					),
					array(
						'type' => 'date',
						'icon' => array(
							'value'   => ' icon-calendar-4',
							'library' => 'sifency-icons',
						),
					),
					array(
						'type' => 'comments',
						'icon' => array(
							'value'   => 'icon-commenting-o',
							'library' => 'sifency-icons',
						),
					),
				),
				'title_field' => '<i class="{{ icon.value }}" aria-hidden="true"></i> {{{ elementor.helpers.renderIcon( this, icon, {}, "i", "panel" ) || "" }}} {{{ type.charAt(0).toUpperCase() + type.slice(1) }}}',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Content Style',
				'selector' => '.sifency-content-style',
				'exclude'  => array( 'alignment', 'stroke', 'shadow' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'content_align',
			array(
				'type' => Controls_Manager::HIDDEN,
			)
		);
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();

		if ( empty( $settings['meta_list'] ) ) {
			return;
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-post-meta' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div 
		<?php

			echo wp_kses_post( $wrapper_attrs );
		?>
		>
			<?php foreach ( $settings['meta_list'] as $item ) : ?>
				<?php
				$item_classes = array( 'sifency-post-meta__item', 'sifency-item-style' );
				$item_attrs   = $animation_manager->get_item_attributes( $item_classes );
				?>
				<div <?php echo wp_kses_post( $item_attrs ); ?>>
					<?php if ( ! empty( $item['icon']['value'] ) ) : ?>
						<span class="sifency-post-meta__icon sifency-icon-style">
							<?php \Elementor\Icons_Manager::render_icon( $item['icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</span>
					<?php endif; ?>
					<span class="sifency-post-meta__text sifency-content-style">
						<?php
						if ( method_exists( $this, 'sifency_addons_render_meta_item' ) ) {
							$this->sifency_addons_render_meta_item( $item );
						}
						?>
					</span>
				</div>
			<?php endforeach; ?>
		</div>
		<?php
	}

	private function sifency_addons_render_meta_item( $item ) {
		$type = $item['type'];

		switch ( $type ) {
			case 'author':
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo get_the_author_posts_link();
				break;
			case 'date':
				$format = $item['date_format'] === 'default' ? get_option( 'date_format' ) : $item['date_format'];
				the_date( $format );
				break;
			case 'time':
				the_time();
				break;
			case 'comments':
				if ( comments_open() ) {
					comments_popup_link( __( 'No Comments', 'sifency-addons' ), __( '1 Comment', 'sifency-addons' ), __( '% Comments', 'sifency-addons' ) );
				}
				break;
			case 'terms':
				the_terms( get_the_ID(), $item['taxonomy'] );
				break;
		}
	}
}
