<?php
/**
 * Search Form widget — vehicle inventory filter form.
 *
 * Delegates render to the SearchFormBlock so widget output stays in lock-step
 * with the Gutenberg block and the archive-sidebar form. Per-filter visibility
 * toggles map to the same `trx-ad-search-form--hide-{filter}` modifier classes
 * the block uses, so a single CSS rule set applies to both surfaces.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Elementor\Widgets;

if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
	return;
}

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;
use ThemeREX\AutoDealer\Blocks\SearchFormBlock;

class SearchFormWidget extends Widget_Base {

	public function get_name(): string {
		return 'trx_ad_search_form';
	}

	public function get_title(): string {
		return __( 'Vehicle Search Form', 'trx-auto-dealer' );
	}

	public function get_icon(): string {
		return 'eicon-search-bold';
	}

	public function get_categories(): array {
		return array( 'trx-auto-dealer' );
	}

	public function get_keywords(): array {
		return array( 'search', 'filter', 'vehicle', 'auto', 'dealer' );
	}

	public function get_style_depends(): array {
		return array( 'trx-ad-frontend' );
	}

	public function get_script_depends(): array {
		return array( 'trx-ad-frontend', 'trx-ad-archive-filters' );
	}

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

		$this->add_control(
			'heading',
			array(
				'label'   => __( 'Heading', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::TEXT,
				'default' => __( 'Find Your Vehicle', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'submit_label',
			array(
				'label'   => __( 'Submit Label', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::TEXT,
				'default' => __( 'Search', 'trx-auto-dealer' ),
			)
		);

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

		$this->add_control(
			'action',
			array(
				'label'         => __( 'Action URL', 'trx-auto-dealer' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => __( 'Defaults to vehicle archive', 'trx-auto-dealer' ),
				'show_external' => false,
				'default'       => array( 'url' => '' ),
			)
		);

		$this->end_controls_section();

		// Filter visibility toggles.
		$this->start_controls_section(
			'section_filters',
			array(
				'label' => __( 'Filters', 'trx-auto-dealer' ),
			)
		);

		$filter_map = array(
			'show_keyword'       => __( 'Keyword', 'trx-auto-dealer' ),
			'show_make'          => __( 'Make', 'trx-auto-dealer' ),
			'show_model'         => __( 'Model', 'trx-auto-dealer' ),
			'show_body_style'    => __( 'Body Style', 'trx-auto-dealer' ),
			'show_fuel'          => __( 'Fuel Type', 'trx-auto-dealer' ),
			'show_condition'     => __( 'Condition', 'trx-auto-dealer' ),
			'show_year'          => __( 'Year Range', 'trx-auto-dealer' ),
			'show_price'         => __( 'Price Range', 'trx-auto-dealer' ),
			'show_mileage'       => __( 'Mileage Range', 'trx-auto-dealer' ),
			'show_featured'      => __( 'Featured Only', 'trx-auto-dealer' ),
			'show_accident_free' => __( 'Accident-Free Only', 'trx-auto-dealer' ),
		);
		foreach ( $filter_map as $key => $label ) {
			$this->add_control(
				$key,
				array(
					'label'        => $label,
					'type'         => Controls_Manager::SWITCHER,
					'default'      => 'yes',
					'return_value' => 'yes',
				)
			);
		}

		$this->end_controls_section();

		// Style: Heading.
		$this->start_controls_section(
			'section_style_heading',
			array(
				'label' => __( 'Heading', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

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

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => '{{WRAPPER}} .trx-ad-search-form__heading',
			)
		);

		$this->end_controls_section();

		// Style: Form box.
		$this->start_controls_section(
			'section_style_box',
			array(
				'label' => __( 'Form Box', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

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

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'box_border',
				'selector' => '{{WRAPPER}} .trx-ad-filter-form',
			)
		);

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

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

		$this->end_controls_section();

		// Style: Submit Button.
		$this->start_controls_section(
			'section_style_submit',
			array(
				'label' => __( 'Submit Button', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

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

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

		$this->add_control(
			'submit_hover_bg',
			array(
				'label'     => __( 'Hover Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-filter-form__submit:hover' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'submit_typography',
				'selector' => '{{WRAPPER}} .trx-ad-filter-form__submit',
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();

		$action_url = '';
		if ( ! empty( $settings['action']['url'] ) ) {
			$action_url = (string) $settings['action']['url'];
		}

		$attributes = array(
			'heading'          => (string) ( $settings['heading'] ?? '' ),
			'submitLabel'      => (string) ( $settings['submit_label'] ?? '' ),
			'compact'          => 'yes' === ( $settings['compact'] ?? '' ),
			'action'           => $action_url,
			'showKeyword'      => 'yes' === ( $settings['show_keyword'] ?? 'yes' ),
			'showMake'         => 'yes' === ( $settings['show_make'] ?? 'yes' ),
			'showModel'        => 'yes' === ( $settings['show_model'] ?? 'yes' ),
			'showBodyStyle'    => 'yes' === ( $settings['show_body_style'] ?? 'yes' ),
			'showFuel'         => 'yes' === ( $settings['show_fuel'] ?? 'yes' ),
			'showCondition'    => 'yes' === ( $settings['show_condition'] ?? 'yes' ),
			'showYear'         => 'yes' === ( $settings['show_year'] ?? 'yes' ),
			'showPrice'        => 'yes' === ( $settings['show_price'] ?? 'yes' ),
			'showMileage'      => 'yes' === ( $settings['show_mileage'] ?? 'yes' ),
			'showFeatured'     => 'yes' === ( $settings['show_featured'] ?? 'yes' ),
			'showAccidentFree' => 'yes' === ( $settings['show_accident_free'] ?? 'yes' ),
		);

		echo wp_kses( ( new SearchFormBlock() )->render( $attributes ), \ThemeREX\AutoDealer\allowedHtml() );
	}
}
