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

if ( ! class_exists( 'Sifency_Frontend_Offcanvas' ) ) {

	class Sifency_Frontend_Offcanvas {

		public function __construct() {
			if ( is_admin() ) {
				return;
			}
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_offcanvas_assets' ) );
			add_action( 'wp_footer', array( $this, 'render_offcanvas' ), 9999 );
			add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_link' ), 200 );
		}

		public function enqueue_offcanvas_assets() {

			$offcanvas_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'offcanvas' );

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

			add_action(
				'elementor/frontend/after_enqueue_styles',
				function () use ( $offcanvas_id ) {

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

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

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

			/**
			 * Render Elementor content (CSS already enqueued above)
			 */
			ob_start();
			\Elementor\Plugin::instance()->frontend
				->get_builder_content_for_display( $offcanvas_id, true );
			ob_end_clean();

			/**
			 * Offcanvas custom assets
			 */
			wp_enqueue_style(
				'sifency-offcanvas-css',
				SIFENCY_TB_URL . 'assets/css/offcanvas-tb.css',
				array(),
				SIFENCY_TB_VERSION,
				'all'
			);

			wp_enqueue_script(
				'sifency-offcanvas-js',
				SIFENCY_TB_URL . 'assets/js/offcanvas-tb.js',
				array( 'jquery' ),
				SIFENCY_TB_VERSION,
				true
			);
		}

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

			$offcanvas_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'offcanvas' );

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

		public function render_offcanvas() {
			$offcanvas_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'offcanvas' );
			if ( ! $offcanvas_id ) {
				return;
			}

			echo '<!-- Sifency Offcanvas Start -->';
			echo '<div id="sifency-offcanvas-wrapper" class="sifency-offcanvas-wrapper">';
				echo '<div class="sifency-offcanvas-overlay" data-sifency-offcanvas="close"></div>';

				echo '<div class="sifency-offcanvas-panel">';
					echo '<button class="sifency-offcanvas-close-btn-tb" data-sifency-offcanvas="close" aria-label="Close">×</button>';
					echo '<div class="sifency-offcanvas-content">';
						echo wp_kses_post( \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $offcanvas_id ) );
					echo '</div>';
				echo '</div>';
			echo '</div>';
			echo '<!-- Sifency Offcanvas End -->';
		}
	}
	new Sifency_Frontend_Offcanvas();
}
