<?php
/**
 * Global post tags widget.
 *
 * @package IMPACT_WIDGETS_ELEMENTOR
 */

namespace ImpactAddons\ImpactWidgetsForElementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Post_Tags extends Widget_Base {
	use Post_Context;

	public function get_name(): string {
		return 'impact_widgets_elementor_post-tags';
	}

	public function get_title(): string {
		return esc_html__( 'Post Tags', 'impact-widgets-for-elementor' );
	}

	public function get_icon(): string {
		return 'eicon-tags';
	}

	public function get_categories(): array {
		return array( 'impact-widgets-elementor' );
	}

	public function get_keywords(): array {
		return array( 'blog', 'post', 'tag', 'tags', 'taxonomy', 'archive' );
	}

	public function get_style_depends(): array {
		return array( 'impact_widgets_elementor_post-widgets' );
	}

	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_card_style_controls();
		$this->register_heading_style_controls();
		$this->register_layout_style_controls();
		$this->register_tag_style_controls();
	}

	private function register_content_controls(): void {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Tags', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'show_heading',
			array(
				'label'        => esc_html__( 'Show Heading', 'impact-widgets-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'impact-widgets-for-elementor' ),
				'label_off'    => esc_html__( 'Hide', 'impact-widgets-for-elementor' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'heading',
			array(
				'label'     => esc_html__( 'Heading', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Tags', 'impact-widgets-for-elementor' ),
				'condition' => array(
					'show_heading' => 'yes',
				),
			)
		);

		$this->add_control(
			'heading_tag',
			array(
				'label'     => esc_html__( 'Heading HTML Tag', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'h3',
				'options'   => array(
					'h2'  => 'H2',
					'h3'  => 'H3',
					'h4'  => 'H4',
					'h5'  => 'H5',
					'div' => 'div',
				),
				'condition' => array(
					'show_heading' => 'yes',
				),
			)
		);

		$this->add_control(
			'tag_source',
			array(
				'label'   => esc_html__( 'Show Tags', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'all',
				'options' => array(
					'all'      => esc_html__( 'All Tags', 'impact-widgets-for-elementor' ),
					'selected' => esc_html__( 'Selected Tags', 'impact-widgets-for-elementor' ),
					'current'  => esc_html__( 'Current Post Tags', 'impact-widgets-for-elementor' ),
				),
			)
		);

		$this->add_control(
			'selected_tags',
			array(
				'label'       => esc_html__( 'Select Tags', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::SELECT2,
				'options'     => $this->get_tag_options(),
				'multiple'    => true,
				'label_block' => true,
				'condition'   => array(
					'tag_source' => 'selected',
				),
			)
		);

		$this->add_control(
			'hide_empty',
			array(
				'label'        => esc_html__( 'Hide Empty Tags', 'impact-widgets-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'impact-widgets-for-elementor' ),
				'label_off'    => esc_html__( 'No', 'impact-widgets-for-elementor' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => array(
					'tag_source!' => 'current',
				),
			)
		);

		$this->add_control(
			'orderby',
			array(
				'label'     => esc_html__( 'Order By', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'name',
				'options'   => array(
					'name'  => esc_html__( 'Name', 'impact-widgets-for-elementor' ),
					'count' => esc_html__( 'Post Count', 'impact-widgets-for-elementor' ),
					'id'    => esc_html__( 'Tag ID', 'impact-widgets-for-elementor' ),
					'slug'  => esc_html__( 'Slug', 'impact-widgets-for-elementor' ),
				),
				'condition' => array(
					'tag_source!' => 'selected',
				),
			)
		);

		$this->add_control(
			'order',
			array(
				'label'     => esc_html__( 'Order', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'ASC',
				'options'   => array(
					'ASC'  => esc_html__( 'Ascending', 'impact-widgets-for-elementor' ),
					'DESC' => esc_html__( 'Descending', 'impact-widgets-for-elementor' ),
				),
				'condition' => array(
					'tag_source!' => 'selected',
				),
			)
		);

		$this->add_control(
			'limit',
			array(
				'label'       => esc_html__( 'Maximum Tags', 'impact-widgets-for-elementor' ),
				'description' => esc_html__( 'Use 0 to show all available tags.', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 500,
				'step'        => 1,
				'default'     => 0,
				'condition'   => array(
					'tag_source!' => 'selected',
				),
			)
		);

		$this->add_control(
			'show_count',
			array(
				'label'        => esc_html__( 'Show Post Count', 'impact-widgets-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'impact-widgets-for-elementor' ),
				'label_off'    => esc_html__( 'Hide', 'impact-widgets-for-elementor' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'count_separator',
			array(
				'label'     => esc_html__( 'Count Separator', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => '',
				'condition' => array(
					'show_count' => 'yes',
				),
			)
		);

		$this->end_controls_section();
	}

	private function register_card_style_controls(): void {
		$this->start_controls_section(
			'section_card_style',
			array(
				'label' => esc_html__( 'Card', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'card_background',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .iaew-post-tags',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'card_border',
				'selector' => '{{WRAPPER}} .iaew-post-tags',
			)
		);

		$this->add_responsive_control(
			'card_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'default'    => array(
					'top'      => 8,
					'right'    => 8,
					'bottom'   => 8,
					'left'     => 8,
					'unit'     => 'px',
					'isLinked' => true,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-tags' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'card_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'default'    => array(
					'top'      => 24,
					'right'    => 18,
					'bottom'   => 24,
					'left'     => 18,
					'unit'     => 'px',
					'isLinked' => false,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-tags' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'card_box_shadow',
				'selector' => '{{WRAPPER}} .iaew-post-tags',
			)
		);

		$this->end_controls_section();
	}

	private function register_heading_style_controls(): void {
		$this->start_controls_section(
			'section_heading_style',
			array(
				'label'     => esc_html__( 'Heading', 'impact-widgets-for-elementor' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'show_heading' => 'yes',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => '{{WRAPPER}} .iaew-post-tags__heading',
			)
		);

		$this->add_control(
			'heading_color',
			array(
				'label'     => esc_html__( 'Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#111827',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__heading' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'heading_alignment',
			array(
				'label'     => esc_html__( 'Alignment', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__heading' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'heading_spacing',
			array(
				'label'      => esc_html__( 'Bottom Spacing', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 14,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-tags__heading' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	private function register_layout_style_controls(): void {
		$this->start_controls_section(
			'section_layout_style',
			array(
				'label' => esc_html__( 'Layout', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => esc_html__( 'Alignment', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default'   => 'flex-start',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__list' => 'justify-content: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'column_gap',
			array(
				'label'      => esc_html__( 'Column Gap', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 60,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 8,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-tags__list' => 'column-gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'row_gap',
			array(
				'label'      => esc_html__( 'Row Gap', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 60,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 8,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-tags__list' => 'row-gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	private function register_tag_style_controls(): void {
		$this->start_controls_section(
			'section_tag_style',
			array(
				'label' => esc_html__( 'Tag Pills', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'tag_typography',
				'selector' => '{{WRAPPER}} .iaew-post-tags__link',
			)
		);

		$this->start_controls_tabs( 'tag_style_tabs' );

		$this->start_controls_tab(
			'tag_normal_tab',
			array(
				'label' => esc_html__( 'Normal', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'tag_color',
			array(
				'label'     => esc_html__( 'Text Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#111827',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__link' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'tag_background',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__link' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'tag_border',
				'selector' => '{{WRAPPER}} .iaew-post-tags__link',
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'tag_hover_tab',
			array(
				'label' => esc_html__( 'Hover', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'tag_hover_color',
			array(
				'label'     => esc_html__( 'Text Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__link:hover, {{WRAPPER}} .iaew-post-tags__link:focus' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'tag_hover_background',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#4f2df5',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__link:hover, {{WRAPPER}} .iaew-post-tags__link:focus' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'tag_hover_border_color',
			array(
				'label'     => esc_html__( 'Border Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#4f2df5',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__link:hover, {{WRAPPER}} .iaew-post-tags__link:focus' => 'border-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'tag_hover_box_shadow',
				'selector' => '{{WRAPPER}} .iaew-post-tags__link:hover, {{WRAPPER}} .iaew-post-tags__link:focus',
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->add_responsive_control(
			'tag_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'top'      => 6,
					'right'    => 12,
					'bottom'   => 6,
					'left'     => 12,
					'unit'     => 'px',
					'isLinked' => false,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-tags__link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'tag_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'default'    => array(
					'top'      => 50,
					'right'    => 50,
					'bottom'   => 50,
					'left'     => 50,
					'unit'     => 'px',
					'isLinked' => true,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-tags__link' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'tag_box_shadow',
				'selector' => '{{WRAPPER}} .iaew-post-tags__link',
			)
		);

		$this->add_responsive_control(
			'count_spacing',
			array(
				'label'      => esc_html__( 'Count Spacing', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'unit' => 'px',
					'size' => 5,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-tags__count' => 'margin-left: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'show_count' => 'yes',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'      => 'count_typography',
				'selector'  => '{{WRAPPER}} .iaew-post-tags__count',
				'condition' => array(
					'show_count' => 'yes',
				),
			)
		);

		$this->add_control(
			'count_color',
			array(
				'label'     => esc_html__( 'Count Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__count' => 'color: {{VALUE}};',
				),
				'condition' => array(
					'show_count' => 'yes',
				),
			)
		);

		$this->add_control(
			'count_hover_color',
			array(
				'label'     => esc_html__( 'Count Hover Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-tags__link:hover .iaew-post-tags__count, {{WRAPPER}} .iaew-post-tags__link:focus .iaew-post-tags__count' => 'color: {{VALUE}};',
				),
				'condition' => array(
					'show_count' => 'yes',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings    = $this->get_settings_for_display();
		$tags        = $this->get_display_tags( $settings );
		$heading_tag = in_array( $settings['heading_tag'], array( 'h2', 'h3', 'h4', 'h5', 'div' ), true ) ? $settings['heading_tag'] : 'h3';

		echo '<div class="iaew-post-tags">';

		if ( 'yes' === $settings['show_heading'] && '' !== trim( (string) $settings['heading'] ) ) {
			printf(
				'<%1$s class="iaew-post-tags__heading">%2$s</%1$s>',
				esc_attr( $heading_tag ),
				esc_html( $settings['heading'] )
			);
		}

		if ( empty( $tags ) ) {
			echo '<div class="iaew-post-tags__empty">' . esc_html__( 'No tags found.', 'impact-widgets-for-elementor' ) . '</div>';
			echo '</div>';
			return;
		}

		echo '<ul class="iaew-post-tags__list">';

		foreach ( $tags as $tag ) {
			$link = get_tag_link( $tag->term_id );

			if ( is_wp_error( $link ) ) {
				continue;
			}

			printf(
				'<li class="iaew-post-tags__item"><a class="iaew-post-tags__link" href="%1$s"><span class="iaew-post-tags__name">%2$s</span>',
				esc_url( $link ),
				esc_html( $tag->name )
			);

			if ( 'yes' === $settings['show_count'] ) {
				printf(
					'<span class="iaew-post-tags__count">%1$s%2$s</span>',
					esc_html( $settings['count_separator'] ),
					esc_html( number_format_i18n( $tag->count ) )
				);
			}

			echo '</a></li>';
		}

		echo '</ul></div>';
	}

	private function get_tag_options(): array {
		$options = array();
		$terms   = get_terms(
			array(
				'taxonomy'   => 'post_tag',
				'hide_empty' => false,
				'orderby'    => 'name',
				'order'      => 'ASC',
			)
		);

		if ( is_wp_error( $terms ) ) {
			return $options;
		}

		foreach ( $terms as $term ) {
			$options[ (string) $term->term_id ] = $term->name;
		}

		return $options;
	}

	private function get_display_tags( array $settings ): array {
		$source = in_array( $settings['tag_source'], array( 'all', 'selected', 'current' ), true ) ? $settings['tag_source'] : 'all';

		if ( 'selected' === $source ) {
			$selected = isset( $settings['selected_tags'] ) && is_array( $settings['selected_tags'] ) ? array_filter( array_map( 'absint', $settings['selected_tags'] ) ) : array();

			if ( empty( $selected ) ) {
				return array();
			}

			$terms = get_terms(
				array(
					'taxonomy'   => 'post_tag',
					'include'    => $selected,
					'orderby'    => 'include',
					'hide_empty' => 'yes' === $settings['hide_empty'],
				)
			);

			return is_wp_error( $terms ) ? array() : $terms;
		}

		if ( 'current' === $source ) {
			$post = $this->iaew_get_blog_context_post();

			if ( ! $post ) {
				return array();
			}

			$terms = get_the_tags( $post->ID );
			return is_array( $terms ) ? $terms : array();
		}

		$orderby = in_array( $settings['orderby'], array( 'name', 'count', 'id', 'slug' ), true ) ? $settings['orderby'] : 'name';
		$order   = 'DESC' === $settings['order'] ? 'DESC' : 'ASC';
		$limit   = isset( $settings['limit'] ) ? absint( $settings['limit'] ) : 0;
		$args    = array(
			'taxonomy'   => 'post_tag',
			'hide_empty' => 'yes' === $settings['hide_empty'],
			'orderby'    => $orderby,
			'order'      => $order,
		);

		if ( $limit > 0 ) {
			$args['number'] = $limit;
		}

		$terms = get_terms( $args );
		return is_wp_error( $terms ) ? array() : $terms;
	}
}
