<?php
/**
 * Elementor control schema for `SpecsHighlightsWidget`.
 *
 * Extracted from the widget body in QUALITY-IMPROVEMENT-PLAN T2-2f — 450L
 * of declarative `add_control()` / `add_responsive_control()` definitions
 * dominated the widget file, drowning out the actual `render()` logic.
 *
 * Implemented as a trait so `$this->add_control(...)` and the other
 * `Widget_Base` helpers still resolve against the using widget; the
 * methods become first-class private members of the widget at compile
 * time, identical to leaving them inline.
 *
 * @package ThemeREX\AutoDealer\Elementor\Widgets\Controls
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Elementor\Widgets\Controls;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Typography;
use ThemeREX\AutoDealer\Elementor\Widgets\WidgetHelpers;
use ThemeREX\AutoDealer\Frontend\SpecsHighlightsRenderer;

defined( 'ABSPATH' ) || exit;

// Same Elementor guard as the widget files — the trait references control
// constants on `Controls_Manager` and group-control classes, all of which
// only exist when Elementor is loaded. Returning early prevents a fatal
// during PSR-4 autoload on Elementor-less installs.
if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
	return;
}

trait SpecsHighlightsControls {

	private function registerContentControls(): void {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => __( 'Content', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'vehicle_id',
			array(
				'label'       => __( 'Vehicle', 'trx-auto-dealer' ),
				'type'        => Controls_Manager::SELECT2,
				'options'     => WidgetHelpers::vehicleOptions( __( '— Use current vehicle —', 'trx-auto-dealer' ) ),
				'default'     => '',
				'description' => __( 'Leave empty to use the current vehicle.', 'trx-auto-dealer' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'show_title',
			array(
				'label'        => __( 'Show Title', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'return_value' => 'yes',
			)
		);

		$this->add_control(
			'title',
			array(
				'label'     => __( 'Title', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => __( 'Technical Specifications', 'trx-auto-dealer' ),
				'condition' => array( 'show_title' => 'yes' ),
			)
		);

		$this->add_control(
			'sources',
			array(
				'label'       => __( 'Specs to Show', 'trx-auto-dealer' ),
				'type'        => Controls_Manager::SELECT2,
				'multiple'    => true,
				'options'     => SpecsHighlightsRenderer::getSourceOptions(),
				'default'     => array(),
				'description' => __( 'Leave empty to show all available specs. Drag to reorder.', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'show_icons',
			array(
				'label'        => __( 'Show Icons', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'return_value' => 'yes',
			)
		);

		$this->add_responsive_control(
			'columns',
			array(
				'label'     => __( 'Columns', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '4',
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				),
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);',
				),
			)
		);

		$this->add_control(
			'layout',
			array(
				'label'   => __( 'Layout', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'horizontal',
				'options' => array(
					'horizontal' => __( 'Horizontal (icon left)', 'trx-auto-dealer' ),
					'vertical'   => __( 'Vertical (icon top)', 'trx-auto-dealer' ),
				),
			)
		);

		$this->end_controls_section();
	}

	private function registerStyleTitleControls(): void {
		$this->start_controls_section(
			'section_style_title',
			array(
				'label'     => __( 'Title', 'trx-auto-dealer' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_title' => 'yes' ),
			)
		);

		$this->add_control(
			'title_color',
			array(
				'label'     => __( 'Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__title' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'title_typography',
				'label'    => __( 'Typography', 'trx-auto-dealer' ),
				'selector' => '{{WRAPPER}} .trx-ad-specs-highlights__title',
			)
		);

		$this->add_responsive_control(
			'title_spacing',
			array(
				'label'     => __( 'Bottom Spacing', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 60,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	private function registerStyleItemControls(): void {
		$this->start_controls_section(
			'section_style_item',
			array(
				'label' => __( 'Item Box', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'h_align',
			array(
				'label'        => __( 'Horizontal Alignment', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::CHOOSE,
				'options'      => array(
					'left'   => array(
						'title' => __( 'Left', 'trx-auto-dealer' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => __( 'Center', 'trx-auto-dealer' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => __( 'Right', 'trx-auto-dealer' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'      => '',
				'prefix_class' => 'trx-ad-sh-align-',
			)
		);

		$this->add_control(
			'item_bg',
			array(
				'label'     => __( 'Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__item' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'item_border',
				'label'    => __( 'Border', 'trx-auto-dealer' ),
				'selector' => '{{WRAPPER}} .trx-ad-specs-highlights__item',
			)
		);

		$this->add_responsive_control(
			'item_padding',
			array(
				'label'      => __( 'Padding', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'item_gap',
			array(
				'label'     => __( 'Gap', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 30,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__grid' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'item_border_radius',
			array(
				'label'      => __( 'Border Radius', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px' ),
				'selectors'  => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__grid' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	private function registerStyleIconControls(): void {
		$this->start_controls_section(
			'section_style_icon',
			array(
				'label'     => __( 'Icon', 'trx-auto-dealer' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_icons' => 'yes' ),
			)
		);

		$this->add_control(
			'icon_v_align',
			array(
				'label'       => __( 'Vertical Position', 'trx-auto-dealer' ),
				'description' => __( 'Only applies in Horizontal layout.', 'trx-auto-dealer' ),
				'type'        => Controls_Manager::CHOOSE,
				'options'     => array(
					'flex-start' => array(
						'title' => __( 'Top', 'trx-auto-dealer' ),
						'icon'  => 'eicon-v-align-top',
					),
					'center'     => array(
						'title' => __( 'Middle', 'trx-auto-dealer' ),
						'icon'  => 'eicon-v-align-middle',
					),
					'flex-end'   => array(
						'title' => __( 'Bottom', 'trx-auto-dealer' ),
						'icon'  => 'eicon-v-align-bottom',
					),
				),
				'default'     => 'center',
				'condition'   => array( 'layout' => 'horizontal' ),
				'selectors'   => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__item:not(.trx-ad-specs-highlights__item--vertical)' => 'align-items: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'icon_color',
			array(
				'label'     => __( 'Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__icon' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'icon_bg',
			array(
				'label'     => __( 'Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__icon' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'icon_size',
			array(
				'label'     => __( 'Size', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 16,
						'max' => 80,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .trx-ad-specs-highlights__icon img' => 'max-width: {{SIZE}}{{UNIT}}; max-height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'icon_box_size',
			array(
				'label'     => __( 'Box Size', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 24,
						'max' => 100,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__icon' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'icon_border_radius',
			array(
				'label'      => __( 'Border Radius', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__icon' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'icon_gap',
			array(
				'label'     => __( 'Gap (Icon to Text)', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 40,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__item' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	private function registerStyleLabelControls(): void {
		$this->start_controls_section(
			'section_style_label',
			array(
				'label' => __( 'Label', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'label_color',
			array(
				'label'     => __( 'Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__label' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'label_typography',
				'label'    => __( 'Typography', 'trx-auto-dealer' ),
				'selector' => '{{WRAPPER}} .trx-ad-specs-highlights__label',
			)
		);

		$this->add_responsive_control(
			'label_gap',
			array(
				'label'     => __( 'Gap (Label to Value)', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 20,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__text' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	private function registerStyleValueControls(): void {
		$this->start_controls_section(
			'section_style_value',
			array(
				'label' => __( 'Value', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'value_color',
			array(
				'label'     => __( 'Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-specs-highlights__value' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'value_typography',
				'label'    => __( 'Typography', 'trx-auto-dealer' ),
				'selector' => '{{WRAPPER}} .trx-ad-specs-highlights__value',
			)
		);

		$this->end_controls_section();
	}
}
