<?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;
use Elementor\Group_Control_Css_Filter;
use Elementor\Group_Control_Typography;

/**
 * A reusable Elementor control class for adding granular Odd/Even item styling options.
 *
 * @package Sifency
 * @version 1.0.5
 */
final class Sifency_Addons_Parity_Style_Controls {

	public static function register( \Elementor\Widget_Base $widget, array $args = array() ) {
		$default_selectors = array(
			'odd_item'  => '.odd-item',
			'even_item' => '.even-item',
			'image'     => 'img',
			'title'     => 'h1, h2, h3, h4, h5, h6',
			'text'      => 'p, span',
			'icon'      => 'i',
		);

		$config = wp_parse_args(
			$args,
			array(
				'section_id' => 'section_parity_styles',
				'selectors'  => array(),
			)
		);

		$config['selectors'] = wp_parse_args( $config['selectors'], $default_selectors );

		foreach ( $config['selectors'] as $key => $value ) {
			if ( ( $key === 'odd_item' || $key === 'even_item' ) && strpos( $value, '{{WRAPPER}}' ) === false ) {
				$config['selectors'][ $key ] = '{{WRAPPER}} ' . $value;
			}
		}

		$widget->start_controls_section(
			$config['section_id'],
			array(
				'label' => esc_html__( 'Odd/Even Styles', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$widget->add_control(
			'parity_pattern',
			array(
				'label'   => 'Pattern',
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'standard'    => 'Standard (Odd/Even)',
					'every_third' => 'Every 3rd',
					'custom'      => 'Custom',
				),
				'default' => 'standard',
			)
		);

		$widget->add_control(
			'custom_even_positions',
			array(
				'label'       => 'Highlighted Positions',
				'description' => 'Comma-separated (e.g., 2,5,8).',
				'type'        => Controls_Manager::TEXT,
				'default'     => '2,5,8',
				'condition'   => array( 'parity_pattern' => 'custom' ),
			)
		);

		$widget->start_controls_tabs( 'parity_element_style_tabs' );

		/**
		 * TAB: CONTAINER
		 */
		$widget->start_controls_tab( 'parity_tab_container', array( 'label' => 'Container' ) );
			self::add_container_controls( $widget, $config, 'odd', 'Standard Items' );
			self::add_container_controls( $widget, $config, 'even', 'Highlighted Items' );
		$widget->end_controls_tab();

		/**
		 * TAB: IMAGE
		 */
		$widget->start_controls_tab( 'parity_tab_image', array( 'label' => 'Image' ) );
			self::add_image_controls( $widget, $config, 'odd', 'Standard Items Image' );
			self::add_image_controls( $widget, $config, 'even', 'Highlighted Items Image' );
		$widget->end_controls_tab();

		$widget->end_controls_tabs();

		$widget->end_controls_section();
	}

	private static function add_container_controls( $widget, $config, $type, $heading ) {
		$selector = $config['selectors'][ $type . '_item' ];

		$widget->add_control(
			$type . '_items_heading',
			array(
				'label'     => $heading,
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => $type . '_item_background',
				'selector' => $selector,
			)
		);

		$widget->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $type . '_item_border',
				'selector' => $selector,
			)
		);

		$widget->add_responsive_control(
			$type . '_item_radius',
			array(
				'label'      => 'Border Radius',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array( $selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		$widget->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => $type . '_item_shadow',
				'selector' => $selector,
			)
		);

		$widget->add_responsive_control(
			$type . '_item_margin',
			array(
				'label'      => 'Margin',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array( $selector => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		$widget->add_responsive_control(
			$type . '_item_padding',
			array(
				'label'      => 'Padding',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array( $selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
	}

	private static function add_image_controls( $widget, $config, $type, $heading ) {
		$selector       = $config['selectors'][ $type . '_item' ] . ' ' . $config['selectors']['image'];
		$hover_selector = $config['selectors'][ $type . '_item' ] . ':hover ' . $config['selectors']['image'];

		$widget->add_control(
			$type . '_image_main_heading',
			array(
				'label'     => $heading,
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		// -- NORMAL STATE --
		$widget->add_control(
			$type . '_image_normal_heading',
			array(
				'label' => 'Normal',
				'type'  => Controls_Manager::HEADING,
			)
		);

		$widget->add_responsive_control(
			$type . '_image_opacity',
			array(
				'label'     => 'Opacity',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'max'  => 1,
						'min'  => 0,
						'step' => 0.01,
					),
				),
				'selectors' => array( $selector => 'opacity: {{SIZE}};' ),
			)
		);

		$widget->add_group_control(
			Group_Control_Css_Filter::get_type(),
			array(
				'name'     => $type . '_image_css_filters',
				'selector' => $selector,
			)
		);

		$widget->add_responsive_control(
			$type . '_image_radius',
			array(
				'label'     => 'Border Radius',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( $selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		// -- HOVER STATE --
		$widget->add_control(
			$type . '_image_hover_heading',
			array(
				'label'     => 'Hover',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_responsive_control(
			$type . '_image_opacity_hover',
			array(
				'label'     => 'Opacity',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'max'  => 1,
						'min'  => 0,
						'step' => 0.01,
					),
				),
				'selectors' => array( $hover_selector => 'opacity: {{SIZE}};' ),
			)
		);

		$widget->add_group_control(
			Group_Control_Css_Filter::get_type(),
			array(
				'name'     => $type . '_image_css_filters_hover',
				'selector' => $hover_selector,
			)
		);

		$widget->add_responsive_control(
			$type . '_image_scale_hover',
			array(
				'label'     => 'Scale',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 0.5,
						'max'  => 2,
						'step' => 0.01,
					),
				),
				'selectors' => array( $hover_selector => 'transform: scale({{SIZE}});' ),
			)
		);

		// Fix: Transition duration should be on the normal state for smooth "hover out"
		$widget->add_control(
			$type . '_image_transition',
			array(
				'label'     => 'Transition Duration (s)',
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 0.3 ),
				'range'     => array(
					'px' => array(
						'min'  => 0,
						'max'  => 3,
						'step' => 0.1,
					),
				),
				'selectors' => array( $selector => 'transition: all {{SIZE}}s ease;' ),
			)
		);
	}
}
