<?php
/**
 * A reusable Elementor trait for adding a comprehensive set of Swiper.js carousel controls
 * and generating the final options object for the frontend.
 *
 * @package Sifency\Traits
 */

namespace Sifency\Traits;

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

use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Slider_Nav_Style_Controls;
use Sifency\Traits\Sifency_Pagination_Style_Controls;

trait Sifency_Addons_Slider_Controls {
	use Sifency_Slider_Nav_Style_Controls;
    use Sifency_Pagination_Style_Controls;

	public function get_carousel_script_depends() {
		return [ 'swiper', 'sifency-carousel-handler', 'sifency-frontend-script'];
	}

	public function get_carousel_style_depends() {
		return [ 'swiper', 'sifency-swiper-style', 'sifency-frontend-style', 'sifency-icon' ];
	}

    protected function add_sifency_carousel_controls( array $args = [] ) {
		$args = wp_parse_args( $args, [
            'label'     => esc_html__('Carousel Settings', 'sifency-addons'),
            'condition' => [],
        ]);

		$this->start_controls_section( 'section_carousel_settings', $args );

		$this->add_responsive_control(
			'slides_per_view',
			[
				'label'   => esc_html__( 'Slides Per View', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 10,
				'default' => 3,
				'tablet_default' => 2,
				'mobile_default' => 1,
				'frontend_available' => true,
			]
		);

		$this->add_responsive_control(
			'space_between',
			[
				'label'   => esc_html__( 'Space Between (px)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 30,
				'tablet_default' => 20,
				'mobile_default' => 10,
				'frontend_available' => true,
			]
		);

		$this->add_control(
			'effect',
			[
				'label'   => esc_html__( 'Effect', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'slide'     => esc_html__( 'Slide', 'sifency-addons' ),
					'fade'      => esc_html__( 'Fade', 'sifency-addons' ),
					'cube'      => esc_html__( 'Cube', 'sifency-addons' ),
					'coverflow' => esc_html__( 'Coverflow', 'sifency-addons' ),
					'flip'      => esc_html__( 'Flip', 'sifency-addons' ),
				],
				'default' => 'slide',
				'frontend_available' => true,
				'help' => esc_html__( 'Note: The "Fade", "Cube", and "Flip" effects work best with Slides Per View set to 1.', 'sifency-addons' ),
			]
		);

		$this->add_control( 'speed', [ 'label' => esc_html__( 'Speed (ms)', 'sifency-addons' ), 'type' => Controls_Manager::NUMBER, 'default' => 500, 'frontend_available' => true ] );
		$this->add_control( 'autoplay', [ 'label' => esc_html__( 'Autoplay', 'sifency-addons' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'frontend_available' => true ] );
		$this->add_control( 'autoplay_delay', [ 'label' => esc_html__( 'Autoplay Delay (ms)', 'sifency-addons' ), 'type' => Controls_Manager::NUMBER, 'default' => 5000, 'condition' => [ 'autoplay' => 'yes' ], 'frontend_available' => true ] );
		$this->add_control( 'loop', [ 'label' => esc_html__( 'Infinite Loop', 'sifency-addons' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'frontend_available' => true ] );
		$this->add_control( 'pause_on_hover', [ 'label' => esc_html__( 'Pause on Hover', 'sifency-addons' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'frontend_available' => true ] );

		$this->add_control( 'hr_nav', [ 'type' => Controls_Manager::DIVIDER ] );

		$this->add_control( 'show_arrows', [ 'label' => esc_html__( 'Navigation Arrows', 'sifency-addons' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'no', 'frontend_available' => true ] );
		
		$this->add_control(
			'show_pagination',
			[
				'label'   => esc_html__( 'Pagination', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					''            => esc_html__( 'None', 'sifency-addons' ),
					'bullets'     => esc_html__( 'Bullets', 'sifency-addons' ),
					'fraction'    => esc_html__( 'Fraction', 'sifency-addons' ),
					'progressbar' => esc_html__( 'Progress Bar', 'sifency-addons' ),
				],
				'default' => '',
				'frontend_available' => true,
			]
		);

		$this->end_controls_section();

		$nav_condition = array_merge($args['condition'], ['show_arrows' => 'yes']);
		$pagination_condition = array_merge($args['condition'], ['show_pagination!' => '']);

		$this->add_sifency_slider_nav_style_controls([
            'condition' => $nav_condition,
        ]);
        $this->add_sifency_pagination_style_controls([
            'condition' => $pagination_condition,
            'pagination_control_name' => 'show_pagination',
        ]);
	}

	/**
	 * Processes widget settings and returns a Swiper.js-compatible options object.
	 * Includes "smart loop" logic and fixes for common effect issues.
	 *
	 * @param array $settings The widget's settings array.
	 * @param int   $post_count The total number of slides.
	 * @return array The options array for Swiper.
	 */
	protected function get_sifency_swiper_options( $settings, $post_count = 0 ) {
		$slides_per_view_desktop = ! empty( $settings['slides_per_view'] ) ? (int) $settings['slides_per_view'] : 3;

		$is_loop_enabled = ( 'yes' === ( $settings['loop'] ?? 'yes' ) );
		if ( $post_count > 0 && $post_count <= $slides_per_view_desktop ) {
			$is_loop_enabled = false;
		}

		$options = [
			'effect' => $settings['effect'] ?? 'slide',
			'speed'  => (int) ( $settings['speed'] ?? 500 ),
			'loop'   => $is_loop_enabled,
			'watchSlidesProgress' => true,
		];

		// --- Breakpoints for responsive slides
		$options['slidesPerView']  = ! empty( $settings['slides_per_view_mobile'] ) ? (int) $settings['slides_per_view_mobile'] : 1;
		$options['spaceBetween']   = ! empty( $settings['space_between_mobile'] ) ? (int) $settings['space_between_mobile'] : 10;
		$options['breakpoints'] = [
			'768' => [
				'slidesPerView' => ! empty( $settings['slides_per_view_tablet'] ) ? (int) $settings['slides_per_view_tablet'] : 2,
				'spaceBetween'  => ! empty( $settings['space_between_tablet'] ) ? (int) $settings['space_between_tablet'] : 20,
			],
			'1024' => [
				'slidesPerView' => $slides_per_view_desktop,
				'spaceBetween'  => ! empty( $settings['space_between'] ) ? (int) $settings['space_between'] : 30,
			],
		];
		
		if ( in_array( $options['effect'], ['fade', 'cube', 'flip'] ) ) {
			$options['slidesPerView'] = 1;
			$options['breakpoints']['768']['slidesPerView'] = 1;
			$options['breakpoints']['1024']['slidesPerView'] = 1;
			$options['spaceBetween'] = 0;
			$options['breakpoints']['768']['spaceBetween'] = 0;
			$options['breakpoints']['1024']['spaceBetween'] = 0;
		}

		// --- Autoplay
		if ( 'yes' === ( $settings['autoplay'] ?? 'yes' ) ) {
			$options['autoplay'] = [
				'delay' => (int) ( $settings['autoplay_delay'] ?? 5000 ),
				'disableOnInteraction' => ( 'yes' === ( $settings['pause_on_hover'] ?? 'yes' ) ) ? false : true,
			];
		}

		// --- Navigation
		if ( 'yes' === ( $settings['show_arrows'] ?? 'no' ) ) {
			$options['navigation'] = [
				'nextEl' => '.sifency-slider-nav-next',
				'prevEl' => '.sifency-slider-nav-prev',
			];
		}

		// --- Pagination
		if ( ! empty( $settings['show_pagination'] ) ) {
			$options['pagination'] = [
				'el'        => '.swiper-pagination',
				'type'      => $settings['show_pagination'],
				'clickable' => ( 'bullets' === $settings['show_pagination'] ),
			];
		}

		return $options;
	}
}