<?php
namespace Sifency\Traits;

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

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

trait Sifency_Addons_Image_Style_Controls {

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

		$defaults = array(
			'prefix'          => 'image_',
			'label'           => esc_html__( 'Image', 'sifency-addons' ),
			'selector'        => '.sifency-image-container',
			'hover_selector'  => '',
			'condition'       => array(),
			'exclude'         => array(),
			'show_max_height' => false,
			'show_overlay'    => false,
			'tab'             => Controls_Manager::TAB_STYLE,
			'selectors'       => array(),
		);

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

		/* ---------------- PREFIX ---------------- */
		$prefix    = rtrim( $args['prefix'], '_' ) . '_';
		$exclude   = (array) $args['exclude'];
		$show_tabs = ! in_array( 'tabs', $exclude, true );

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

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

		$custom_selectors = $args['selectors'];

		$default_map = array(
			'container'  => $full_base_sel,
			'dimensions' => $full_base_sel,
			'image'      => $full_base_sel . ' img',
			'overlay'    => $full_base_sel . ' .sifency-image-overlay',
		);

		$selectors_map = wp_parse_args( $custom_selectors, $default_map );

		foreach ( $selectors_map as $key => $val ) {
			if ( strpos( $val, '{{WRAPPER}}' ) === false ) {
				$selectors_map[ $key ] = '{{WRAPPER}} ' . $val;
			}
		}

		$selectors_map['hover_img']  = $hover_trigger . ' ' . ( isset( $custom_selectors['image'] ) ? $custom_selectors['image'] : 'img' );
		$selectors_map['hover_ovl']  = $hover_trigger . ' ' . ( isset( $custom_selectors['overlay'] ) ? $custom_selectors['overlay'] : '.sifency-image-overlay' );
		$selectors_map['hover_base'] = $hover_trigger;

		/*
		=====================================
		 * STYLE SECTION — IMAGE
		 * ===================================== */
		$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' ),
					'selectors'  => array( $selectors_map['dimensions'] => '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( $selectors_map['dimensions'] => 'height: {{SIZE}}{{UNIT}};' ),
				)
			);
			$this->add_responsive_control(
				$prefix . 'max_height',
				array(
					'label'      => esc_html__( 'Max Height', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', 'vh', '%' ),
					'selectors'  => array( $selectors_map['dimensions'] => 'max-height: {{SIZE}}{{UNIT}};' ),
				)
			);

			$this->add_control(
				$prefix . 'object_fit',
				array(
					'label'      => esc_html__( 'Object Fit', 'sifency-addons' ),
					'type'       => Controls_Manager::SELECT,
					'options'    => array(
						''        => esc_html__( 'Default', 'sifency-addons' ),
						'fill'    => esc_html__( 'Fill', 'sifency-addons' ),
						'cover'   => esc_html__( 'Cover', 'sifency-addons' ),
						'contain' => esc_html__( 'Contain', 'sifency-addons' ),
					),
					'selectors'  => array( $selectors_map['image'] => 'object-fit: {{VALUE}};' ),
					'conditions' => array(
						'relation' => 'or',
						'terms'    => array(
							array(
								'name'     => $prefix . 'height[size]',
								'operator' => '!=',
								'value'    => '',
							),
							array(
								'name'     => $prefix . 'max_height[size]',
								'operator' => '!=',
								'value'    => '',
							),
						),
					),
				)
			);
		}
		if ( $args['show_max_height'] && ! in_array( 'max_height', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'max_height',
				array(
					'label'      => esc_html__( 'Max Height', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', 'vh', '%' ),
					'selectors'  => array( $selectors_map['dimensions'] => 'max-height: {{SIZE}}{{UNIT}};' ),
				)
			);
		}
		/* ---------- SPACING ---------- */
		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( $selectors_map['container'] => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				)
			);
		}

		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( $selectors_map['container'] => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				)
			);
		}

		/* ---------- TABS START ---------- */
		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( '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( $selectors_map['image'] => 'opacity: {{SIZE}};' ),
				)
			);
		}

		if ( ! in_array( 'filters', $exclude, true ) ) {
			$this->add_group_control(
				Group_Control_Css_Filter::get_type(),
				array(
					'name'     => $prefix . 'css_filters',
					'selector' => $selectors_map['image'],
				)
			);
		}
		if ( ! in_array( 'border', $exclude, true ) ) {
			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => $prefix . 'border',
					'selector' => $selectors_map['container'],
				)
			);}

		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(
						$selectors_map['container'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;',
						$selectors_map['image']     => '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 . 'box_shadow',
					'selector' => $selectors_map['container'],
				)
			);
		}

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

			/* ===== HOVER TAB ===== */
			$this->start_controls_tab( $prefix . 'hover_tab', array( 'label' => esc_html__( 'Hover', 'sifency-addons' ) ) );

			if ( ! in_array( 'opacity', $exclude, true ) ) {
				$this->add_control(
					$prefix . 'hover_opacity',
					array(
						'label'     => esc_html__( 'Opacity', 'sifency-addons' ),
						'type'      => Controls_Manager::SLIDER,
						'selectors' => array( $selectors_map['hover_img'] => 'opacity: {{SIZE}};' ),
					)
				);
			}

			if ( ! in_array( 'filters', $exclude, true ) ) {
				$this->add_group_control(
					Group_Control_Css_Filter::get_type(),
					array(
						'name'     => $prefix . 'hover_css_filters',
						'selector' => $selectors_map['hover_img'],
					)
				);
			}
			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( $selectors_map['hover_base'] => 'border-color: {{VALUE}};' ),
					)
				);}

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => $prefix . 'hover_box_shadow',
					'selector' => $selectors_map['hover_base'],
				)
			);

			$this->add_control(
				$prefix . 'hover_animation',
				array(
					'label' => esc_html__( 'Hover Animation', 'sifency-addons' ),
					'type'  => Controls_Manager::HOVER_ANIMATION,
				)
			);

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

		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,
					'selectors' => array(
						$selectors_map['container'] . ', ' . $selectors_map['image'] . ', ' . $selectors_map['overlay'] => 'transition: all {{SIZE}}s ease;',
					),
				)
			);
		}

		$this->end_controls_section();

		/* ================= OVERLAY SECTION ================= */
		if ( $args['show_overlay'] && ! in_array( 'overlay', $exclude, true ) ) {
			$this->start_controls_section(
				$prefix . 'overlay_style_section',
				array(
					'label'     => $args['label'] . ' ' . esc_html__( 'Overlay', 'sifency-addons' ),
					'tab'       => Controls_Manager::TAB_STYLE,
					'condition' => $args['condition'],
				)
			);
			$this->add_control(
				$prefix . 'overlay_visibility',
				array(
					'label'   => esc_html__( 'Visibility', 'sifency-addons' ),
					'type'    => Controls_Manager::SELECT,
					'options' => array(
						'none'   => 'None',
						'always' => 'Always',
						'hover'  => 'On Hover',
					),
					'default' => 'none',
				)
			);

			$this->start_controls_tabs( $prefix . 'overlay_tabs', array( 'condition' => array( $prefix . 'overlay_visibility!' => 'none' ) ) );
			$this->start_controls_tab( $prefix . 'overlay_normal_tab', array( 'label' => 'Normal' ) );
			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => $prefix . 'overlay_background',
					'selector' => $selectors_map['overlay'],
				)
			);
			$this->end_controls_tab();

			$this->start_controls_tab( $prefix . 'overlay_hover_tab', array( 'label' => 'Hover' ) );
			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => $prefix . 'overlay_hover_background',
					'selector' => $selectors_map['hover_ovl'],
				)
			);
			$this->end_controls_tab();
			$this->end_controls_tabs();
			$this->end_controls_section();
		}
	}
}
