<?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; }

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

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

	/**
	 * 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_addons_swiper_slider_pagination_style_controls( $args = array() ) {
		$args = wp_parse_args(
			$args,
			array(
				'prefix'                  => 'pagination_',
				'pagination_control_name' => 'show_pagination',
			)
		);

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

		// ======================================================================
		// ==         STYLE SECTION: PAGINATION (BULLETS)                    ==
		// ======================================================================
		$this->start_controls_section(
			$prefix . 'bullets_style_section',
			array(
				'label'     => esc_html__( 'Pagination (Bullets)', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( $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',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default'   => 'center',
				'selectors' => array( $selector_container => 'text-align: {{VALUE}};' ),
			)
		);

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

		$this->add_responsive_control(
			$prefix . 'bullets_vertical_offset',
			array(
				'label'     => esc_html__( 'Vertical Offset', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => -200,
						'max' => 200,
					),
				),
				'selectors' => array( $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', array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) ) );
		$this->add_responsive_control(
			$prefix . 'bullets_width',
			array(
				'label'     => esc_html__( 'Width', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( $selector_bullet => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_control(
			$prefix . 'bullets_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $selector_bullet => 'background: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'bullets_border',
				'selector' => $selector_bullet,
			)
		);
		$this->add_responsive_control(
			$prefix . 'bullets_radius',
			array(
				'label'     => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( $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', array( 'label' => esc_html__( 'Active', 'sifency-addons' ) ) );
		$this->add_responsive_control(
			$prefix . 'bullets_active_width',
			array(
				'label'     => esc_html__( 'Width', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( $selector_bullet_active => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_control(
			$prefix . 'bullets_active_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $selector_bullet_active => 'background: {{VALUE}};' ),
			)
		);
		$this->add_control(
			$prefix . 'bullets_active_border_color',
			array(
				'label'     => esc_html__( 'Border Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $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',
			array(
				'label'     => esc_html__( 'Pagination (Fraction)', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( $condition_key => 'fraction' ),
			)
		);

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

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

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

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

		$this->add_responsive_control(
			$prefix . 'position_left',
			array(
				'label'      => esc_html__( 'Left', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					$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',
			array(
				'label'     => esc_html__( 'Pagination (Progress Bar)', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( $condition_key => 'progressbar' ),
			)
		);

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

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

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

		$this->end_controls_section();
	}
	protected function render_sifency_addons_slider_pagination( $settings ) {

		if ( empty( $settings['show_pagination'] ) ) {
			return;
		}
		?>
		<div class="swiper-pagination"></div>
		<?php
	}
}
