<?php

namespace Gigneo\Modules;

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

use Elementor\Elements_Manager;
use Elementor\Widgets_Manager;
use Gigneo\Elementor\Widgets\Employer_Archive;
use Gigneo\Elementor\Widgets\Employer_Carousel;
use Gigneo\Elementor\Widgets\Employer_Grid;
use Gigneo\Elementor\Widgets\Forgot_Password;
use Gigneo\Elementor\Widgets\Freelancer_Archive;
use Gigneo\Elementor\Widgets\Freelancer_Carousel;
use Gigneo\Elementor\Widgets\Freelancer_Grid;
use Gigneo\Elementor\Widgets\Job_Archive;
use Gigneo\Elementor\Widgets\Job_Carousel;
use Gigneo\Elementor\Widgets\Job_Grid;
use Gigneo\Elementor\Widgets\Login;
use Gigneo\Elementor\Widgets\Register;
use Gigneo\Elementor\Widgets\Search_Bar;
use Gigneo\Elementor\Widgets\Service_Archive;
use Gigneo\Elementor\Widgets\Service_Carousel;
use Gigneo\Elementor\Widgets\Service_Grid;
use Gigneo\Elementor\Widgets\User_Menu;

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


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

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

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

		// Register category
		add_action( 'elementor/elements/categories_registered', array( $this, 'register_category' ), 0 );

		// Editor enqueue
		add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'editor_enqueue' ) );

		// Widget enqueue
		add_action( 'elementor/frontend/after_register_scripts', array( $this, 'widget_enqueue' ) );

		// Register widgets
		add_action( 'elementor/widgets/register', array( $this, 'register_widgets' ) );

		//Register icon tab
		add_filter( 'elementor/icons_manager/additional_tabs', array( $this, 'register_icon_tab' ) );
	}

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

		return self::$instance;
	}

	/**
	 * Check if the current post has Elementor data.
	 *
	 * @since 0.0.1
	 *
	 * @return bool True if the post has Elementor data, false otherwise.
	 */
	public static function has_elementor_data() {
		$post_id = get_the_ID();

		// Check if the page was built with Elementor
		$elementor_data = gigneo_post_meta( $post_id, '_elementor_data' );

		if ( ! empty( $elementor_data ) ) {
			return true;
		}

		// Check if the content contains Elementor shortcodes
		if ( has_shortcode( get_post_field( 'post_content', $post_id ), 'elementor-widget' ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Register a new category for Elementor widgets.
	 *
	 * @since 0.0.1
	 *
	 * @param Elements_Manager $elements_manager Elementor elements manager.
	 */
	public function register_category( $elements_manager ) {
		$elements_manager->add_category(
			'gigneo-widgets',
			array(
				'title' => esc_html__( 'Gigneo', 'gigneo' ),
				'icon'  => 'eicon-settings',
			)
		);
	}

	/**
	 * Enqueues editor scripts and styles.
	 *
	 * @since 0.0.1
	 */
	public function editor_enqueue() {

		//Hero Icons - CSS
		wp_enqueue_style(
			'hero-icons',
			GIGNEO_DIR_URL . 'assets/vendor/css/hero-icons.min.css',
			null,
			'2.1.5'
		);

		//Editor - CSS
		wp_enqueue_style(
			'gigneo-ele-editor',
			GIGNEO_DIR_URL . 'assets/admin/css/ele-editor.css',
			null,
			GIGNEO_VERSION
		);
	}

	/**
	 * Enqueues widget scripts and styles.
	 *
	 * @since 0.0.1
	 */
	public function widget_enqueue() {

		//CSS
		wp_enqueue_style(
			'gigneo-ele-widgets',
			GIGNEO_DIR_URL . 'assets/css/ele-widgets.css',
			null,
			GIGNEO_VERSION
		);

		if ( is_rtl() ) {
			wp_enqueue_style(
				'gigneo-ele-widgets-rtl',
				GIGNEO_DIR_URL . 'assets/css/ele-widgets-rtl.css',
				array( 'gigneo-ele-widgets' ),
				GIGNEO_VERSION
			);
		}

		//JS
		wp_enqueue_script(
			'gigneo-ele-widgets',
			GIGNEO_DIR_URL . 'assets/js/ele-widgets.js',
			array( 'jquery', 'elementor-frontend' ),
			GIGNEO_VERSION,
			true
		);
	}

	/**
	 * Register widgets.
	 *
	 * @since 0.0.1
	 *
	 * @param Widgets_Manager $widgets_manager Elementor widgets manager.
	 */
	public function register_widgets( $widgets_manager ) {

		$platform_type = gigneo_platform_type();

		// Register widgets
		$widgets_manager->register( new Search_Bar() );
		$widgets_manager->register( new User_Menu() );
		$widgets_manager->register( new Freelancer_Grid() );
		$widgets_manager->register( new Freelancer_Carousel() );
		$widgets_manager->register( new Freelancer_Archive() );

		if ( 'jobs' !== $platform_type ) {
			$widgets_manager->register( new Service_Grid() );
			$widgets_manager->register( new Service_Carousel() );
			$widgets_manager->register( new Service_Archive() );
		}

		$widgets_manager->register( new Employer_Grid() );
		$widgets_manager->register( new Employer_Carousel() );
		$widgets_manager->register( new Employer_Archive() );

		if ( 'services' !== $platform_type ) {
			$widgets_manager->register( new Job_Grid() );
			$widgets_manager->register( new Job_Carousel() );
			$widgets_manager->register( new Job_Archive() );
		}

		$widgets_manager->register( new Login() );
		$widgets_manager->register( new Register() );
		$widgets_manager->register( new Forgot_Password() );
	}

	/**
	 * Registers a custom icon tab for Elementor's icon manager.
	 *
	 * Adds a new tab named "Hero Icons - Gigneo" to the Elementor icon manager,
	 * allowing the use of Hero Icons in Elementor widgets.
	 *
	 * @since 0.0.1
	 *
	 * @param array $tabs Existing icon tabs in Elementor.
	 * @return array Modified icon tabs with the new "Hero Icons - Gigneo" tab.
	 */
	public function register_icon_tab( $tabs ) {
		$tabs['hero-icons'] = array(
			'name'          => 'hero-icons',
			'label'         => esc_html__( 'Hero Icons - Gigneo', 'gigneo' ),
			'prefix'        => 'hi-',
			'displayPrefix' => 'hi',
			'labelIcon'     => 'hi hi-gigneo-alt',
			'ver'           => '2.1.5',
			'fetchJson'     => GIGNEO_DIR_URL . 'includes/elementor/icons/list.json?v=' . GIGNEO_VERSION,
			'native'        => false,
		);

		return $tabs;
	}
}
