<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Sifency_Addons_Frontend_Popup_Loader' ) ) {

	class Sifency_Addons_Frontend_Popup_Loader {

		private static $_instance = null;
		private static $rendered  = false;

		public static function instance() {
			if ( is_null( self::$_instance ) ) {
				self::$_instance = new self();
			}
			return self::$_instance;
		}

		public function __construct() {
			if ( is_admin() ) {
				return;
			}

			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_popup_assets' ) );
			add_action( 'wp_footer', array( $this, 'render_popup' ), 9999 );
			add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_link' ), 200 );
		}

		public function enqueue_popup_assets() {

			if ( self::$rendered ) {
				return;
			}

			$popup_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'popup' );

			if ( ! $popup_id || ! class_exists( '\Elementor\Plugin' ) ) {
				return;
			}

			/**
			 * Ensure Elementor frontend styles are registered first
			 */
			add_action(
				'elementor/frontend/after_enqueue_styles',
				function () use ( $popup_id ) {

					\Elementor\Plugin::instance()->frontend->enqueue_scripts();

					if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
						$css_file = new \Elementor\Core\Files\CSS\Post( $popup_id );
					} elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
						$css_file = new \Elementor\Post_CSS_File( $popup_id );
					} else {
						$css_file = null;
					}

					if ( $css_file ) {
						$css_file->enqueue();
					}
				}
			);

			/**
			 * Render Elementor popup content
			 */
			ob_start();
			\Elementor\Plugin::instance()->frontend
				->get_builder_content_for_display( $popup_id, true );
			ob_end_clean();

			/**
			 * Popup custom assets
			 */
		}

		public function add_admin_bar_link( \WP_Admin_Bar $admin_bar ) {
			if ( ! current_user_can( 'edit_posts' ) || ! is_admin_bar_showing() ) {
				return;
			}

			$popup_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'popup' );

			if ( $popup_id && class_exists( '\Elementor\Plugin' ) ) {
				$admin_bar->add_node(
					array(
						'id'     => 'sifency-popup-edit',
						'parent' => 'elementor_edit_page',
						'title'  => __( 'Edit Popup', 'sifency-addons' ),
						'href'   => admin_url( 'post.php?post=' . $popup_id . '&action=elementor' ),
					)
				);
			}
		}

		public function render_popup() {
			if ( self::$rendered ) {
				return;
			}

			$popup_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'popup' );
			if ( ! $popup_id ) {
				return;
			}

			self::$rendered = true;

			echo '<!-- Sifency Custom Popup Start -->';
			echo '<div id="sifency-custom-popup" class="sifency-custom-popup">';
				echo '<div class="sifency-popup-overlay" data-sifency-popup="close"></div>';
				echo '<div class="sifency-popup-content-wrapper">';
					echo '<button class="sifency-popup-close" data-sifency-popup="close" aria-label="Close">×</button>';
					echo '<div class="sifency-popup-inner-content">';
						Sifency_Addons_Frontend_Helper::render_elementor_content( $popup_id, true );
					echo '</div>';
				echo '</div>';
			echo '</div>';
			echo '<!-- Sifency Custom Popup End -->';
		}
	}

	Sifency_Addons_Frontend_Popup_Loader::instance();
}
