<?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_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Widget_Posttagcat extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_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-tag-category',
			'title'         => 'Sifency Post Terms',
			'icon'          => 'eicon-tags',
			'category'      => array( 'sifency-post-components' ),
			'keywords'      => array( 'faq', 'accordion', 'toggle', 'question', 'answer', 'terms', 'category', 'tag' ),
			'style_depends' => array( 'sifency-frontend-style', 'sifency-post-single-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'taxonomy',
			array(
				'label'   => esc_html__( 'Taxonomy', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'category' => 'category',
					'post_tag' => 'Tags',
				),
				'default' => 'category',
			)
		);
		$this->add_control(
			'prefix',
			array(
				'label'       => esc_html__( 'Prefix', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'e.g. Categories:', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'separator',
			array(
				'label'       => esc_html__( 'Separator', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => ',',
			)
		);
		$this->add_control(
			'term_count',
			array(
				'label'       => esc_html__( 'Max Count', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 1,
				'step'        => 1,
				'description' => esc_html__( 'Leave empty to show all terms.', 'sifency-addons' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- Style Section ---
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'center'     => array(
						'title' => 'Center',
						'icon'  => 'eicon-h-align-center',
					),
					'flex-end'   => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-post-terms' => 'justify-content: {{VALUE}};' ),
			)
		);
		$this->add_responsive_control(
			'gap',
			array(
				'label'     => esc_html__( 'Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array( 'px' => array( 'max' => 50 ) ),
				'selectors' => array( '{{WRAPPER}} .sifency-post-terms, {{WRAPPER}} .sifency-post-terms-list' => 'gap: {{SIZE}}px;' ),
			)
		);
		$this->add_control(
			'heading_prefix',
			array(
				'label'     => esc_html__( 'Prefix', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'prefix_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-post-terms__prefix' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'prefix_typography',
				'selector' => '{{WRAPPER}} .sifency-post-terms__prefix',
			)
		);

		$this->add_control(
			'heading_separator',
			array(
				'label'     => esc_html__( 'Separator', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'separator_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-post-terms__separator' => 'color: {{VALUE}};' ),
			)
		);

		$this->end_controls_section();

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style',
				'label'          => 'Tag or Categories',
				'selector'       => '.sifency-content-style',
				'show_alignment' => false,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'label_style',
				'label'      => 'Prefix Style',
				'selector'   => '.sifency-label-style',
				'condition'  => array( 'prefix!' => '' ),
				'color_only' => true,
				'exclude'    => array( 'alignment', 'transition', 'tabs', 'bg', 'radius', 'border', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'meta_style',
				'label'      => 'Separator',
				'selector'   => '.sifency-meta-style',
				'condition'  => array( 'separator!' => '' ),
				'color_only' => true,
				'exclude'    => array( 'alignment', 'transition', 'tabs', 'bg', 'radius', 'border', 'shadow' ),
			)
		);

		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();
		$taxonomy   = $settings['taxonomy'];

		$terms = get_the_terms( get_the_ID(), $taxonomy );
		if ( ( empty( $terms ) || is_wp_error( $terms ) ) && \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$terms = array(
				(object) array(
					'name' => esc_html__( 'Technology', 'sifency-addons' ),
					'link' => '#',
				),
				(object) array(
					'name' => esc_html__( 'WordPress', 'sifency-addons' ),
					'link' => '#',
				),
				(object) array(
					'name' => esc_html__( 'Web Design', 'sifency-addons' ),
					'link' => '#',
				),
			);
			foreach ( $terms as $term ) {
				$term->link = '#';
			}
		}

		if ( empty( $terms ) || is_wp_error( $terms ) ) {
			return;
		}

		if ( ! empty( $settings['term_count'] ) && is_numeric( $settings['term_count'] ) ) {
			$terms = array_slice( $terms, 0, $settings['term_count'] );
		}

		$animation_manager    = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes      = array( 'sifency-post-terms' );
		$wrapper_attrs_string = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs_string ); ?>>
			<?php if ( ! empty( $settings['prefix'] ) ) : ?>
				<span class="sifency-post-terms__prefix sifency-label-style">
					<?php echo esc_html( $settings['prefix'] ); ?>
				</span>
			<?php endif; ?>

			<div class="sifency-post-terms-list">
				<?php
				if ( ! empty( $terms ) && is_array( $terms ) ) {

					$separator_text = $settings['separator'] ?? ', ';
					$separator      = '<span class="sifency-post-terms__separator sifency-meta-style">' . esc_html( $separator_text ) . '</span>';

					$term_links = array();
					foreach ( $terms as $term ) {
						$link         = ( isset( $term->link ) && $term->link === '#' ) ? '#' : esc_url( get_term_link( $term ) );
						$term_links[] = '<a href="' . $link . '" class="sifency-content-style">' . esc_html( $term->name ) . '</a>';
					}

					$allowed_tags = array(
						'a'    => array(
							'href'  => array(),
							'class' => array(),
						),
						'span' => array(
							'class' => array(),
						),
					);

					echo wp_kses( implode( $separator, $term_links ), $allowed_tags );
				}
				?>
			</div>

		</div>
		<?php
	}

	/**
	 * Render the widget output in the editor.
	 *
	 * Written as a Backbone JavaScript template and used to generate the live preview.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function _content_template() {
		?>
		<#
		var demoTerms = [ 'Technology', 'WordPress', 'Web Design', 'Development', 'UI/UX' ];
		
		// Apply limit to dummy data for preview
		if ( settings.term_count && settings.term_count > 0 ) {
			demoTerms = demoTerms.slice( 0, settings.term_count );
		}

		var term_links = [];

		view.addRenderAttribute( 'wrapper', 'class', 'sifency-post-terms' );

		var separator = '<span class="sifency-post-terms__separator">' + ( settings.separator || ',' ) + '</span>';

		_.each( demoTerms, function( term ) {
			term_links.push( '<a href="#">' + term + '</a>' );
		} );
		
		#>
		<div {{{ view.getRenderAttributeString( 'wrapper' ) }}}>
			<# if ( settings.prefix ) { #>
				<span class="sifency-post-terms__prefix">
					{{{ settings.prefix }}}
				</span>
			<# } #>

			<div class="sifency-post-terms-list sifency-content-style">
				{{{ term_links.join( separator ) }}}
			</div>
		</div>
		<?php
	}
}
