<?php
/**
 * Elementor bridge for published Niwa snippets.
 *
 * @package Niwa_AI
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Niwa_AI_Elementor_Adapter {
	private $repository;

	public function __construct( Niwa_AI_Snippet_Repository $repository ) {
		$this->repository = $repository;
	}

	public function register_hooks() {
		add_action( 'elementor/elements/categories_registered', array( $this, 'register_category' ) );
		add_action( 'elementor/widgets/register', array( $this, 'register_widget' ) );
	}

	public function is_elementor_active() {
		return did_action( 'elementor/loaded' ) || defined( 'ELEMENTOR_VERSION' );
	}

	public function register_category( $elements_manager ) {
		if ( ! method_exists( $elements_manager, 'add_category' ) ) {
			return;
		}
		$elements_manager->add_category(
			'niwa-generated',
			array(
				'title' => __( 'Niwa Generated', 'niwa-ai' ),
				'icon'  => 'fa fa-magic',
			)
		);
	}

	public function register_widget( $widgets_manager ) {
		if ( ! class_exists( '\Elementor\Widget_Base' ) || ! class_exists( '\Elementor\Controls_Manager' ) ) {
			return;
		}

		$this->register_published_template_widgets( $widgets_manager );

		$options = array( '' => __( 'Select a snippet', 'niwa-ai' ) );
		foreach ( $this->repository->get_published_shortcode_snippets() as $snippet ) {
			if ( 'live' === (string) $snippet['status'] ) {
				$options[ (string) $snippet['id'] ] = (string) $snippet['title'];
			}
		}

		$widget = new class( $options ) extends \Elementor\Widget_Base {
				private $snippet_options;

				public function __construct( $snippet_options = array(), $data = array(), $args = null ) {
					$this->snippet_options = is_array( $snippet_options ) ? $snippet_options : array();
					parent::__construct( $data, $args );
				}

				public function get_name() {
					return 'niwa_generated_snippet';
				}

				public function get_title() {
					return __( 'Niwa Snippet', 'niwa-ai' );
				}

				public function get_icon() {
					return 'eicon-code';
				}

				public function get_categories() {
					return array( 'niwa-generated' );
				}

				protected function register_controls() {
					$this->start_controls_section(
						'content_section',
						array( 'label' => __( 'Niwa Snippet', 'niwa-ai' ) )
					);
					$this->add_control(
						'snippet_id',
						array(
							'label'   => __( 'Snippet', 'niwa-ai' ),
							'type'    => \Elementor\Controls_Manager::SELECT,
							'options' => $this->snippet_options,
							'default' => '',
						)
					);
					$this->end_controls_section();
				}

				protected function render() {
					$settings = $this->get_settings_for_display();
					$id       = isset( $settings['snippet_id'] ) ? intval( $settings['snippet_id'] ) : 0;
					if ( $id > 0 ) {
						echo do_shortcode( '[niwa_snippet id="' . intval( $id ) . '"]' );
					}
				}
			};

		if ( method_exists( $widgets_manager, 'register' ) ) {
			$widgets_manager->register( $widget );
		} elseif ( method_exists( $widgets_manager, 'register_widget_type' ) ) {
			$widgets_manager->register_widget_type( $widget );
		}
	}

	private function register_published_template_widgets( $widgets_manager ) {
		foreach ( $this->repository->get_published_shortcode_snippets() as $snippet ) {
			if ( Niwa_AI_Template_Library_Types::TYPE_ELEMENTOR_WIDGET !== (string) $snippet['type'] || 'live' !== (string) $snippet['status'] ) {
				continue;
			}
			$snippet_id = intval( $snippet['id'] );
			if ( $snippet_id <= 0 ) {
				continue;
			}
			$dir         = trailingslashit( WP_CONTENT_DIR ) . 'niwa-snippets/prod/snippet-' . $snippet_id;
			$widget_file = trailingslashit( $dir ) . 'widget.php';
			if ( ! file_exists( $widget_file ) ) {
				continue;
			}

			$style_url  = content_url( 'niwa-snippets/prod/snippet-' . $snippet_id . '/style.css' );
			$script_url = content_url( 'niwa-snippets/prod/snippet-' . $snippet_id . '/script.js' );
			wp_register_style( 'niwa-template-widget-style-' . $snippet_id, $style_url, array(), isset( $snippet['version'] ) ? (string) $snippet['version'] : NIWA_AI_VERSION );
			wp_register_script( 'niwa-template-widget-script-' . $snippet_id, $script_url, array( 'elementor-frontend' ), isset( $snippet['version'] ) ? (string) $snippet['version'] : NIWA_AI_VERSION, true );

			include_once $widget_file;
			$class = 'Niwa_AI_Template_Widget_' . $snippet_id;
			if ( ! class_exists( $class ) ) {
				continue;
			}
			$widget = new $class();
			if ( method_exists( $widgets_manager, 'register' ) ) {
				$widgets_manager->register( $widget );
			} elseif ( method_exists( $widgets_manager, 'register_widget_type' ) ) {
				$widgets_manager->register_widget_type( $widget );
			}
		}
	}
}
