<?php
/**
 * UAE / Header Footer Elementor compatibility.
 *
 * Renders HFE header, footer, and before-footer templates on theme hooks
 * instead of the default Eronex markup when those templates are enabled.
 *
 * @package Eronex
 * @since   0.0.1
 */

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

if ( ! class_exists( 'Eronex_UAE_HF_Compatibility' ) ) {

	/**
	 * Header Footer Elementor (UAE) compatibility.
	 *
	 * @since 0.0.1
	 */
	class Eronex_UAE_HF_Compatibility {

		/**
		 * Singleton instance.
		 *
		 * @since 0.0.1
		 * @var Eronex_UAE_HF_Compatibility|null
		 */
		private static $instance;

		/**
		 * Returns the singleton instance.
		 *
		 * @since 0.0.1
		 *
		 * @return Eronex_UAE_HF_Compatibility
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Constructor.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function __construct() {
			add_theme_support( 'header-footer-elementor' );
			add_action( 'wp', array( $this, 'hooks' ) );
		}

		/**
		 * Replaces theme header / footer markup when HFE templates are active.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function hooks() {
			if ( ! class_exists( 'Header_Footer_Elementor' ) ) {
				return;
			}

			if ( function_exists( 'hfe_header_enabled' ) && hfe_header_enabled() && ! $this->elementor_location_active( 'header' ) ) {
				remove_action( 'eronex_header', 'eronex_header_markup' );
				add_action( 'eronex_header', array( $this, 'render_header' ) );
			}

			if ( function_exists( 'hfe_is_before_footer_enabled' ) && hfe_is_before_footer_enabled() ) {
				add_action( 'eronex_footer', array( $this, 'render_before_footer' ), 5 );
			}

			if ( function_exists( 'hfe_footer_enabled' ) && hfe_footer_enabled() && ! $this->elementor_location_active( 'footer' ) ) {
				remove_action( 'eronex_footer', 'eronex_footer_markup' );
				add_action( 'eronex_footer', array( $this, 'render_footer' ) );
			}
		}

		/**
		 * Prints the HFE header template.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function render_header() {
			Header_Footer_Elementor::get_header_content();
		}

		/**
		 * Prints the HFE footer template.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function render_footer() {
			Header_Footer_Elementor::get_footer_content();
		}

		/**
		 * Prints the HFE before-footer template.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function render_before_footer() {
			Header_Footer_Elementor::get_before_footer_content();
		}

		/**
		 * Whether Elementor Pro Theme Builder already owns a location.
		 *
		 * @since 0.0.1
		 *
		 * @param string $location Location slug.
		 * @return bool
		 */
		protected function elementor_location_active( $location ) {
			return class_exists( 'Eronex_Elementor_Compatibility' ) && Eronex_Elementor_Compatibility::location_exists( $location );
		}
	}

	Eronex_UAE_HF_Compatibility::get_instance();

}
