<?php
/**
 * A reusable Elementor trait for adding a comprehensive "Button" style preset.
 * This is a STYLE-ONLY trait that applies its styles to the defined selector.
 *
 * @package Sifency\Traits
 */

namespace Sifency\Traits;

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

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

/**
 * Trait Sifency_Button_Style_Controls
 */
trait Sifency_Button_Style_Controls {

	/**
	 * Adds the complete button styling control panel to the current widget instance.
	 *
	 * @param array $args { 
	 *     Optional. Arguments to customize the controls.
	 *     @type string $prefix          A unique prefix for control names.
	 *     @type string $label           The label for the style section.
	 *     @type string $selector        The CSS class/selector to target (e.g. '.my-btn').
	 *     @type bool   $target_children Whether to target span/i/svg inside or just the selector.
	 * }
	 */
	protected function add_sifency_button_style_controls( $args = [] ) {
		$args = wp_parse_args( $args, [
			'prefix'          => 'button_',
			'label'           => esc_html__( 'Button Style', 'sifency-addons' ),
			'selector'        => '.sifency-button-style',
			'target_children' => true,
		] );

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

		if ( $args['target_children'] ) {
			$children_tags = [ '', 'span', 'i', 'svg', 'strong', 'em', 'b' ];
			
			$text_selectors_arr = [];
			$hover_text_selectors_arr = [];

			foreach ( $children_tags as $tag ) {
				$suffix = empty( $tag ) ? '' : ' ' . $tag;
				$text_selectors_arr[] = $base_selector . $suffix;
				// Hover logic: Parent:hover Child
				$hover_text_selectors_arr[] = $base_selector . ':hover' . $suffix;
			}

			$text_selector_string = implode( ', ', $text_selectors_arr );
			$hover_text_selector_string = implode( ', ', $hover_text_selectors_arr );

		} else {
			$text_selector_string = $base_selector;
			$hover_text_selector_string = $base_selector . ':hover';
		}

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

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[ 
				'name'     => $prefix . 'typography', 
				'selector' => $text_selector_string 
			] 
		);
		
		$this->add_control( 'hr_border_' . $prefix, [ 'type' => Controls_Manager::DIVIDER, 'style' => 'thick' ] );
		
		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			[
				'name'      => $prefix . 'border',
				'selector'  => $base_selector,
				'separator' => 'before',
			]
		);

		$this->add_control(
			$prefix . 'border_gradient_switcher',
			[
				'label'     => esc_html__( 'Enable Gradient Border', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::SWITCHER,
				'separator' => 'before',
				'condition' => [
					$prefix . 'border_border!' => '',
				],
			]
		);

		$this->add_control(
			$prefix . 'border_gradient_color_a',
			[
				'label'     => esc_html__( 'First Color', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#54a0ff',
				'condition' => [ $prefix . 'border_gradient_switcher' => 'yes' ],
			]
		);

		$this->add_control(
			$prefix . 'border_gradient_location_a',
			[
				'label'     => esc_html__( 'Location', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ '%' ],
				'default'   => [ 'unit' => '%', 'size' => 0 ],
				'condition' => [ $prefix . 'border_gradient_switcher' => 'yes' ],
			]
		);

		$this->add_control(
			$prefix . 'border_gradient_color_b',
			[
				'label'     => esc_html__( 'Second Color', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#ff6b6b',
				'condition' => [ $prefix . 'border_gradient_switcher' => 'yes' ],
			]
		);

		$this->add_control(
			$prefix . 'border_gradient_location_b',
			[
				'label'     => esc_html__( 'Location', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ '%' ],
				'default'   => [ 'unit' => '%', 'size' => 100 ],
				'condition' => [ $prefix . 'border_gradient_switcher' => 'yes' ],
			]
		);

		$this->add_control(
			$prefix . 'border_gradient_angle',
			[
				'label'     => esc_html__( 'Angle', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'deg' ],
				'range'     => [ 'deg' => [ 'min' => 0, 'max' => 360 ] ],
				'default'   => [ 'unit' => 'deg', 'size' => 180 ],
				'selectors' => [
					$base_selector => 'border-image: linear-gradient({{SIZE}}{{UNIT}}, {{' . $prefix . 'border_gradient_color_a.VALUE}} {{' . $prefix . 'border_gradient_location_a.SIZE}}%, {{' . $prefix . 'border_gradient_color_b.VALUE}} {{' . $prefix . 'border_gradient_location_b.SIZE}}%) 1; border-color: transparent !important;',
				],
				'condition' => [ $prefix . 'border_gradient_switcher' => 'yes' ],
			]
		);

		$this->add_responsive_control(
			$prefix . 'border_radius',
			[
				'label'     => 'Border Radius',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => [
					$base_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'condition' => [
					$prefix . 'border_fill_type'        => 'color',
					$prefix . 'border_gradient_switcher!' => 'no',
				],
			]
		);

		$this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => $prefix . 'box_shadow', 'selector' => $base_selector ] );
		$this->add_responsive_control( $prefix . 'padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ $base_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		$this->add_responsive_control( $prefix . 'margin', [ 'label' => 'Margin', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ $base_selector => 'margin: {{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: all {{SIZE}}s ease;',
					$text_selector_string => 'transition: all {{SIZE}}s ease;' 
				] 
			] 
		);
		
		$this->start_controls_tabs( $prefix . 'style_tabs' );

		$this->start_controls_tab( $prefix . 'normal_tab', [ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ] );
		
		$this->add_control( $prefix . 'text_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ $text_selector_string => 'color: {{VALUE}}; fill: {{VALUE}};' ] ] );
		
		$this->add_group_control( Group_Control_Background::get_type(), [ 'name' => $prefix . 'background', 'selector' => $base_selector ] );
		
		$this->end_controls_tab();

		// --- Hover Tab ---
		$this->start_controls_tab( $prefix . 'hover_tab', [ 'label' => esc_html__( 'Hover', 'sifency-addons' ) ] );
		
		$this->add_control( $prefix . 'hover_text_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ $hover_text_selector_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' => 'Border Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ $base_selector . ':hover' => 'border-color: {{VALUE}};' ] ] );
		
		$this->end_controls_tab();
		$this->end_controls_tabs();
		
		$this->end_controls_section();
	}
}