<?php
namespace Sifency\Traits;
if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('\Elementor\Plugin')) {
    return;
}
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;
use \Elementor\Group_Control_Text_Shadow;
/**
 * A reusable Elementor control class for adding granular Odd/Even item styling options.
 *
 * @package Sifency
 * @version 1.0.4
 */
final class Sifency_Elementor_Parity_Controls {

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

		$widget->start_controls_section(
			$config['section_id'],
			[ 'label' => esc_html__( 'Odd/Even Styles', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ]
		);
		$widget->add_control(
			'parity_pattern',
			[ 'label' => 'Pattern', 'type' => Controls_Manager::SELECT, 'options' => [ 'standard' => 'Standard (Odd/Even)', 'every_third' => 'Every 3rd', 'custom' => 'Custom' ], 'default' => 'standard' ]
		);
		$widget->add_control(
			'custom_even_positions',
			[ 'label' => 'Highlighted Positions', 'description' => 'Comma-separated (e.g., 2,5,8).', 'type' => Controls_Manager::TEXT, 'default' => '2,5,8', 'condition' => [ 'parity_pattern' => 'custom' ] ]
		);
		
		$widget->start_controls_tabs( 'parity_element_style_tabs' );
		$widget->start_controls_tab( 'parity_tab_container', [ 'label' => 'Container' ] );
		self::add_container_controls( $widget, $config, 'odd', 'Standard Items' );
		self::add_container_controls( $widget, $config, 'even', 'Highlighted Items' );
		$widget->end_controls_tab();
		$widget->start_controls_tab( 'parity_tab_image', [ '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', [ 'label' => $heading, 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		$widget->add_group_control( Group_Control_Background::get_type(), [ 'name' => $type . '_item_background', 'selector' => $selector ] );
		$widget->add_group_control( Group_Control_Border::get_type(), [ 'name' => $type . '_item_border', 'selector' => $selector ] );
	}
    
	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', [ 'label' => $heading, 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		
        // -- NORMAL STATE CONTROLS --
        $widget->add_control( $type.'_image_normal_heading', [ 'label' => 'Normal', 'type' => Controls_Manager::HEADING ] );
		$widget->add_responsive_control( $type . '_image_opacity', [ 'label' => 'Opacity', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0, 'step' => 0.01 ] ], 'selectors' => [ $selector => 'opacity: {{SIZE}};' ] ]);
		$widget->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => $type . '_image_css_filters', 'selector' => $selector ]);
		
        // -- HOVER STATE CONTROLS --
        $widget->add_control( $type.'_image_hover_heading', [ 'label' => 'Hover', 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		$widget->add_responsive_control( $type . '_image_opacity_hover', [ 'label' => 'Opacity', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0, 'step' => 0.01 ] ], 'selectors' => [ $hover_selector => 'opacity: {{SIZE}};' ] ]);
		$widget->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => $type . '_image_css_filters_hover', 'selector' => $hover_selector ]);
		$widget->add_responsive_control( $type . '_image_scale_hover', [ 'label' => 'Scale', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0.5, 'max' => 2, 'step' => 0.01 ] ], 'selectors' => [ $hover_selector => 'transform: scale({{SIZE}});' ] ]);
        $widget->add_control( $type . '_image_transition', [ 'label' => 'Transition (s)', 'type' => Controls_Manager::SLIDER, 'selectors' => [ $selector => 'transition-duration: {{SIZE}}s;' ] ]);
	}
}