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

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

		$defaults = array(
			'prefix'          => 'item_',
			'label'           => esc_html__( 'Item Style', 'sifency-addons' ),
			'selector'        => '.sifency-item-style',
			'hover_selector'  => '',
			'hover_tab_label' => esc_html__( 'Hover', 'sifency-addons' ),
			'target_children' => false,
			'condition'       => array(),
			'exclude'         => array(),
			'tab'             => Controls_Manager::TAB_STYLE,
			'show_opacity'    => false,
		);

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

		/* ---------------- PREFIX ---------------- */
		$prefix = rtrim( $args['prefix'], '_' ) . '_';

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

		$selector = $args['target_children'] ? $full_base_sel . ' > *' : $full_base_sel;

		if ( ! empty( $args['hover_selector'] ) ) {
			$hover_selector = ( strpos( $args['hover_selector'], '{{WRAPPER}}' ) === false ) ? '{{WRAPPER}} ' . $args['hover_selector'] : $args['hover_selector'];
		} else {
			$hover_selector = $selector . ':hover';
		}

		$exclude   = (array) $args['exclude'];
		$show_tabs = ! in_array( 'tabs', $exclude, true );

		/* ================= SECTION ================= */
		if ( ! in_array( 'wrapper', $exclude, true ) ) {
			$this->start_controls_section(
				$prefix . 'style_section',
				array(
					'label'     => $args['label'],
					'tab'       => $args['tab'],
					'condition' => $args['condition'],
				)
			);}

		/* ---------------- DIMENSIONS ---------------- */
		if ( ! 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', '%', 'vw' ),
					'range'      => array(
						'px' => array(
							'min' => 0,
							'max' => 1600,
						),
						'%'  => array(
							'min' => 0,
							'max' => 100,
						),
					),
					'selectors'  => array( $selector => 'width: {{SIZE}}{{UNIT}};' ),
				)
			);

			$this->add_responsive_control(
				$prefix . 'height',
				array(
					'label'      => esc_html__( 'Height', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', 'vh' ),
					'selectors'  => array( $selector => 'height: {{SIZE}}{{UNIT}};' ),
				)
			);
		}

		/* ================= TABS LOGIC ================= */
		if ( $show_tabs ) {
			$this->start_controls_tabs( $prefix . 'style_tabs' );
			$this->start_controls_tab( $prefix . 'normal_tab', array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) ) );
		}

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

			$this->add_control(
				$prefix . 'background_blur',
				array(
					'label'      => esc_html__( 'Background Blur', 'sifency-addons' ),
					'type'       => \Elementor\Controls_Manager::SLIDER,
					'size_units' => array( 'px' ),
					'range'      => array(
						'px' => array(
							'min'  => 0,
							'max'  => 100,
							'step' => 1,
						),
					),
					'selectors'  => array(
						$selector => 'backdrop-filter: blur({{SIZE}}{{UNIT}}); -webkit-backdrop-filter: blur({{SIZE}}{{UNIT}});',
					),
				)
			);
		}

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

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

		if ( $show_tabs ) {
			$this->end_controls_tab();

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

			if ( ! in_array( 'bg', $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}};' ),
					)
				);
			}

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

			if ( ! in_array( 'transform', $exclude, true ) ) {
				$this->add_control(
					$prefix . 'hover_transform',
					array(
						'label'     => esc_html__( 'Transform', 'sifency-addons' ),
						'type'      => Controls_Manager::SELECT,
						'options'   => array(
							''                  => esc_html__( 'None', 'sifency-addons' ),
							'translateY(-10px)' => esc_html__( 'Move Up', 'sifency-addons' ),
							'translateY(10px)'  => esc_html__( 'Move Down', 'sifency-addons' ),
							'scale(1.1)'        => esc_html__( 'Scale Up', 'sifency-addons' ),
							'scale(0.9)'        => esc_html__( 'Scale Down', 'sifency-addons' ),
						),
						'selectors' => array( $hover_selector => 'transform: {{VALUE}};' ),
					)
				);
			}

			$this->end_controls_tab();
			$this->end_controls_tabs();
		}
		if ( ! in_array( 'radius', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'border_radius',
				array(
					'label'     => esc_html__( 'Border Radius', 'sifency-addons' ),
					'type'      => Controls_Manager::DIMENSIONS,
					'selectors' => array( $selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				)
			);
		}
		/* ---------------- SPACING ---------------- */
		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( $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( $selector => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				)
			);}

		/* ---------------- TRANSITION ---------------- */
		if ( ! in_array( 'transition', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'transition_duration',
				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( $selector => 'transition: all {{SIZE}}s ease-in-out;' ),
				)
			);
		}
		if ( $args['show_opacity'] && ! in_array( 'opacity', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'opacity',
				array(
					'label'     => esc_html__( 'Opacity', 'sifency-addons' ),
					'type'      => Controls_Manager::SLIDER,
					'range'     => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.01,
					),
					'selectors' => array( $selector => 'opacity: {{SIZE}};' ),
				)
			);
		}

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