<?php
/**
 * A reusable Elementor trait for adding a comprehensive set of SLIDER PAGINATION style controls.
 * This trait includes conditional sections for Bullets, Fraction, and Progress Bar types.
 *
 * @package Sifency\Traits
 */

namespace Sifency\Traits;

if ( ! defined('ABSPATH') ) {
    exit; // Exit if accessed directly.
}

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

/**
 * Trait Sifency_Pagination_Style_Controls
 *
 * This trait is designed to style the standard Swiper.js pagination container:
 * <div class="swiper-pagination"></div>
 *
 * Your Swiper.js initialization should be configured based on the pagination type selected.
 */
trait Sifency_Pagination_Style_Controls {

	/**
	 * Adds the complete slider pagination styling control panel with conditional sections.
	 *
	 * @param array $args { Optional. Arguments to customize the controls.
	 *     @type string $prefix A unique prefix for control names.
	 *     @type string $pagination_control_name The name of the control that selects the pagination type.
	 * }
	 */
	protected function add_sifency_pagination_style_controls( $args = [] ) {
		$args = wp_parse_args( $args, [
			'prefix'                  => 'pagination_',
			'pagination_control_name' => 'show_pagination', // The name of your SELECT control
		] );

		$prefix         = $args['prefix'];
		$condition_key  = $args['pagination_control_name'];

		//======================================================================
		//==         STYLE SECTION: PAGINATION (BULLETS)                    ==
		//======================================================================
		$this->start_controls_section(
			$prefix . 'bullets_style_section',
			[
				'label'     => esc_html__( 'Pagination (Bullets)', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ $condition_key => 'bullets' ],
			]
		);

		$selector_container = '{{WRAPPER}} .swiper-pagination';
		$selector_bullet = '{{WRAPPER}} .swiper-pagination-bullet';
		$selector_bullet_active = '{{WRAPPER}} .swiper-pagination-bullet-active';

		$this->add_responsive_control(
			$prefix . 'bullets_align',
			[
				'label'   => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-h-align-left' ],
					'center' => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-h-align-center' ],
					'right' => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-h-align-right' ],
				],
				'default' => 'center',
				'selectors' => [ $selector_container => 'text-align: {{VALUE}};' ],
			]
		);

		$this->add_responsive_control(
			$prefix . 'bullets_spacing',
			[
				'label' => esc_html__( 'Spacing', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors' => [ $selector_bullet => 'margin: 0 {{SIZE}}{{UNIT}} !important;' ],
			]
		);

		$this->add_responsive_control(
			$prefix . 'bullets_vertical_offset',
			[
				'label' => esc_html__( 'Vertical Offset', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => -200, 'max' => 200 ] ],
				'selectors' => [ $selector_container => 'bottom: {{SIZE}}{{UNIT}} !important; top: auto;' ],
			]
		);

		$this->start_controls_tabs( $prefix . 'bullets_style_tabs' );
		// Normal Tab
		$this->start_controls_tab( $prefix . 'bullets_normal_tab', [ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ] );
		$this->add_responsive_control( $prefix . 'bullets_width', [ 'label' => esc_html__( 'Width', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ $selector_bullet => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};' ] ] );
		$this->add_control( $prefix . 'bullets_color', [ 'label' => esc_html__( 'Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $selector_bullet => 'background: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => $prefix . 'bullets_border', 'selector' => $selector_bullet ] );
		$this->add_responsive_control( $prefix . 'bullets_radius', [ 'label' => esc_html__( 'Border Radius', 'sifency-addons' ), 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ $selector_bullet => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		$this->end_controls_tab();

		// Active Tab
		$this->start_controls_tab( $prefix . 'bullets_active_tab', [ 'label' => esc_html__( 'Active', 'sifency-addons' ) ] );
		$this->add_responsive_control( $prefix . 'bullets_active_width', [ 'label' => esc_html__( 'Width', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ $selector_bullet_active => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};' ] ] );
		$this->add_control( $prefix . 'bullets_active_color', [ 'label' => esc_html__( 'Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $selector_bullet_active => 'background: {{VALUE}};' ] ] );
		$this->add_control( $prefix . 'bullets_active_border_color', [ 'label' => esc_html__( 'Border Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $selector_bullet_active => 'border-color: {{VALUE}};' ] ] );
		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();

		//======================================================================
		//==         STYLE SECTION: PAGINATION (FRACTION)                   ==
		//======================================================================
		$this->start_controls_section(
			$prefix . 'fraction_style_section',
			[
				'label'     => esc_html__( 'Pagination (Fraction)', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ $condition_key => 'fraction' ],
			]
		);

		$selector_fraction = '{{WRAPPER}} .swiper-pagination-fraction';

		$this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => $prefix . 'fraction_typography', 'selector' => $selector_fraction ] );
		$this->add_control( $prefix . 'fraction_color', [ 'label' => esc_html__( 'Text Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $selector_fraction => 'color: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Background::get_type(), [ 'name' => $prefix . 'fraction_background', 'selector' => $selector_fraction, 'types' => ['classic', 'gradient'] ] );
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => $prefix . 'fraction_border', 'selector' => $selector_fraction ] );
		$this->add_responsive_control( $prefix . 'fraction_radius', [ 'label' => esc_html__( 'Border Radius', 'sifency-addons' ), 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ $selector_fraction => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		$this->add_responsive_control( $prefix . 'fraction_padding', [ 'label' => esc_html__( 'Padding', 'sifency-addons' ), 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ $selector_fraction => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		$this->add_responsive_control(
			$prefix . 'position_top',
			[
				'label' => esc_html__( 'Top', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					$selector_fraction => 'top: {{SIZE}}{{UNIT}}; bottom: auto; right: auto; left: auto;',
				],
			]
		);

		$this->add_responsive_control(
			$prefix . 'position_right',
			[
				'label' => esc_html__( 'Right', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					$selector_fraction => 'right: {{SIZE}}{{UNIT}}; top: auto; bottom: auto; left: auto;',
				],
			]
		);

		$this->add_responsive_control(
			$prefix . 'position_bottom',
			[
				'label' => esc_html__( 'Bottom', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					$selector_fraction => 'bottom: {{SIZE}}{{UNIT}}; top: auto; right: auto; left: auto;',
				],
			]
		);

		$this->add_responsive_control(
			$prefix . 'position_left',
			[
				'label' => esc_html__( 'Left', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					$selector_fraction => 'left: {{SIZE}}{{UNIT}}; top: auto; right: auto; bottom: auto;',
				],
			]
		);

		$this->end_controls_section();

		//======================================================================
		//==         STYLE SECTION: PAGINATION (PROGRESS BAR)               ==
		//======================================================================
		$this->start_controls_section(
			$prefix . 'progressbar_style_section',
			[
				'label'     => esc_html__( 'Pagination (Progress Bar)', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ $condition_key => 'progressbar' ],
			]
		);

		$selector_progressbar = '{{WRAPPER}} .swiper-pagination-progressbar';
		$selector_progressbar_fill = '{{WRAPPER}} .swiper-pagination-progressbar-fill';

		$this->add_control(
			$prefix . 'progressbar_position',
			[
				'label'   => esc_html__( 'Position', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => [ 'top' => [ 'title' => 'Top', 'icon' => 'eicon-v-align-top' ], 'bottom' => [ 'title' => 'Bottom', 'icon' => 'eicon-v-align-bottom' ] ],
				'default' => 'bottom',
				'selectors_dictionary' => [ 'top' => 'top: 0; bottom: auto;', 'bottom' => 'bottom: 0; top: auto;' ],
				'selectors' => [ $selector_progressbar => '{{VALUE}}' ],
			]
		);

		$this->add_responsive_control( $prefix . 'progressbar_height', [ 'label' => esc_html__( 'Height', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 1, 'max' => 20]], 'selectors' => [ $selector_progressbar => 'height: {{SIZE}}{{UNIT}};' ] ] );
		$this->add_control( $prefix . 'progressbar_track_color', [ 'label' => esc_html__( 'Track Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $selector_progressbar => 'background: {{VALUE}};' ] ] );
		$this->add_control( $prefix . 'progressbar_fill_color', [ 'label' => esc_html__( 'Fill Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $selector_progressbar_fill => 'background: {{VALUE}};' ] ] );
		$this->add_responsive_control( $prefix . 'progressbar_radius', [ 'label' => esc_html__( 'Border Radius', 'sifency-addons' ), 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ $selector_progressbar . ', ' . $selector_progressbar_fill => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );

		$this->end_controls_section();
	}
}