<?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_Icon_Style_Controls {

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

		$defaults = array(
			'prefix'          => 'icon_',
			'label'           => esc_html__( 'Icon Style', 'sifency-addons' ),
			'selector'        => '.sifency-icon-style',
			'hover_selector'  => '',
			'hover_tab_label' => esc_html__( 'Hover', 'sifency-addons' ),
			'target_children' => false,
			'show_dimensions' => false,
			'condition'       => array(),
			'exclude'         => array(),
			'tab'             => Controls_Manager::TAB_STYLE,
		);

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

		$prefix  = $args['prefix'];
		$exclude = $args['exclude'];

		$base_sel = $args['selector'];
		$base_sel = ( strpos( $base_sel, '{{WRAPPER}}' ) === false ) ? '{{WRAPPER}} ' . $base_sel : $base_sel;

		if ( ! empty( $args['hover_selector'] ) && strpos( $args['hover_selector'], '{{WRAPPER}}' ) === false ) {
			$args['hover_selector'] = '{{WRAPPER}} ' . $args['hover_selector'];
		}

		/*
		-------------------------
		 * Selectors
		 * ------------------------- */
		if ( $args['target_children'] ) {

			$wrapper = $base_sel . ' > *';
			$icon    = $wrapper . ' i, ' . $wrapper . ' svg';

			$wrapper_hover = $args['hover_selector']
				? $args['hover_selector'] . ' > *'
				: $base_sel . ' > *:hover';

			$icon_hover = $args['hover_selector']
				? $args['hover_selector'] . ' > * i, ' . $args['hover_selector'] . ' > * svg'
				: $base_sel . ' > *:hover i, ' . $base_sel . ' > *:hover svg';

		} else {

			$wrapper = $base_sel;
			$icon    = $base_sel . ' i, ' . $base_sel . ' svg';

			$wrapper_hover = $args['hover_selector'] ?: $base_sel . ':hover';
			$icon_hover    = $wrapper_hover . ' i, ' . $wrapper_hover . ' svg';
		}

		$selectors = array(
			'wrapper'       => $wrapper,
			'icon'          => $icon,
			'wrapper_hover' => $wrapper_hover,
			'icon_hover'    => $icon_hover,
		);

		/*
		==================================================
		 * SECTION: ICON STYLE
		 * ================================================== */
		$this->start_controls_section(
			$prefix . 'style_section',
			array(
				'label'     => $args['label'],
				'tab'       => $args['tab'],
				'condition' => $args['condition'],
			)
		);

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

		/* ---------- Box Dimensions ---------- */
		if ( $args['show_dimensions'] ) {

			$this->add_responsive_control(
				$prefix . 'box_width',
				array(
					'label'      => esc_html__( 'Box Width', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', '%', 'em' ),
					'selectors'  => array(
						$selectors['wrapper'] => 'width: {{SIZE}}{{UNIT}}; min-width: {{SIZE}}{{UNIT}};',
					),
				)
			);

			$this->add_responsive_control(
				$prefix . 'box_height',
				array(
					'label'      => esc_html__( 'Box Height', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', '%', 'em' ),
					'selectors'  => array(
						$selectors['wrapper'] => 'height: {{SIZE}}{{UNIT}}; line-height: {{SIZE}}{{UNIT}};',
					),
				)
			);
		}

		/* ---------- Tabs ---------- */
		$this->start_controls_tabs( $prefix . 'style_tabs' );

		/* ===== Normal ===== */
		$this->start_controls_tab( $prefix . 'normal_tab', array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) ) );

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

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

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

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

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

		$this->end_controls_tab();

		/* ===== Hover ===== */
		$this->start_controls_tab( $prefix . 'hover_tab', array( 'label' => $args['hover_tab_label'] ) );

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

		if ( ! in_array( 'bg', $exclude, true ) ) {
			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => $prefix . 'hover_background',
					'selector' => $selectors['wrapper_hover'],
				)
			);
		}

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

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

		if ( ! in_array( 'rotation', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'hover_rotation',
				array(
					'label'     => esc_html__( 'Rotation', 'sifency-addons' ),
					'type'      => Controls_Manager::SLIDER,
					'range'     => array(
						'deg' => array(
							'min' => -360,
							'max' => 360,
						),
					),
					'selectors' => array(
						$selectors['wrapper_hover'] => 'transform: rotate({{SIZE}}deg);',
					),
				)
			);
		}

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

		/* ---------- Box Model ---------- */
		$this->add_control( $prefix . 'hr', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_responsive_control(
			$prefix . 'margin',
			array(
				'label'     => esc_html__( 'Margin', 'sifency-addons' ),
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array(
					$selectors['wrapper'] => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

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

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

		$this->add_control(
			$prefix . 'transition',
			array(
				'label'     => esc_html__( 'Transition Duration (s)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'max'  => 2,
						'step' => 0.1,
					),
				),
				'default'   => array( 'size' => 0.3 ),
				'selectors' => array(
					$selectors['wrapper'] => 'transition: all {{SIZE}}s ease;',
					$selectors['icon']    => 'transition: all {{SIZE}}s ease;',
				),
			)
		);

		$this->end_controls_section();
	}
}
