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

trait Sifency_Addons_Toggle_Style_Controls {

	/**
	 * Reusable Advanced Toggle Switch Style Controls
	 */
	protected function add_sifency_addons_toggle_style_controls( $args = array() ) {

		$defaults = array(
			'prefix'    => 'toggle_',
			'label'     => esc_html__( 'Advanced Toggle Switch', 'sifency-addons' ),
			'selector'  => '.sifency-content-toggle-switch',
			'condition' => array(),
		);

		$args = wp_parse_args( $args, $defaults );

		$prefix   = $args['prefix'];
		$base_sel = '{{WRAPPER}} ' . $args['selector'];

		$custom_selectors = isset( $args['selectors'] ) ? $args['selectors'] : array();

		$default_selectors = array(
			'nav'          => '{{WRAPPER}} .sifency-toggle-nav',
			'track'        => $base_sel . ' .toggle-slider',
			'thumb'        => $base_sel . ' .toggle-slider::before',
			'active_track' => $base_sel . ' input:checked + .toggle-slider',
			'active_thumb' => $base_sel . ' input:checked + .toggle-slider::before',
			'hover_track'  => $base_sel . ':hover .toggle-slider',
			'hover_thumb'  => $base_sel . ':hover .toggle-slider::before',
		);
		$selectors         = wp_parse_args( $custom_selectors, $default_selectors );
		foreach ( $selectors as $key => $selector ) {
			if ( strpos( $selector, '{{WRAPPER}}' ) === false ) {
				$selectors[ $key ] = '{{WRAPPER}} ' . $selector;
			}
		}

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

		// --- Layout Controls ---
		$this->add_responsive_control(
			$prefix . 'container_gap',
			array(
				'label'     => esc_html__( 'Labels Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( $selectors['nav'] => 'gap: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_responsive_control(
			$prefix . 'width',
			array(
				'label'     => esc_html__( 'Switch Width', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 30,
						'max' => 250,
					),
				),
				'selectors' => array( $base_sel => 'width: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_responsive_control(
			$prefix . 'height',
			array(
				'label'     => esc_html__( 'Switch Height', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'selectors' => array(
					$base_sel           => 'height: {{SIZE}}{{UNIT}};',
					$selectors['thumb'] => 'height: calc({{SIZE}}{{UNIT}} - 8px); width: calc({{SIZE}}{{UNIT}} - 8px); top: 50%; transform: translateY(-50%);',
				),
			)
		);

		$this->add_responsive_control(
			$prefix . 'thumb_width',
			array(
				'label'     => esc_html__( 'Thumb Width', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 5,
						'max' => 100,
					),
				),
				'selectors' => array(
					$selectors['thumb'] => 'width: {{SIZE}}{{UNIT}};',
				),
				'separator' => 'before',
			)
		);

		$this->add_responsive_control(
			$prefix . 'thumb_height',
			array(
				'label'     => esc_html__( 'Thumb Height', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 5,
						'max' => 100,
					),
				),
				'selectors' => array(
					$selectors['thumb'] => 'height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		// --- COLOR TABS (NORMAL / ACTIVE) ---
		$this->start_controls_tabs( $prefix . 'color_tabs' );

		$this->start_controls_tab( $prefix . 'color_normal', array( 'label' => 'Normal' ) );

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => $prefix . 'bg_normal',
				'selector' => $selectors['track'],
			)
		);

		$this->add_control(
			$prefix . 'thumb_color_normal',
			array(
				'label'     => 'Thumb Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $selectors['thumb'] => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => $prefix . 'sh_normal',
				'selector' => $selectors['track'],
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab( $prefix . 'color_active', array( 'label' => 'Active' ) );

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => $prefix . 'bg_active',
				'selector' => $selectors['active_track'],
			)
		);

		$this->add_control(
			$prefix . 'thumb_color_active',
			array(
				'label'     => 'Thumb Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $selectors['active_thumb'] => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => $prefix . 'sh_active',
				'selector' => $selectors['active_track'],
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();

		// --- BORDER SETTINGS ---
		$this->add_control( $prefix . 'hr_border', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			$prefix . 'heading_track_border',
			array(
				'label' => 'Track Border',
				'type'  => Controls_Manager::HEADING,
			)
		);
		$this->start_controls_tabs( $prefix . 'tabs_track_border' );
		$this->start_controls_tab( $prefix . 'tr_b_n', array( 'label' => 'Normal' ) );
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'tr_b_n',
				'selector' => $selectors['track'],
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( $prefix . 'tr_b_h', array( 'label' => 'Hover' ) );
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'tr_b_h',
				'selector' => $selectors['hover_track'],
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( $prefix . 'tr_b_a', array( 'label' => 'Active' ) );
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'tr_b_a',
				'selector' => $selectors['active_track'],
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->add_control(
			$prefix . 'heading_thumb_border',
			array(
				'label'     => 'Thumb Border',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->start_controls_tabs( $prefix . 'tabs_thumb_border' );
		$this->start_controls_tab( $prefix . 'th_b_n', array( 'label' => 'Normal' ) );
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'th_b_n',
				'selector' => $selectors['thumb'],
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( $prefix . 'th_b_h', array( 'label' => 'Hover' ) );
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'th_b_h',
				'selector' => $selectors['hover_thumb'],
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( $prefix . 'th_b_a', array( 'label' => 'Active' ) );
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'th_b_a',
				'selector' => $selectors['active_thumb'],
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();

		// --- Global Radius ---
		$this->add_responsive_control(
			$prefix . 'radius',
			array(
				'label'     => esc_html__( 'Global Radius', 'sifency-addons' ),
				'type'      => Controls_Manager::DIMENSIONS,
				'separator' => 'before',
				'selectors' => array(
					$selectors['track'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					$selectors['thumb'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}
}
