<?php

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Elementor;

defined( 'ABSPATH' ) || exit;
/**
 * Elementor integration loader.
 * Only instantiated when did_action('elementor/loaded') is true.
 *
 * @package ThemeREX\AutoDealer
 */
class ElementorIntegration {

	/**
	 * Hook into Elementor lifecycle.
	 *
	 * @return void
	 */
	public function register(): void {
		add_action( 'elementor/elements/categories_registered', array( $this, 'registerCategories' ) );
		add_action( 'elementor/frontend/after_register_scripts', array( $this, 'registerAssets' ) );
		add_action( 'elementor/frontend/after_register_styles', array( $this, 'registerStyles' ) );
		add_action( 'elementor/widgets/register', array( $this, 'registerWidgets' ) );
		add_action( 'elementor/dynamic_tags/register', array( $this, 'registerDynamicTags' ) );
		add_action( 'elementor/theme/register_conditions', array( $this, 'registerThemeConditions' ) );
	}

	/**
	 * Register the plugin widget category.
	 *
	 * @param \Elementor\Elements_Manager $manager Elements manager.
	 * @return void
	 */
	public function registerCategories( \Elementor\Elements_Manager $manager ): void {
		$manager->add_category(
			'trx-auto-dealer',
			array(
				'title' => __( 'Auto Dealer', 'trx-auto-dealer' ),
				'icon'  => 'eicon-car',
			)
		);
	}

	/**
	 * Register JS dependencies used by widgets.
	 *
	 * @return void
	 */
	public function registerAssets(): void {
		$version = defined( 'TRX_AD_VERSION' ) ? TRX_AD_VERSION : '1.0.0';
		$url     = TRX_AD_URL . 'assets/';

		wp_register_script(
			'trx-ad-swiper',
			$url . 'vendor/swiper/swiper-bundle.min.js',
			array(),
			'11.0',
			true
		);

		wp_register_script(
			'trx-ad-widget-listing-form',
			$url . 'js/widgets/listing-form.js',
			array(),
			$version,
			true
		);

		wp_register_script(
			'trx-ad-widget-media-slider',
			$url . 'js/widgets/media-slider.js',
			array( 'trx-ad-swiper' ),
			$version,
			true
		);
	}

	/**
	 * Register CSS dependencies used by widgets.
	 *
	 * @return void
	 */
	public function registerStyles(): void {
		$version = defined( 'TRX_AD_VERSION' ) ? TRX_AD_VERSION : '1.0.0';
		$url     = TRX_AD_URL . 'assets/';

		wp_register_style(
			'trx-ad-swiper',
			$url . 'vendor/swiper/swiper-bundle.min.css',
			array(),
			'11.0'
		);

		wp_register_style(
			'trx-ad-widget-listing-form',
			$url . 'css/widgets/listing-form.css',
			array(),
			$version
		);

		wp_register_style(
			'trx-ad-widget-specs-highlights',
			$url . 'css/widgets/specs-highlights.css',
			array(),
			$version
		);

		wp_register_style(
			'trx-ad-widget-media-slider',
			$url . 'css/widgets/media-slider.css',
			array( 'trx-ad-swiper' ),
			$version
		);
	}

	/**
	 * Register all plugin widgets with Elementor.
	 *
	 * @param \Elementor\Widgets_Manager $manager Widgets manager.
	 * @return void
	 */
	public function registerWidgets( \Elementor\Widgets_Manager $manager ): void {
		$widgets = array(
			Widgets\SingleListingWidget::class,
			Widgets\MediaSliderWidget::class,
			Widgets\SpecsHighlightsWidget::class,
			Widgets\ListingFormWidget::class,
			Widgets\SearchFormWidget::class,
			Widgets\VehicleGridWidget::class,
			Widgets\VehicleCardWidget::class,
			Widgets\SpecsTableWidget::class,
			Widgets\ContactFormWidget::class,
			Widgets\DealerInfoWidget::class,
			Widgets\DealerDirectoryWidget::class,
			Widgets\ComparisonBarWidget::class,
			Widgets\CompareLinkWidget::class,
			Widgets\MapViewWidget::class,
			Widgets\FavoritesListWidget::class,
			Widgets\FeaturedCarouselWidget::class,
			Widgets\View360Widget::class,
		);

		foreach ( $widgets as $widget_class ) {
			if ( class_exists( $widget_class ) ) {
				$manager->register( new $widget_class() );
			}
		}
	}

	/**
	 * Register dynamic tags + the plugin's tag group.
	 *
	 * Elementor's dynamic-tag module wants every tag registered after the
	 * group it belongs to is added; otherwise tags appear under "Other".
	 *
	 * @param \Elementor\Core\DynamicTags\Manager $manager Dynamic tags manager.
	 * @return void
	 */
	public function registerDynamicTags( \Elementor\Core\DynamicTags\Manager $manager ): void {
		$manager->register_group(
			'trx-auto-dealer',
			array(
				'title' => __( 'Auto Dealer', 'trx-auto-dealer' ),
			)
		);

		$tags = array(
			DynamicTags\VinTag::class,
			DynamicTags\MakeTag::class,
			DynamicTags\ModelTag::class,
			DynamicTags\YearTag::class,
			DynamicTags\MileageTag::class,
			DynamicTags\PriceTag::class,
			DynamicTags\FuelTypeTag::class,
			DynamicTags\StatusTag::class,
			DynamicTags\GalleryTag::class,
			DynamicTags\DealerNameTag::class,
		);

		foreach ( $tags as $tag_class ) {
			if ( class_exists( $tag_class ) ) {
				$manager->register( new $tag_class() );
			}
		}
	}

	/**
	 * Register Theme Builder conditions for vehicle + dealer single pages.
	 * Only fires when Elementor Pro's Theme Builder is active.
	 *
	 * @param \ElementorPro\Modules\ThemeBuilder\Classes\Conditions_Manager $manager Conditions manager.
	 * @return void
	 */
	public function registerThemeConditions( $manager ): void {
		if ( ! class_exists( '\ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base' ) ) {
			return;
		}

		$conditions = array(
			ThemeBuilder\VehicleCondition::class,
			ThemeBuilder\DealerCondition::class,
		);

		foreach ( $conditions as $condition_class ) {
			if ( ! class_exists( $condition_class ) ) {
				continue;
			}
			$instance = new $condition_class();
			if ( method_exists( $manager, 'get_condition' ) ) {
				// Modern Elementor Pro: register against parent "singular" condition.
				$singular = $manager->get_condition( 'singular' );
				if ( $singular && method_exists( $singular, 'register_sub_condition' ) ) {
					$singular->register_sub_condition( $instance );
					continue;
				}
			}
			if ( method_exists( $manager, 'register_condition' ) ) {
				$manager->register_condition( $instance );
			}
		}
	}
}
