<?php
namespace Sifency\Traits;

if ( ! defined( 'ABSPATH' ) ) {
	exit; }

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_Addons_Button_Style_Controls {

	protected function add_Sifency_Addons_Button_Style_Controls( $args = array() ) {

		$args = wp_parse_args(
			$args,
			array(
				'prefix'          => 'button_',
				'label'           => esc_html__( 'Button', 'sifency-addons' ),
				'selector'        => '.sifency-button-style',
				'hover_selector'  => '',
				'target_children' => false,
				'condition'       => array(),
				'show_typography' => true,
				'exclude'         => array(),
				'show_dimensions' => false,
				'hover_tab_label' => esc_html__( 'Hover', 'sifency-addons' ),
			)
		);

		$prefix  = $args['prefix'];
		$exclude = (array) $args['exclude'];

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

		$hover_selector = $args['hover_selector'];
		if ( empty( $hover_selector ) ) {
			$hover_selector = $base_selector . ':hover';
		} elseif ( strpos( $hover_selector, '{{WRAPPER}}' ) === false ) {
			$hover_selector = '{{WRAPPER}} ' . $hover_selector;
		}

		if ( $args['target_children'] ) {
			$tags       = array( '', 'span', 'i', 'svg', 'strong', 'em', 'b' );
			$text       = array();
			$hover_text = array();

			foreach ( $tags as $tag ) {
				$suffix       = $tag ? ' ' . $tag : '';
				$text[]       = $base_selector . $suffix;
				$hover_text[] = $hover_selector . $suffix;
			}

			$text_selector  = implode( ', ', $text );
			$hover_text_sel = implode( ', ', $hover_text );
		} else {
			$text_selector  = $base_selector;
			$hover_text_sel = $hover_selector;
		}
		if ( ! in_array( 'wrapper', $exclude, true ) ) {
			$this->start_controls_section(
				$prefix . 'style_section',
				array(
					'label'     => $args['label'],
					'tab'       => Controls_Manager::TAB_STYLE,
					'condition' => $args['condition'],
				)
			);}

		if ( $args['show_dimensions'] && ! in_array( 'dimensions', $exclude, true ) ) {

			$this->add_responsive_control(
				$prefix . 'width',
				array(
					'label'      => esc_html__( 'Width', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', '%', 'em', 'rem', 'vw' ),
					'selectors'  => array(
						$base_selector => 'width: {{SIZE}}{{UNIT}}; display:inline-flex; align-items:center; justify-content:center;',
					),
				)
			);

			$this->add_responsive_control(
				$prefix . 'height',
				array(
					'label'      => esc_html__( 'Height', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', 'em', 'rem', 'vh' ),
					'selectors'  => array(
						$base_selector => 'height: {{SIZE}}{{UNIT}}; display:inline-flex; align-items:center; justify-content:center;',
					),
				)
			);
		}

		if ( $args['show_typography'] && ! in_array( 'typography', $exclude, true ) ) {
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => $prefix . 'typography',
					'selector' => $text_selector,
				)
			);
		}

		if ( ! in_array( 'border', $exclude, true ) ) {

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

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

		if ( ! in_array( 'shadow', $exclude, true ) ) {
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => $prefix . 'shadow',
					'selector' => $base_selector,
				)
			);
		}

		if ( ! in_array( 'padding', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'padding',
				array(
					'label'     => esc_html__( 'Padding', 'sifency-addons' ),
					'type'      => Controls_Manager::DIMENSIONS,
					'selectors' => array(
						$base_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
				)
			);
		}

		if ( ! in_array( 'margin', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'margin',
				array(
					'label'     => esc_html__( 'Margin', 'sifency-addons' ),
					'type'      => Controls_Manager::DIMENSIONS,
					'selectors' => array(
						$base_selector => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
				)
			);
		}

		if ( ! in_array( 'transition', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'transition',
				array(
					'label'     => esc_html__( 'Transition (s)', 'sifency-addons' ),
					'type'      => Controls_Manager::SLIDER,
					'range'     => array(
						'px' => array(
							'min'  => 0,
							'max'  => 2,
							'step' => 0.05,
						),
					),
					'default'   => array( 'size' => 0.3 ),
					'selectors' => array(
						$base_selector => 'transition: all {{SIZE}}s ease;',
						$text_selector => 'transition: all {{SIZE}}s ease;',
					),
				)
			);
		}

		$this->start_controls_tabs( $prefix . 'tabs' );
		$this->start_controls_tab(
			$prefix . 'normal',
			array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) )
		);

		$this->add_control(
			$prefix . 'text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$text_selector => 'color: {{VALUE}}; fill: {{VALUE}};',
				),
			)
		);

		if ( ! in_array( 'background', $exclude, true ) ) {
			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => $prefix . 'background',
					'selector' => $base_selector,
				)
			);
		}

		$this->end_controls_tab();

		$this->start_controls_tab(
			$prefix . 'hover',
			array( 'label' => $args['hover_tab_label'] )
		);

		$this->add_control(
			$prefix . 'hover_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$hover_text_sel => 'color: {{VALUE}}; fill: {{VALUE}};',
				),
			)
		);

		if ( ! in_array( 'background', $exclude, true ) ) {
			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => $prefix . 'hover_background',
					'selector' => $hover_selector,
				)
			);
		}

		if ( ! in_array( 'border', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'hover_border_color',
				array(
					'label'     => esc_html__( 'Border Color', 'sifency-addons' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$hover_selector => 'border-color: {{VALUE}};',
					),
				)
			);
		}

		$this->end_controls_tab();

		$this->end_controls_tabs();
		if ( ! in_array( 'wrapper', $exclude, true ) ) {
			$this->end_controls_section();}
	}
}
