<?php
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;
trait Sifency_Button_Controls {
	protected function add_sifency_button_controls( $args = [] ) {
		$this->start_controls_section(
			'section_content_button',
			[ 'label' => esc_html__( 'Button', 'sifency-addons' ) ]
		);

		$this->add_control(
			'button_style_type',
			[
				'label' => esc_html__( 'Button Style', 'sifency-addons' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'primary',
				'options' => [
					'primary'   => esc_html__( 'Primary (Gradient)', 'sifency-addons' ),
					'secondary' => esc_html__( 'Secondary (Light)', 'sifency-addons' ),
					'outline'   => esc_html__( 'Outline', 'sifency-addons' ),
					'flip_text'   => esc_html__( 'Flip Text', 'sifency-addons' ),
					'circle'   => esc_html__( 'Circle', 'sifency-addons' ),
					'ghost'     => esc_html__( 'Ghost (Minimal)', 'sifency-addons' ),
					'icon-only' => esc_html__( 'Icon Only', 'sifency-addons' ),
				],
				'prefix_class' => 'sifency-button-style--',
				'render_type' => 'template',
			]
		);

		$this->add_control( 'button_text', [ 'label' => 'Text', 'type' => Controls_Manager::TEXT, 'default' => 'Click Here', 'dynamic' => [ 'active' => true ] ] );
		$this->add_control( 'button_link', [ 'label' => 'Link', 'type' => Controls_Manager::URL, 'default' => [ 'url' => '#' ], 'dynamic' => [ 'active' => true ] ] );
		$this->add_control( 'button_icon', [
			'label' => 'Icon',
			'type' => Controls_Manager::ICONS,
			'separator' => 'before',
			'default' => [
				'value' => 'icon-right',
				'library' => 'sifency-icons',
			],
		] );
		$this->add_control( 'icon_position', [ 'label' => 'Icon Position', 'type' => Controls_Manager::SELECT, 'default' => 'after', 'options' => [ 'before' => 'Before Text', 'after'  => 'After Text' ], 'prefix_class' => 'sifency-button-icon--', 'condition' => [ 'button_icon[value]!' => '' ] ] );
		$this->add_responsive_control( 'icon_spacing', [ 'label' => 'Icon Spacing', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 50 ] ], 'selectors' => [ '{{WRAPPER}}.sifency-button-icon--after .sifency-icon-style' => 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}}.sifency-button-icon--before .sifency-icon-style' => 'margin-right: {{SIZE}}{{UNIT}};' ], 'condition' => [ 'button_icon[value]!' => '' ] ] );
		$this->add_responsive_control( 'align', [ 'label' => 'Alignment', 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'icon' => 'eicon-text-align-left' ], 'center' => [ 'icon' => 'eicon-text-align-center' ], 'right' => [ 'icon' => 'eicon-text-align-right' ], 'justify' => [ 'icon' => 'eicon-text-align-justify' ] ], 'prefix_class' => 'elementor-align%s-', 'default' => '' ] );
		$this->end_controls_section();
	}

	protected function add_sifency_circle_button_style_controls() {
		
		$condition = [ 'button_style_type' => 'circle' ];
		$selector  = '{{WRAPPER}} .sifency-button.sifency-circle-btn';

		$this->start_controls_section(
			'section_circle_button_style',
			[
				'label'     => esc_html__( 'Circle Button Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => $condition,
			]
		);

		$this->add_responsive_control(
			'circle_size',
			[
				'label'      => esc_html__( 'Size (Width/Height)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em', 'rem' ],
				'range'      => [
					'px' => [ 'min' => 50, 'max' => 300 ],
				],
				'default'    => [ 'unit' => 'px', 'size' => 150 ],
				'selectors'  => [
					$selector => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				],
				'condition'  => $condition,
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'      => 'circle_typography',
				'selector'  => "$selector, $selector span",
				'condition' => $condition,
			]
		);
		$this->start_controls_tabs( 'tabs_circle_button_style' );

		$this->start_controls_tab(
			'tab_circle_normal',
			[
				'label'     => esc_html__( 'Normal', 'sifency-addons' ),
				'condition' => $condition,
			]
		);

		$this->add_control(
			'circle_text_color',
			[
				'label'     => esc_html__( 'Text / Icon Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => [
					"$selector" => 'color: {{VALUE}};',
					"$selector span" => 'color: {{VALUE}};',
					"$selector i" => 'color: {{VALUE}};',
					"$selector svg" => 'fill: {{VALUE}};',
				],
				'condition' => $condition,
			]
		);

		$this->add_control(
			'circle_bg_color',
			[
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => 'transparent',
				'selectors' => [
					$selector => 'background-color: {{VALUE}};',
				],
				'condition' => $condition,
			]
		);

		$this->add_control(
			'circle_border_color',
			[
				'label'     => esc_html__( 'Border Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#54a0ff',
				'selectors' => [
					$selector => 'border-color: {{VALUE}};',
				],
				'condition' => $condition,
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'tab_circle_hover',
			[
				'label'     => esc_html__( 'Hover', 'sifency-addons' ),
				'condition' => $condition,
			]
		);

		$this->add_control(
			'circle_hover_text_color',
			[
				'label'     => esc_html__( 'Text / Icon Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#000000',
				'selectors' => [
					"$selector:hover" => 'color: {{VALUE}};',
					"$selector:hover span" => 'color: {{VALUE}};',
					"$selector:hover i" => 'color: {{VALUE}};',
					"$selector:hover svg" => 'fill: {{VALUE}};',
				],
				'condition' => $condition,
			]
		);

		// The Slide Effect Color (Targeting ::before)
		$this->add_control(
			'circle_hover_fill_color',
			[
				'label'     => esc_html__( 'Sliding Fill Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#54a0ff',
				'selectors' => [
					"$selector::before" => 'background-color: {{VALUE}};',
					"$selector:hover"   => 'border-color: {{VALUE}};',
				],
				'condition' => $condition,
			]
		);

		$this->add_control(
			'circle_hover_border_color',
			[
				'label'     => esc_html__( 'Border Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					"$selector:hover" => 'border-color: {{VALUE}};',
				],
				'condition' => $condition,
			]
		);

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

		// --- BORDER SETTINGS ---
		$this->add_control(
			'circle_border_width',
			[
				'label'      => esc_html__( 'Border Width', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px' ],
				'default'    => [ 'top' => 1, 'right' => 1, 'bottom' => 1, 'left' => 1, 'unit' => 'px' ],
				'separator'  => 'before',
				'selectors'  => [
					$selector => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; border-style: solid;',
				],
				'condition'  => $condition,
			]
		);
		$this->add_responsive_control(
			'circle_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'default'    => [
					'top'      => 50,
					'right'    => 50,
					'bottom'   => 50,
					'left'     => 50,
					'unit'     => '%',
					'isLinked' => true,
				],
				'separator'  => 'before',
				'selectors'  => [
					$selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'condition'  => $condition,
			]
		);

		$this->add_control(
			'circle_transition',
			[
				'label'     => esc_html__( 'Transition Duration (s)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => [ 'px' => [ 'min' => 0.1, 'max' => 2, 'step' => 0.1 ] ],
				'default'   => [ 'size' => 0.3 ],
				'selectors' => [
					"$selector::before" => 'transition: left {{SIZE}}s ease-in-out;',
					"$selector:hover::before" => 'transition: left {{SIZE}}s ease-in-out;',
				],
				'condition' => $condition,
			]
		);

		$this->end_controls_section();
	}

	protected function add_sifency_flip_button_style_controls( $args = [] ) {
		$args = wp_parse_args( $args, [
			'prefix' => 'flip_btn_',
		] );

		$prefix = $args['prefix'];

		$condition = [ 'button_style_type' => 'flip_text' ];

		$wrapper_selector = '{{WRAPPER}} .action-bar-button';
		$span_selector    = '{{WRAPPER}} .action-bar-button .flip-text span';
		$default_text_sel = '{{WRAPPER}} .action-bar-button .default-text';
		$hover_text_sel   = '{{WRAPPER}} .action-bar-button .hover-text';

		$this->start_controls_section(
			$prefix . 'style_section',
			[
				'label'     => esc_html__( 'Flip Button Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => $condition,
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'      => $prefix . 'typography',
				'selector'  => $span_selector,
				'condition' => $condition,
			]
		);

		$this->start_controls_tabs( $prefix . 'tabs' );

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

		$this->add_control(
			$prefix . 'text_color',
			[
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => [
					$default_text_sel => 'color: {{VALUE}};',
				],
				'condition' => $condition,
			]
		);

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

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

		// 3. Gradient Controls
		$this->add_control(
			$prefix . 'border_gradient_color_a',
			[
				'label'     => esc_html__( 'First Color', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#54a0ff',
				'condition' => array_merge( $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' => array_merge( $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' => array_merge( $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' => array_merge( $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' => [
					$wrapper_selector => '
						border-image: linear-gradient({{SIZE}}deg, {{' . $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' => array_merge( $condition, [ $prefix . 'border_gradient_switcher' => 'yes' ] ),
			]
		);

		$this->end_controls_tab();

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

		$this->add_control(
			$prefix . 'hover_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => [
					$hover_text_sel => 'color: {{VALUE}};',
				],
				'condition' => $condition,
			]
		);

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

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

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

		$this->add_control(
			$prefix . 'hr_spacing',
			[
				'type' => Controls_Manager::DIVIDER,
				'style' => 'thick',
				'condition' => $condition,
			]
		);

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

		$this->add_responsive_control(
			$prefix . 'padding',
			[
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'default'    => [
					'top' => 15, 'right' => 30, 'bottom' => 15, 'left' => 30, 'unit' => 'px',
				],
				'selectors'  => [
					$wrapper_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'condition'  => $condition,
			]
		);

		$this->add_control(
			$prefix . 'transition_speed',
			[
				'label'      => esc_html__( 'Flip Speed (s)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [
					'px' => [ 'min' => 0.1, 'max' => 2.0, 'step' => 0.1 ],
				],
				'default'    => [ 'size' => 0.4 ],
				'separator'  => 'before',
				'selectors'  => [
					$span_selector => 'transition: transform {{SIZE}}s ease, opacity {{SIZE}}s ease;',
				],
				'condition'  => $condition,
			]
		);

		$this->end_controls_section();
	}
}