<?php
/**
 * Elementor integration.
 *
 * @package Vist360
 */

namespace Vist360\Integrations;

defined( 'ABSPATH' ) || exit;

use Vist360\Domain\TourPostType;

/**
 * Registers the 360° Tour widget with Elementor.
 */
class ElementorWidget {

	/**
	 * Hook registrations.
	 */
	public function register_hooks() {
		add_action( 'elementor/widgets/register', array( $this, 'register_widget' ) );
	}

	/**
	 * Register the widget with Elementor's widgets manager.
	 *
	 * @param object $widgets_manager Elementor widgets manager.
	 */
	public function register_widget( $widgets_manager ) {
		if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
			return;
		}

		$widget = new class() extends \Elementor\Widget_Base {

			/**
			 * Widget slug.
			 *
			 * @return string
			 */
			public function get_name() {
				return 'vist360_tour';
			}

			/**
			 * Widget title.
			 *
			 * @return string
			 */
			public function get_title() {
				return __( '360° Panorama Tour', 'vistour-virtual-tour-builder' );
			}

			/**
			 * Widget icon.
			 *
			 * @return string
			 */
			public function get_icon() {
				return 'eicon-panorama';
			}

			/**
			 * Widget categories.
			 *
			 * @return string[]
			 */
			public function get_categories() {
				return array( 'general' );
			}

			/**
			 * Search keywords.
			 *
			 * @return string[]
			 */
			public function get_keywords() {
				return array( 'panorama', '360', 'virtual tour', 'vr' );
			}

			/**
			 * Register widget controls.
			 */
			protected function register_controls() {
				$this->start_controls_section(
					'section_tour',
					array( 'label' => __( 'Tour', 'vistour-virtual-tour-builder' ) )
				);

				$tours   = get_posts(
					array(
						'post_type'   => TourPostType::POST_TYPE,
						'post_status' => array( 'publish', 'draft', 'private' ),
						'numberposts' => 100,
					)
				);
				$options = array( 0 => __( '— Select a tour —', 'vistour-virtual-tour-builder' ) );

				foreach ( $tours as $tour ) {
					$options[ $tour->ID ] = $tour->post_title ? $tour->post_title : __( '(no title)', 'vistour-virtual-tour-builder' );
				}

				$this->add_control(
					'tour_id',
					array(
						'label'   => __( 'Tour', 'vistour-virtual-tour-builder' ),
						'type'    => \Elementor\Controls_Manager::SELECT,
						'options' => $options,
						'default' => 0,
					)
				);

				$this->add_control(
					'height',
					array(
						'label'   => __( 'Height (px, 0 = tour default)', 'vistour-virtual-tour-builder' ),
						'type'    => \Elementor\Controls_Manager::NUMBER,
						'min'     => 0,
						'max'     => 1200,
						'default' => 0,
					)
				);

				$this->end_controls_section();
			}

			/**
			 * Render widget output.
			 */
			protected function render() {
				$settings = $this->get_settings_for_display();

				echo do_shortcode(
					sprintf(
						'[vist360_tour id="%d" height="%d"]',
						absint( $settings['tour_id'] ?? 0 ),
						absint( $settings['height'] ?? 0 )
					)
				);
			}
		};

		$widgets_manager->register( $widget );
	}
}
