<?php

namespace Gigneo;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Compatibility
 *
 * Plugin Compatibility class for Gigneo.
 *
 * @since 0.0.1
 */
class Compatibility {

	/**
	 * The single instance of the class.
	 *
	 * @var Compatibility|null
	 * @since 0.0.1
	 */
	private static $instance = null;

	/**
	 * Constructor for the class.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {

		// Check if the current theme is compatible
		add_action( 'wp', array( $this, 'initialized' ) );
	}

	/**
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @return Compatibility
	 * @since 0.0.1
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Get the compatible themes.
	 *
	 * @return array
	 * @since 0.0.1
	 */
	public static function compatible_themes() {
		$themes = array( 'hello-elementor', 'astra', 'eronex' );

		$parent_theme_slug  = get_template();
		$current_theme_slug = get_stylesheet();

		// Add the active child theme when its parent is compatible.
		if ( $parent_theme_slug !== $current_theme_slug && in_array( $parent_theme_slug, $themes, true ) ) {
			$themes[] = $current_theme_slug;
		}

		return apply_filters( 'gigneo_compatible_themes', $themes );
	}

	/**
	 * Initialize the compatibility checks.
	 *
	 * @since 0.0.1
	 */
	public function initialized() {
		// Get the active theme name
		$active_theme      = wp_get_theme();
		$active_theme_name = strtolower( $active_theme->get( 'Name' ) );

		$parent_theme      = wp_get_theme( get_template() );
		$parent_theme_name = strtolower( $parent_theme->get( 'Name' ) );

		$hire_template = gigneo_option( '_gigneo_ts_template_hire' );

		// Compatible themes
		if ( 'hello elementor' === $active_theme_name || 'hello elementor' === $parent_theme_name ) {
			//Add user menu to header menu
			add_filter( 'wp_nav_menu', array( $this, 'user_menu_markup_on_hello_theme' ), 10, 2 );

			//Remove Page title
			if ( is_singular(
				array(
					'gigneo_freelancer',
					'gigneo_service',
					'gigneo_employer',
					'gigneo_job',
				)
			) || is_page( $hire_template ) ) {
				add_filter( 'hello_elementor_page_title', '__return_false' );
			}
		} elseif ( 'astra' === $active_theme_name || 'astra' === $parent_theme_name ) {
			//Add user menu to header menu
			add_action( 'astra_render_header_column', array( $this, 'user_menu_markup_on_astra_theme' ), 5, 2 );
		} elseif ( 'eronex' === $active_theme_name || 'eronex' === $parent_theme_name ) {
			//Add user menu to header menu
			add_action( 'eronex_nav_menu_after', array( $this, 'user_menu_markup_on_eronex_theme' ) );
			//Remove Extra hooks
			if ( is_singular(
				array(
					'gigneo_freelancer',
					'gigneo_service',
					'gigneo_employer',
					'gigneo_job',
				)
			) || is_page( $hire_template ) ) {
				remove_action( 'eronex_post_categories', 'eronex_post_categories_markup' );
				remove_action( 'eronex_post_title', 'eronex_post_title_markup' );
				remove_action( 'eronex_post_meta', 'eronex_post_meta_markup' );
				remove_action( 'eronex_post_thumbnail', 'eronex_post_thumbnail_markup' );
				remove_action( 'eronex_post_comments', 'eronex_post_comments_markup' );
			}
		}
	}

	/**
	 * Add user menu markup to the Hello theme navigation menu.
	 *
	 * Appends the user authentication menu to the primary navigation
	 * when the feature is enabled in the plugin settings.
	 *
	 * @param string $nav_menu The navigation menu HTML.
	 * @param object $args     The menu arguments object.
	 *
	 * @return string The modified navigation menu HTML.
	 * @since 0.0.1
	 */
	public function user_menu_markup_on_hello_theme( $nav_menu, $args ) {
		$user_menu = gigneo_option( '_gigneo_ts_auth_user_menu' );
		if ( $user_menu && 'menu-1' === $args->theme_location ) {
			$user_menu_enable = gigneo_option( '_gigneo_ts_auth_user_menu' );
			if ( $user_menu_enable ) {
				ob_start();
				gigneo_load_template( 'auth/menu', false );
				$user_menu = ob_get_clean();

				return $nav_menu . $user_menu;
			}
		}
		return $nav_menu;
	}

	/**
	 * Output user menu markup for the Astra theme.
	 *
	 * Displays the user authentication menu in the right header
	 * section when the feature is enabled in the plugin settings.
	 *
	 * @param string $row      The current header row.
	 * @param string $position The current header position.
	 *
	 * @return void
	 * @since 0.0.1
	 */
	public function user_menu_markup_on_astra_theme( $row, $position ) {
		$user_menu = gigneo_option( '_gigneo_ts_auth_user_menu' );
		if ( $user_menu && 'right' === $position ) {
			gigneo_load_template( 'auth/menu', false );
		}
	}

	/**
	 * Output user menu markup for the Evora theme.
	 *
	 * Displays the user authentication menu when the feature
	 * is enabled in the plugin settings.
	 *
	 * @return void
	 * @since 0.0.1
	 */
	public function user_menu_markup_on_eronex_theme() {
		$user_menu = gigneo_option( '_gigneo_ts_auth_user_menu' );
		if ( $user_menu ) {
			gigneo_load_template( 'auth/menu', false );
		}
	}
}
