<?php
/**
 * The core plugin class.
 *
 * @package Zluck_Ajax_Posts_Filter_Elementor
 */

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

/**
 * Main plugin class.
 */
class AAPF_Plugin {

	/**
	 * The single instance of the class.
	 *
	 * @var AAPF_Plugin
	 */
	private static $instance = null;

	/**
	 * Get the instance of the class.
	 *
	 * @return AAPF_Plugin
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor.
	 */
	private function __construct() {
		$this->load_dependencies();
		$this->init_hooks();
	}

	/**
	 * Load required dependencies.
	 */
	private function load_dependencies() {
		require_once AAPF_PLUGIN_DIR . 'includes/helpers.php';
		require_once AAPF_PLUGIN_DIR . 'includes/class-ajax-handler.php';
		// Widget class is loaded only when Elementor is available
	}

	/**
	 * Initialize hooks.
	 */
	private function init_hooks() {
		add_action( 'plugins_loaded', array( $this, 'check_elementor' ) );
		add_action( 'elementor/widgets/register', array( $this, 'register_widgets' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		// Enqueue scripts in Elementor editor preview
		add_action( 'elementor/frontend/after_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		// Note: load_plugin_textdomain() is not needed for WordPress.org hosted plugins
		// WordPress automatically loads translations for plugins hosted on WordPress.org
	}

	/**
	 * Check if Elementor is loaded.
	 */
	public function check_elementor() {
		if ( ! did_action( 'elementor/loaded' ) ) {
			add_action( 'admin_notices', array( $this, 'elementor_missing_notice' ) );
			return;
		}
	}

	/**
	 * Display notice if Elementor is missing.
	 */
	public function elementor_missing_notice() {
		$message = sprintf(
			/* translators: 1: Plugin name 2: Elementor */
			esc_html__( '%1$s requires %2$s to be installed and activated.', 'zluck-ajax-posts-filter-for-elementor' ),
			'<strong>' . esc_html__( 'Zluck Ajax Posts Filter For Elementor', 'zluck-ajax-posts-filter-for-elementor' ) . '</strong>',
			'<strong>' . esc_html__( 'Elementor', 'zluck-ajax-posts-filter-for-elementor' ) . '</strong>'
		);
		printf( '<div class="notice notice-warning is-dismissible"><p>%s</p></div>', wp_kses_post( $message ) );
	}

	/**
	 * Register Elementor widgets.
	 *
	 * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager.
	 */
	public function register_widgets( $widgets_manager ) {
		// Load widget class only when Elementor is ready
		if ( ! class_exists( 'AAPF_Widget_Posts' ) ) {
			require_once AAPF_PLUGIN_DIR . 'includes/class-widget-posts.php';
		}
		
		$widgets_manager->register( new AAPF_Widget_Posts() );
	}

	/**
	 * Enqueue scripts and styles.
	 */
	public function enqueue_scripts() {
		// Check if Elementor is active
		if ( ! did_action( 'elementor/loaded' ) ) {
			return;
		}

		// Enqueue on frontend and in Elementor editor preview
		// Elementor editor preview is not considered admin for script enqueuing purposes
		$is_elementor_preview = false;
		
		// Check if we're in Elementor preview mode using WordPress functions
		if ( ! wp_doing_ajax() ) {
			$elementor_preview_id = get_query_var( 'elementor-preview', 0 );
			if ( ! empty( $elementor_preview_id ) ) {
				$is_elementor_preview = true;
			} elseif ( defined( 'ELEMENTOR_VERSION' ) && class_exists( '\Elementor\Plugin' ) ) {
				try {
					if ( isset( \Elementor\Plugin::$instance->editor ) && method_exists( \Elementor\Plugin::$instance->editor, 'is_edit_mode' ) ) {
						$is_elementor_preview = \Elementor\Plugin::$instance->editor->is_edit_mode();
					}
				} catch ( Exception $e ) {
					// Editor not available, continue with normal check
				}
			}
		}
		
		if ( is_admin() && ! $is_elementor_preview ) {
			return;
		}

		wp_enqueue_style(
			'aapf-frontend',
			AAPF_PLUGIN_URL . 'assets/css/frontend.css',
			array(),
			AAPF_VERSION
		);

		wp_enqueue_script(
			'aapf-frontend',
			AAPF_PLUGIN_URL . 'assets/js/frontend.js',
			array( 'jquery' ),
			AAPF_VERSION,
			true
		);

		wp_localize_script(
			'aapf-frontend',
			'aapfAjax',
			array(
				'ajaxurl'      => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
				'nonce'        => wp_create_nonce( 'aapf_ajax_nonce' ),
				'errorMessage' => esc_html__( 'An error occurred. Please try again.', 'zluck-ajax-posts-filter-for-elementor' ),
			)
		);
	}

	/**
	 * Run the plugin.
	 */
	public function run() {
		// Plugin is initialized via hooks
	}
}

