<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://orphic.ca
 * @since      1.0.0
 *
 * @package    Orphic_UI
 * @subpackage Orphic_UI/includes
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    Orphic_UI
 * @subpackage Orphic_UI/includes
 * @author     orphic <contact@orphic.ca>
 */
class Orphic_UI
{

  /**
   * The loader that's responsible for maintaining and registering all hooks that power
   * the plugin.
   *
   * @since    1.0.0
   * @access   protected
   * @var      Orphic_UI_Loader    $loader    Maintains and registers all hooks for the plugin.
   */
  protected $loader;

  /**
   * The unique identifier of this plugin.
   *
   * @since    1.0.0
   * @access   protected
   * @var      string    $plugin_name    The string used to uniquely identify this plugin.
   */
  protected $plugin_name;

  /**
   * The current version of the plugin.
   *
   * @since    1.0.0
   * @access   protected
   * @var      string    $version    The current version of the plugin.
   */
  protected $version;

  /**
   * Define the core functionality of the plugin.
   *
   * Set the plugin name and the plugin version that can be used throughout the plugin.
   * Load the dependencies, define the locale, and set the hooks for the admin area and
   * the public-facing side of the site.
   *
   * @since    1.0.0
   */
  public function __construct()
  {
    if (defined('ORPHIC_BLOCK_UI_VERSION')) {
      $this->version = ORPHIC_BLOCK_UI_VERSION;
    } else {
      $this->version = '1.0.0';
    }
    $this->plugin_name = 'orphic-ui';

    $this->load_dependencies();
    $this->set_locale();
    $this->define_admin_hooks();
    $this->define_public_hooks();
  }

  /**
   * Load the required dependencies for this plugin.
   *
   * Include the following files that make up the plugin:
   *
   * - Orphic_UI_Loader. Orchestrates the hooks of the plugin.
   * - Orphic_UI_i18n. Defines internationalization functionality.
   * - Orphic_UI_Admin. Defines all hooks for the admin area.
   * - Orphic_UI_Public. Defines all hooks for the public side of the site.
   *
   * Create an instance of the loader which will be used to register the hooks
   * with WordPress.
   *
   * @since    1.0.0
   * @access   private
   */
  private function load_dependencies()
  {

    /**
     * The class responsible for orchestrating the actions and filters of the
     * core plugin.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-orphic-ui-loader.php';

    /**
     * The class responsible for defining internationalization functionality
     * of the plugin.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-orphic-ui-i18n.php';

    /**
     * The class responsible for defining all actions that occur in the admin area.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-orphic-ui-admin.php';

    /**
     * The class responsible for defining all actions that occur in the public-facing
     * side of the site.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-orphic-ui-public.php';

    /**
     * The class responsible for defining the option page for the plugin.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'public/orphic-wp-block-option-page/class-wp-block-option-page.php';

    /**
     * The class responsible for defining the global options for the plugin.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'public/const/class-global-plugin-option.php';

    /**
     * The class responsible for creating the css file.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'public/orphic-wp-block-option-page/class-wp-block-option-script.php';

    /**
     * The class responsible for handling module imports.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'features/modules/import/class-module-import.php';

    /**
     * The class responsible for handling the updater.
     */
    require_once plugin_dir_path(dirname(__FILE__)) . 'features/update/class-updater.php';

    require_once plugin_dir_path(dirname(__FILE__)) . 'public/const/shared/enum-opm.php';

    require_once plugin_dir_path(dirname(__FILE__)) . 'features/react-search-bar/api/class-search-bar-orphic-ui.php';

    require_once plugin_dir_path(dirname(__FILE__)) . 'features/react-search-bar/api/class-search-bar-controller-orphic-ui.php';

    $this->loader = new Orphic_UI_Loader();
  }

  /**
   * Define the locale for this plugin for internationalization.
   *
   * Uses the Orphic_UI_i18n class in order to set the domain and to register the hook
   * with WordPress.
   *
   * @since    1.0.0
   * @access   private
   */
  private function set_locale()
  {

    $plugin_i18n = new Orphic_UI_i18n();

    $this->loader->add_action('plugins_loaded', $plugin_i18n, 'load_plugin_textdomain');
  }

  /**
   * Register all of the hooks related to the admin area functionality
   * of the plugin.
   *
   * @since    1.0.0
   * @access   private
   */
  private function define_admin_hooks()
  {
    $updater = new Updater();


    $plugin_admin = new Orphic_UI_Admin($this->get_plugin_name(), $this->get_version());

    $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
    $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
    $this->loader->add_action('enqueue_block_assets', $plugin_admin, 'orphic_wp_block_plugin_enqueue_assets');

    // Ajouter l'action pour charger le JS de l'éditeur de barre de recherche uniquement sur les bonnes pages
    $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'load_search_builder_script');

    $this->loader->add_action('rest_api_init', Search_Bar_Orphic_UI::class, 'register_routes');
    $this->loader->add_action('rest_api_init', Search_Bar_Controller_Orphic_UI::class, 'register_routes');
  }

  /**
   * Register all of the hooks related to the public-facing functionality
   * of the plugin.
   *
   * @since    1.0.0
   * @access   private
   */
  private function define_public_hooks()
  {

    $plugin_public = new Orphic_UI_Public($this->get_plugin_name(), $this->get_version());

    $this->loader->add_action('admin_enqueue_scripts', $plugin_public, 'register_admin_script_and_style');
    $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');
    $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');
    $this->loader->add_action('init', $plugin_public, 'set_global_options_settings_wp_block_orphic');
    $this->loader->add_action('init', $plugin_public, 'register_orphic_block');
    $this->loader->add_action('init', $plugin_public, 'register_orphic_module');
    $this->loader->add_action('admin_menu', $plugin_public, 'create_plugin_block_function_option_page');
    $this->loader->add_action('enqueue_block_assets', $plugin_public, 'orphic_wp_block_plugin_enqueue_assets');
    $this->loader->add_action('enqueue_block_editor_assets', $plugin_public, 'enqueue_editor_assets');

    // Register post types and taxonomies
    $this->loader->add_action('init', $plugin_public, 'create_faq_post_type');
    $this->loader->add_action('init', $plugin_public, 'create_faq_taxonomy');
    $this->loader->add_action('init', $plugin_public, 'create_search_bar_post_type');

    $this->loader->add_action('wp_ajax_orphic_wp_block_save_colors', $plugin_public, 'save_colors');
    $this->loader->add_action('wp_ajax_orphic_wp_block_save_active_blocks', $plugin_public, 'save_active_blocks');
    $this->loader->add_action('wp_ajax_orphic_wp_block_save_active_modules', $plugin_public, 'save_active_modules');
    $this->loader->add_action('wp_ajax_orphic_wp_block_save_google_api_key', $plugin_public, 'save_google_api_key');
    $this->loader->add_action('wp_ajax_orphic_wp_block_save_breakpoint_settings', $plugin_public, 'save_breakpoint_settings');

    // $this->loader->add_action('enqueue_block_editor_assets', $this, 'enqueue_editor_assets');
  }

  /**
   * Run the loader to execute all of the hooks with WordPress.
   *
   * @since    1.0.0
   */
  public function run()
  {
    $this->loader->run();
  }

  /**
   * The name of the plugin used to uniquely identify it within the context of
   * WordPress and to define internationalization functionality.
   *
   * @since     1.0.0
   * @return    string    The name of the plugin.
   */
  public function get_plugin_name()
  {
    return $this->plugin_name;
  }

  /**
   * The reference to the class that orchestrates the hooks with the plugin.
   *
   * @since     1.0.0
   * @return    Orphic_UI_Loader    Orchestrates the hooks of the plugin.
   */
  public function get_loader()
  {
    return $this->loader;
  }

  /**
   * Retrieve the version number of the plugin.
   *
   * @since     1.0.0
   * @return    string    The version number of the plugin.
   */
  public function get_version()
  {
    return $this->version;
  }



  // public function enqueue_editor_assets() {
  //   // Enqueue animation filters for native blocks
  //   wp_enqueue_script(
  //     'orphic-animation-filters',
  //     plugin_dir_url(__DIR__) . 'public/js/animation-filters.js',
  //     array(
  //       'wp-blocks',
  //       'wp-element',
  //       'wp-editor',
  //       'wp-components',
  //       'wp-i18n',
  //       'wp-compose',
  //       'lodash'
  //     ),
  //     filemtime(plugin_dir_path(__DIR__) . 'public/js/animation-filters.js'),
  //     true
  //   );
  // }
}
