<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Item_Style_Controls;
use Sifency\Traits\Sifency_Icon_Style_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Widget_Postmetadata extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Icon_Style_Controls;
	use Sifency_Item_Style_Controls;
	use Sifency_Core_Text_Style;

    public function get_name() {
        return 'post-metadata';
    }

    public function get_title() {
        return __( 'Sifency Post Meta', 'sifency-addons' );
    }

    public function get_icon() {
        return 'eicon-meta-data';
    }

    public function get_categories() {
        return [ 'sifency-post-components' ];
    }

    public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-frontend-style', 'sifency-icon', 'sifency-post-single-style' ];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'section_content',
            [ 'label' => esc_html__( 'Meta Data', 'sifency-addons') ]
        );
        $repeater = new \Elementor\Repeater();
		$repeater->add_control(
			'type',
			[ 'label' => 'Type', 'type' => Controls_Manager::SELECT, 'default' => 'date', 'options' => [ 'author' => 'Author', 'date' => 'Date', 'time' => 'Time', 'comments' => 'Comments', 'terms' => 'Terms' ] ]
		);
		$repeater->add_control(
			'date_format',
			[ 'label' => 'Date Format', 'type' => Controls_Manager::SELECT, 'default' => 'default', 'options' => [ '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' => [ 'type' => 'date' ] ]
		);
        $repeater->add_control(
			'taxonomy',
			[ 'label' => 'Taxonomy', 'type' => Controls_Manager::SELECT, 'default' => 'category', 'options' => [ 'category' => 'Categories', 'post_tag' => 'Tags' ], 'condition' => [ 'type' => 'terms' ] ]
		);
		$repeater->add_control(
			'icon',
			[ 'label' => 'Icon', 'type' => Controls_Manager::ICONS ]
		);
		$this->add_control(
			'meta_list',
			[
				'label' => 'Meta List', 'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[ 'type' => 'author', 'icon' => [ 'value' => 'icon-user', 'library' => 'sifency-icons' ] ],
					[ 'type' => 'date', 'icon' => [ 'value' => ' icon-calendar-4', 'library' => 'sifency-icons' ] ],
					[ 'type' => 'comments', 'icon' => [ '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_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Content Style',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
		$this->add_sifency_icon_style_controls();		
		$this->add_sifency_animation_controls();
        $this->update_control(
            'content_align',
            [
                '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_Animation_Manager($settings);
        $wrapper_classes = ['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 = ['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'], [ '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;
        }
    }
}