<?php
namespace Sifency\Traits;

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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;

/**
 * Trait for reusable filter functionality across multiple widgets.
 */
trait Sifency_Addons_Filter_Controls {

	/**
	 * Taxonomy fetching helper for controls
	 */
	protected function get_taxonomies_for_filter() {
		$taxonomies = get_taxonomies( array( 'public' => true ), 'objects' );
		$options    = array();
		foreach ( $taxonomies as $taxonomy ) {
			if ( in_array( $taxonomy->name, array( 'post_format', 'nav_menu', 'link_category' ) ) ) {
				continue;
			}
			$options[ $taxonomy->name ] = $taxonomy->label;
		}
		return $options;
	}

	/**
	 * Add Filter Controls to the widget
	 */
	public function add_sifency_addons_filter_controls( $args = array() ) {
		$args = wp_parse_args(
			$args,
			array(
				'section_id' => 'section_filter_controls',
				'label'      => esc_html__( 'Filter Bar', 'sifency-addons' ),
				'prefix'     => '',
				'condition'  => array(),
			)
		);

		$this->start_controls_section(
			$args['section_id'],
			array(
				'label'     => $args['label'],
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => $args['condition'],
			)
		);

		$this->add_control(
			$args['prefix'] . 'show_filter',
			array(
				'label'        => esc_html__( 'Enable Filter', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'no',
			)
		);

		$this->add_control(
			$args['prefix'] . 'filter_taxonomy',
			array(
				'label'     => esc_html__( 'Filter Taxonomy', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => $this->get_taxonomies_for_filter(),
				'default'   => 'category',
				'condition' => array( $args['prefix'] . 'show_filter' => 'yes' ),
			)
		);

		$this->add_control(
			$args['prefix'] . 'filter_all_label',
			array(
				'label'     => esc_html__( ' "All" Label ', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'All', 'sifency-addons' ),
				'condition' => array( $args['prefix'] . 'show_filter' => 'yes' ),
			)
		);

		$this->add_control(
			$args['prefix'] . 'hide_all_tab',
			array(
				'label'        => esc_html__( 'Hide "All" Tab', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'no',
				'description'  => esc_html__( 'Hide the "All" tab button only. Category items will still be shown.', 'sifency-addons' ),
				'condition'    => array( $args['prefix'] . 'show_filter' => 'yes' ),
			)
		);

		$this->add_control(
			$args['prefix'] . 'filter_exclude_tabs',
			array(
				'label'       => esc_html__( 'Exclude Tabs', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'e.g. news, events, tips', 'sifency-addons' ),
				'description' => esc_html__( 'Enter category slugs to hide their tab buttons (comma-separated). Their posts will still appear under "All".', 'sifency-addons' ),
				'condition'   => array( $args['prefix'] . 'show_filter' => 'yes' ),
			)
		);

		$this->add_responsive_control(
			$args['prefix'] . 'filter_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-h-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default'   => 'center',
				'selectors' => array(
					'{{WRAPPER}} .sifency-addons-filter-items' => 'text-align: {{VALUE}};',
				),
				'condition' => array( $args['prefix'] . 'show_filter' => 'yes' ),
			)
		);

		$this->end_controls_section();

		// --- STYLE SECTION ---
		$this->start_controls_section(
			$args['prefix'] . 'section_filter_style',
			array(
				'label'     => esc_html__( 'Filter Bar Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( $args['prefix'] . 'show_filter' => 'yes' ),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => $args['prefix'] . 'filter_typo',
				'selector' => '{{WRAPPER}} .sifency-addons-filter-item',
			)
		);

		$this->start_controls_tabs( $args['prefix'] . 'filter_tabs' );

		$this->start_controls_tab( $args['prefix'] . 'filter_tab_normal', array( 'label' => 'Normal' ) );
		$this->add_control(
			$args['prefix'] . 'filter_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-addons-filter-item' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();

		$this->start_controls_tab( $args['prefix'] . 'filter_tab_hover', array( 'label' => 'Hover' ) );
		$this->add_control(
			$args['prefix'] . 'filter_hover_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-addons-filter-item:hover' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();

		$this->start_controls_tab( $args['prefix'] . 'filter_tab_active', array( 'label' => 'Active' ) );
		$this->add_control(
			$args['prefix'] . 'filter_active_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-addons-filter-item.active' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			$args['prefix'] . 'filter_item_spacing',
			array(
				'label'      => esc_html__( 'Spacing Between', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-addons-filter-item:not(:last-child)' => 'margin-right: {{SIZE}}{{UNIT}};',
				),
				'separator'  => 'before',
			)
		);

		$this->add_responsive_control(
			$args['prefix'] . 'filter_container_margin',
			array(
				'label'      => esc_html__( 'Container Margin', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-addons-filter-items' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render Filter HTML
	 */
	protected function render_sifency_addons_filter( $args = array() ) {
		$settings = $this->get_settings_for_display();
		$args     = wp_parse_args( $args, array( 'prefix' => '' ) );

		if ( 'yes' !== ( $settings[ $args['prefix'] . 'show_filter' ] ?? 'no' ) ) {
			return;
		}

		$taxonomy  = $settings[ $args['prefix'] . 'filter_taxonomy' ] ?? 'category';
		$all_label = $settings[ $args['prefix'] . 'filter_all_label' ] ?? 'All';

		$terms = get_terms(
			array(
				'taxonomy'   => $taxonomy,
				'hide_empty' => true,
			)
		);

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

		echo '<style>
			.sifency-addons-filter-items { display: block; width: 100%; margin-bottom: 30px; }
			.sifency-addons-filter-item { display: inline-block; cursor: pointer; transition: all 0.3s ease; }
			.sifency-addons-filter-item.active { font-weight: bold; }
		</style>';
		$hide_all_tab   = 'yes' === ( $settings[ $args['prefix'] . 'hide_all_tab' ] ?? 'no' );
		$exclude_raw    = $settings[ $args['prefix'] . 'filter_exclude_tabs' ] ?? '';
		$excluded_slugs = array_filter( array_map( 'trim', explode( ',', $exclude_raw ) ) );

		echo '<div class="sifency-addons-filter-items">';
		if ( ! $hide_all_tab ) {
			echo '<span class="sifency-addons-filter-item active" data-filter="*">' . esc_html( $all_label ) . '</span>';
		}
		foreach ( $terms as $term ) {
			// Skip tab button if slug is in the exclude list — items still appear under "All"
			if ( ! empty( $excluded_slugs ) && in_array( $term->slug, $excluded_slugs, true ) ) {
				continue;
			}
			echo '<span class="sifency-addons-filter-item" data-filter=".term-' . esc_attr( $term->term_id ) . '">' . esc_html( $term->name ) . '</span>';
		}
		echo '</div>';
	}
}
