<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://orphic.ca
 * @since      1.0.0
 *
 * @package    Orphic_UI
 * @subpackage Orphic_UI/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Orphic_UI
 * @subpackage Orphic_UI/admin
 * @author     orphic <contact@orphic.ca>
 */
class Orphic_UI_Admin
{

  /**
   * The ID of this plugin.
   *
   * @since    1.0.0
   * @access   private
   * @var      string    $plugin_name    The ID of this plugin.
   */
  private $plugin_name;

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

  /**
   * Initialize the class and set its properties.
   *
   * @since    1.0.0
   * @param      string    $plugin_name       The name of this plugin.
   * @param      string    $version    The version of this plugin.
   */
  public function __construct($plugin_name, $version)
  {

    $this->plugin_name = $plugin_name;
    $this->version = $version;
  }

  /**
   * Register the stylesheets for the admin area.
   *
   * @since    1.0.0
   */
  public function enqueue_styles()
  {

    /**
     * This function is provided for demonstration purposes only.
     *
     * An instance of this class should be passed to the run() function
     * defined in Orphic_UI_Loader as all of the hooks are defined
     * in that particular class.
     *
     * The Orphic_UI_Loader will then create the relationship
     * between the defined hooks and the functions defined in this
     * class.
     */

    wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/orphic-ui-admin.css', array(), $this->version, 'all');
    $global_color_palette_name = get_option(ConstBlockOrphicOptionName::GLOBAL_COLOR_PALETTE);
    switch ($global_color_palette_name) {
      case ConstBlockOrphicOptionValue::GLOBAL_COLOR_PALETTE_CLASSIC:
        wp_enqueue_style('orphic-wp-block-plugin-classic', ORPHIC_PLUGIN_UI_DIR_URL . 'public/css/orphic-wp-block-plugin-classic.css', array(), $this->version, 'all');
        break;
      case ConstBlockOrphicOptionValue::GLOBAL_COLOR_PALETTE_CUSTOM:
        wp_enqueue_style('orphic-wp-block-plugin-custom', ORPHIC_PLUGIN_UI_DIR_URL . 'public/css/orphic-wp-block-plugin-custom.css', array(), $this->version, 'all');
        break;
    }
  }

  public function orphic_wp_block_plugin_enqueue_assets()
  {
    wp_enqueue_style($this->plugin_name, ORPHIC_PLUGIN_UI_DIR_URL . 'public/css/orphic-ui-public.css', array(), $this->version, 'all');
    $global_color_palette_name = get_option(ConstBlockOrphicOptionName::GLOBAL_COLOR_PALETTE);
    switch ($global_color_palette_name) {
      case ConstBlockOrphicOptionValue::GLOBAL_COLOR_PALETTE_CLASSIC:
        wp_enqueue_style('orphic-wp-block-plugin-classic', ORPHIC_PLUGIN_UI_DIR_URL . 'public/css/orphic-wp-block-plugin-classic.css', array(), $this->version, 'all');
        break;
      case ConstBlockOrphicOptionValue::GLOBAL_COLOR_PALETTE_CUSTOM:
        wp_enqueue_style('orphic-wp-block-plugin-custom', ORPHIC_PLUGIN_UI_DIR_URL . 'public/css/orphic-wp-block-plugin-custom.css', array(), $this->version, 'all');
        break;
    }
  }

  /**
   * Register the JavaScript for the admin area.
   *
   * @since    1.0.0
   */
  public function enqueue_scripts()
  {

    /**
     * This function is provided for demonstration purposes only.
     *
     * An instance of this class should be passed to the run() function
     * defined in Orphic_UI_Loader as all of the hooks are defined
     * in that particular class.
     *
     * The Orphic_UI_Loader will then create the relationship
     * between the defined hooks and the functions defined in this
     * class.
     */

    wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/orphic-ui-admin.js', array('jquery'), $this->version, false);
  }



  /**
   * Charge le script de l'éditeur de barre de recherche uniquement sur les bonnes pages
   */
  public function load_search_builder_script($hook)
  {
    global $post, $typenow;

    // Condition pour vérifier si nous sommes sur la page d'édition ou de création du CPT search_bar_orphic_ui
    $is_edit_page = ($hook === 'post.php' && isset($_GET['action']) && $_GET['action'] === 'edit' && isset($post) && $post->post_type === 'search_bar_orphic_ui');
    $is_new_page = ($hook === 'post-new.php' && isset($_GET['post_type']) && $_GET['post_type'] === 'search_bar_orphic_ui');

    if ($is_edit_page || $is_new_page) {
      wp_enqueue_style('orphic-ui-search-builder', ORPHIC_PLUGIN_UI_DIR_URL . 'features/react-search-bar/init/preStyle.css', array(), $this->version, 'all');

      // Enregistrez React et ReactDOM
      wp_enqueue_script('react', 'https://unpkg.com/react@17/umd/react.development.js', array(), '17.0.2', true);
      wp_enqueue_script('react-dom', 'https://unpkg.com/react-dom@17/umd/react-dom.development.js', array('react'), '17.0.2', true);

      // Enregistrez votre application React
      wp_enqueue_script(
        'orphic-ui-search-builder',
        ORPHIC_PLUGIN_UI_DIR_URL . 'features/react-search-bar/build/search-builder.js',
        array('wp-element'), // WordPress inclut React sous wp-element
        $this->version,
        true
      );

      // Déterminer si c'est un nouveau post ou un post existant
      $is_new_post = (isset($post) && $post->post_status === 'auto-draft' && empty($post->post_title));

      wp_localize_script(
        'orphic-ui-search-builder',
        'page_data',
        array(
          'restUrl' => rest_url('orphic-ui/v1/'),
          'homeUrl' => home_url(),
          'nonce' => wp_create_nonce('wp_rest'),
          'postId' => isset($post) ? $post->ID : 0,
          'isNewPost' => $is_new_post,
          'postStatus' => isset($post) ? $post->post_status : '',
          'postType' => 'search_bar_orphic_ui',
          'title' => isset($post) ? $post->post_title : ''
        )
      );
    }
  }
}
