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

	public function __construct() {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_topbar_assets' ), 9 );
		add_action( 'wp_body_open', array( $this, 'render_topbar_before_header' ), 5 );
		add_action( 'wp_head', array( $this, 'replace_theme_original_topbar' ), 1 );
		add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_link' ), 200 );
	}

	/**
	 * Enqueue Elementor styles & scripts for topbar template
	 */
	public function enqueue_topbar_assets() {

		$topbar_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'topbar' );

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

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

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

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

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

	/**
	 * Render Topbar before Header
	 */
	public function render_topbar_before_header() {

		$topbar_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'topbar' );

		if ( ! $topbar_id || ! class_exists( '\Elementor\Plugin' ) ) {
			return;
		}
		?>
		<topbar id="sifency-custom-topbar"
				class="sifency-topbar"
				itemscope
				itemtype="https://schema.org/WPHeader">

			<?php
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
				echo \Elementor\Plugin::instance()->frontend
				->get_builder_content_for_display( $topbar_id, true );
			?>

		</topbar>
		<?php
	}

	/**
	 * Hide theme original topbar/header
	 */
	public function replace_theme_original_topbar() {

		$topbar_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'topbar' );
		if ( ! $topbar_id ) {
			return;
		}

		$selectors = array(
			'.theme-topbar',
			'topbar#qodef-page-mobile-topbar',
		);

		$theme_slug = wp_get_theme()->get_template();

		$third_party = array(
			'twentytwentyfive'  => 'topbar.wp-block-template-part',
			'twentytwentyfour'  => 'topbar.wp-block-template-part',
			'twentytwentythree' => 'topbar.wp-block-template-part',
			'astra'             => '#masthead',
			'oceanwp'           => '#site-topbar',
			'generatepress'     => '.site-topbar',
			'kadence'           => '#site-topbar',
			'hello-elementor'   => '#site-topbar',
			'blocksy'           => 'topbar.site-topbar',
			'neve'              => 'topbar.topbar',
		);

		if ( isset( $third_party[ $theme_slug ] ) ) {
			$selectors[] = $third_party[ $theme_slug ];
		}

		echo '<style id="sifency-hide-theme-topbar">';
		echo esc_html( implode( ', ', $selectors ) ) . '{display:none!important;}';
		echo '</style>';
	}

	/**
	 * Admin bar edit link
	 */
	public function add_admin_bar_link( \WP_Admin_Bar $admin_bar ) {

		if ( ! current_user_can( 'edit_posts' ) || ! is_admin_bar_showing() ) {
			return;
		}

		$topbar_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'topbar' );

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