<?php
/**
 * Compare Link widget — compact header link with a live count badge.
 *
 * Drops into a header (or anywhere else) to give shoppers a one-tap path
 * to the comparison page. Delegates to `CompareHandler::renderHeaderLink()`
 * so this widget, the matching Gutenberg block, and the
 * `[trx_ad_compare_link]` shortcode all emit the same markup.
 *
 * @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_Typography;
use Elementor\Widget_Base;
use ThemeREX\AutoDealer\Frontend\CompareHandler;

class CompareLinkWidget extends Widget_Base {

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

	public function get_title(): string {
		return __( 'Compare Link', 'trx-auto-dealer' );
	}

	public function get_icon(): string {
		return 'eicon-bullet-list';
	}

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

	public function get_keywords(): array {
		return array( 'compare', 'header', 'count', 'badge', 'link' );
	}

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

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

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

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

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

		$this->add_control(
			'label',
			array(
				'label'     => __( 'Label', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => __( 'Compare', 'trx-auto-dealer' ),
				'condition' => array( 'show_label' => 'yes' ),
			)
		);

		$this->add_control(
			'hide_if_empty',
			array(
				'label'        => __( 'Hide When Empty', 'trx-auto-dealer' ),
				'description'  => __( 'Hide the link entirely when no vehicles are queued for comparison.', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => '',
				'return_value' => 'yes',
			)
		);

		$this->end_controls_section();

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

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

		$this->add_control(
			'text_color_hover',
			array(
				'label'     => __( 'Text Color (Hover)', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-compare-link:hover' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .trx-ad-compare-link__label',
			)
		);

		$this->add_control(
			'badge_bg',
			array(
				'label'     => __( 'Count Badge Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-compare-link .trx-ad-compare-count' => 'background: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'badge_color',
			array(
				'label'     => __( 'Count Badge Text', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-compare-link .trx-ad-compare-count' => 'color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();
		$html     = CompareHandler::renderHeaderLink(
			array(
				'show_icon'     => 'yes' === ( $settings['show_icon'] ?? 'yes' ),
				'show_label'    => 'yes' === ( $settings['show_label'] ?? 'yes' ),
				'hide_if_empty' => 'yes' === ( $settings['hide_if_empty'] ?? '' ),
				'label'         => (string) ( $settings['label'] ?? __( 'Compare', 'trx-auto-dealer' ) ),
			)
		);
		echo wp_kses( $html, \ThemeREX\AutoDealer\allowedHtml() );
	}
}
