<?php
/**
 * Reusable Elementor trait for full ICON STYLE controls (Normal + Hover).
 * Applies styles to a single wrapper class: `.sifency-icon-style`
 *
 * @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_Box_Shadow;

/**
 * Trait Sifency_Icon_Style_Controls
 */
trait Sifency_Icon_Style_Controls {

	/**
	 * Add complete icon style controls (normal + hover) for any widget.
	 *
	 * @param array $args Optional customization.
	 */
	protected function add_sifency_icon_style_controls( $args = [] ) {

		$args = wp_parse_args(
			$args,
			[
				'prefix'    => 'icon_',
				'condition' => [],
			]
		);

		$prefix = $args['prefix'];

		$base_selector   = '{{WRAPPER}} .sifency-icon-style';
		$icon_selector_i = $base_selector . ' i';
		$icon_selector_svg = $base_selector . ' svg';

		$icon_selectors_string       = $icon_selector_i . ', ' . $icon_selector_svg;
		$hover_icon_selectors_string = $icon_selector_i . ':hover, ' . $icon_selector_svg . ':hover';
		$container_hover_icon_selectors_string = $base_selector . ':hover i, ' . $base_selector . ':hover svg';


		// =============================================================
		// STYLE SECTION
		// =============================================================
		$this->start_controls_section(
			$prefix . 'style_section',
			[
				'label'     => esc_html__( 'Icon Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => $args['condition'],
			]
		);

		$this->add_responsive_control(
			$prefix . 'size',
			[
				'label' => esc_html__( 'Icon Size', 'sifency-addons' ),
				'type'  => Controls_Manager::SLIDER,
				'range' => [
					'px' => [ 'min' => 6, 'max' => 300, ],
				],
				'selectors' => [
					$icon_selector_i   => 'font-size: {{SIZE}}{{UNIT}};',
					$icon_selector_svg => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		// -------------------------------------------------------------
		// TABS: Normal / Hover
		// -------------------------------------------------------------
		$this->start_controls_tabs( $prefix . 'style_tabs' );

		// =============================
		// NORMAL TAB
		// =============================
		$this->start_controls_tab(
			$prefix . 'normal_tab',
			[ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ]
		);

		$this->add_control(
			$prefix . 'color',
			[
				'label'     => esc_html__( 'Icon Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					$icon_selector_i => 'color: {{VALUE}}; fill: {{VALUE}};',
					$icon_selector_svg => 'color: {{VALUE}}; fill: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => $prefix . 'background',
				'selector' => $base_selector,
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => $prefix . 'border',
				'selector' => $base_selector,
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => $prefix . 'box_shadow',
				'selector' => $base_selector,
			]
		);

		$this->add_responsive_control(
			$prefix . 'rotation',
			[
				'label'      => esc_html__( 'Rotation', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'deg' ],
				'range'      => [ 'deg' => [ 'min' => -360, 'max' => 360, ], ],
				'selectors'  => [
					$base_selector => 'transform: rotate({{SIZE}}deg);',
				],
			]
		);

		$this->end_controls_tab();

		// =============================
		// HOVER TAB
		// =============================
		$this->start_controls_tab(
			$prefix . 'hover_tab',
			[ 'label' => esc_html__( 'Hover', 'sifency-addons' ) ]
		);

		$this->add_control(
			$prefix . 'hover_color',
			[
				'label'     => esc_html__( 'Icon Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					$container_hover_icon_selectors_string => 'color: {{VALUE}}; fill: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => $prefix . 'hover_background',
				'selector' => $base_selector . ':hover',
			]
		);

		$this->add_control(
			$prefix . 'hover_border_color',
			[
				'label'     => esc_html__( 'Border Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					$base_selector . ':hover' => 'border-color: {{VALUE}};',
				],
				'condition' => [
					$prefix . 'border_border!' => '',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => $prefix . 'hover_box_shadow',
				'selector' => $base_selector . ':hover',
			]
		);

		$this->add_responsive_control(
			$prefix . 'hover_rotation',
			[
				'label'      => esc_html__( 'Rotation', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'deg' ],
				'range'      => [ 'deg' => [ 'min' => -360, 'max' => 360, ], ],
				'selectors'  => [
					$base_selector . ':hover' => 'transform: rotate({{SIZE}}deg);',
				],
			]
		);

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

		// -------------------------------------------------------------
		// SIZING & SPACING (Moved outside tabs for better organization)
		// -------------------------------------------------------------
		$this->add_control(
			$prefix . 'divider_after_tabs',
			[ 'type' => Controls_Manager::DIVIDER, 'style' => 'thick' ]
		);

		$this->add_responsive_control(
			$prefix . 'padding',
			[
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors'  => [
					$base_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			$prefix . 'border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					$base_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

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

		$this->end_controls_section();
	}
}