<?php
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_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;

/**
 * Trait Sifency_Addons_Slider_Nav
 */
trait Sifency_Addons_Slider_Nav {

	/**
	 * Adds the complete slider navigation styling control panel.
	 */
	protected function add_sifency_addons_slider_nav_style_controls( $args = array() ) {
		$args   = wp_parse_args(
			$args,
			array(
				'prefix'    => 'nav_',
				'condition' => array(),
			)
		);
		$prefix = $args['prefix'];

		$selectors = array(
			'container'    => '{{WRAPPER}} .sifency-slider-nav-container',
			'arrow_common' => '{{WRAPPER}} .sifency-slider-nav-arrow',
			'arrow_prev'   => '{{WRAPPER}} .sifency-slider-nav-prev',
			'arrow_next'   => '{{WRAPPER}} .sifency-slider-nav-next',
			'icon_common'  => '{{WRAPPER}} .sifency-slider-nav-arrow i, {{WRAPPER}} .sifency-slider-nav-arrow svg',
		);

		$this->start_controls_section(
			$prefix . 'style_section',
			array(
				'label'     => esc_html__( 'Slider Navigation', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => $args['condition'],
			)
		);

		// --- General Arrow Styling ---
		$this->add_control(
			$prefix . 'heading_arrows_style',
			array(
				'label' => esc_html__( 'Arrow Styling', 'sifency-addons' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

		$this->add_responsive_control(
			$prefix . 'icon_size',
			array(
				'label'     => esc_html__( 'Icon Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'selectors' => array(
					$selectors['icon_common'] => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-slider-nav-arrow svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			$prefix . 'box_size',
			array(
				'label'     => esc_html__( 'Box Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 20,
						'max' => 200,
					),
				),
				'selectors' => array(
					$selectors['arrow_common'] => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}}; line-height: {{SIZE}}{{UNIT}}; display: flex; align-items: center; justify-content: center;',
				),
			)
		);

		// --- Normal & Hover Tabs ---
		$this->start_controls_tabs( $prefix . 'style_tabs' );
		$this->start_controls_tab( $prefix . 'normal_tab', array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) ) );
		$this->add_control(
			$prefix . 'icon_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $selectors['icon_common'] => 'color: {{VALUE}}; fill: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => $prefix . 'background',
				'selector' => $selectors['arrow_common'],
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'border',
				'selector' => $selectors['arrow_common'],
			)
		);
		$this->add_responsive_control(
			$prefix . 'border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array( $selectors['arrow_common'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => $prefix . 'box_shadow',
				'selector' => $selectors['arrow_common'],
			)
		);
		$this->end_controls_tab();

		$this->start_controls_tab( $prefix . 'hover_tab', array( 'label' => esc_html__( 'Hover', 'sifency-addons' ) ) );
		$this->add_control(
			$prefix . 'hover_icon_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$selectors['arrow_common'] . ':hover i'   => 'color: {{VALUE}};',
					$selectors['arrow_common'] . ':hover svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => $prefix . 'hover_background',
				'selector' => $selectors['arrow_common'] . ':hover',
			)
		);
		$this->add_control(
			$prefix . 'hover_border_color',
			array(
				'label'     => esc_html__( 'Border Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $selectors['arrow_common'] . ':hover' => 'border-color: {{VALUE}};' ),
				'condition' => array( $prefix . 'border_border!' => '' ),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => $prefix . 'hover_box_shadow',
				'selector' => $selectors['arrow_common'] . ':hover',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();

		// --- Positioning ---
		$this->add_control(
			$prefix . 'heading_positioning',
			array(
				'label'     => esc_html__( 'Positioning', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			$prefix . 'heading_prev',
			array(
				'label' => esc_html__( 'Previous Arrow', 'sifency-addons' ),
				'type'  => Controls_Manager::HEADING,
			)
		);
		$this->add_responsive_control(
			$prefix . 'prev_vertical_pos',
			array(
				'label'      => esc_html__( 'Vertical Position', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -200,
						'max' => 500,
					),
					'%'  => array(
						'min' => -100,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 50,
				),
				'selectors'  => array( $selectors['arrow_prev'] => 'top: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			$prefix . 'prev_horizontal_pos',
			array(
				'label'      => esc_html__( 'Horizontal Position', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -200,
						'max' => 500,
					),
					'%'  => array(
						'min' => -100,
						'max' => 100,
					),
				),
				'selectors'  => array( $selectors['arrow_prev'] => 'left: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_control(
			$prefix . 'heading_next',
			array(
				'label'     => esc_html__( 'Next Arrow', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_responsive_control(
			$prefix . 'next_vertical_pos',
			array(
				'label'      => esc_html__( 'Vertical Position', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -200,
						'max' => 500,
					),
					'%'  => array(
						'min' => -100,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 50,
				),
				'selectors'  => array( $selectors['arrow_next'] => 'top: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			$prefix . 'next_horizontal_pos',
			array(
				'label'      => esc_html__( 'Horizontal Position', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -200,
						'max' => 500,
					),
					'%'  => array(
						'min' => -100,
						'max' => 100,
					),
				),
				'selectors'  => array( $selectors['arrow_next'] => 'right: {{SIZE}}{{UNIT}};' ),
			)
		);

		// --- Transition ---
		$this->add_control(
			$prefix . 'transition_duration',
			array(
				'label'     => esc_html__( 'Transition Duration (s)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'max'  => 2,
						'step' => 0.1,
					),
				),
				'default'   => array( 'size' => 0.3 ),
				'separator' => 'before',
				'selectors' => array(
					$selectors['arrow_common'] => 'transition: color {{SIZE}}s ease, background {{SIZE}}s ease, border-color {{SIZE}}s ease, box-shadow {{SIZE}}s ease;',
					$selectors['icon_common']  => 'transition: color {{SIZE}}s ease, fill {{SIZE}}s ease;',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Renders the navigation arrows HTML.
	 */
	protected function render_sifency_addons_slider_nav( $settings ) {
		$show_arrows = isset( $settings['show_arrows'] ) ? $settings['show_arrows'] : '';

		if ( 'yes' !== $show_arrows ) {
			return;
		}

		$prev_icon = $settings['arrow_prev_icon'] ?? array(
			'value'   => 'fas fa-chevron-left',
			'library' => 'fa-solid',
		);
		$next_icon = $settings['arrow_next_icon'] ?? array(
			'value'   => 'fas fa-chevron-right',
			'library' => 'fa-solid',
		);
		?>
		<div class="sifency-slider-nav-container">
			<div class="swiper-button-prev sifency-slider-nav-arrow sifency-slider-nav-prev">
				<?php
				\Elementor\Icons_Manager::render_icon( $prev_icon, array( 'aria-hidden' => 'true' ) );
				?>
			</div>
			<div class="swiper-button-next sifency-slider-nav-arrow sifency-slider-nav-next">
				<?php
				\Elementor\Icons_Manager::render_icon( $next_icon, array( 'aria-hidden' => 'true' ) );
				?>
			</div>
		</div>
		<?php
	}
}
