<?php

namespace ModernFields\FieldGroups\Hooks;

use ModernFields\WordPress\Security\SecurityHelper;
use ModernFields\WordPress\Interfaces\Hooks;

defined('ABSPATH') || exit;

/**
 * Enqueues editor assets and sets up the location metabox for field groups.
 *
 * @since 0.7.3
 */
class FieldGroupAssets implements Hooks
{
  /**
   * @since 0.7.3
   */
  public function registerHooks(): void
  {
    add_action('admin_enqueue_scripts', [$this, 'enqueueLocationMetaboxAssets']);
    add_action('enqueue_block_editor_assets', [$this, 'enqueueEditorAssets']);
    add_action('add_meta_boxes', [$this, 'addLocationMetabox']);
    add_filter('wp_theme_json_data_theme', [$this, 'overrideThemeJson']);
  }

  /**
   * Returns true when the current admin screen is a field group edit page.
   *
   * @since 0.7.3
   */
  protected function isFieldGroupPostType(): bool
  {
    $allowed_pages = ['post.php', 'post-new.php'];
    if (!SecurityHelper::isCurrentAdminPage($allowed_pages)) {
      return false;
    }

    if (isset($_GET['post_type']) && $_GET['post_type'] === MODERN_FIELDS_CPT_SLUG) {
      return true;
    }

    if (!isset($_GET['post_type'])) {
      $post_id   = isset($_GET['post']) ? absint($_GET['post']) : 0;
      $post_type = get_post_type($post_id);
      return $post_type === MODERN_FIELDS_CPT_SLUG;
    }

    return false;
  }

  /**
   * @since 0.7.3
   */
  public function enqueueEditorAssets(): void
  {
    if (!$this->isFieldGroupPostType()) {
      return;
    }

    wp_enqueue_style(
      'modern-fields-editor',
      MODERN_FIELDS_PLUGIN_URL . 'assets/css/field-groups-edit-page.css',
      [],
      MODERN_FIELDS_VERSION
    );

    add_editor_style(MODERN_FIELDS_PLUGIN_URL . 'assets/css/blocks-fields.css');
    add_editor_style(MODERN_FIELDS_PLUGIN_URL . 'assets/css/editor-to-left.css');
  }

  /**
   * @since 0.7.3
   */
  public function enqueueLocationMetaboxAssets(): void
  {
    if (!$this->isFieldGroupPostType()) {
      return;
    }

    $file_path = MODERN_FIELDS_PLUGIN_DIR . 'build/location-metabox/index.asset.php';

    if (!file_exists($file_path)) {
      return;
    }

    $asset_file = include $file_path;

    wp_enqueue_script(
      'modern-fields-location-metabox',
      MODERN_FIELDS_PLUGIN_URL . 'build/location-metabox/index.js',
      $asset_file['dependencies'],
      $asset_file['version'],
      true
    );

    wp_set_script_translations('modern-fields-location-metabox', 'modern-fields');

    wp_enqueue_style(
      'modern-fields-location-metabox',
      MODERN_FIELDS_PLUGIN_URL . 'build/location-metabox/index.css',
      ['wp-components'],
      MODERN_FIELDS_VERSION
    );
  }

  /**
   * @since 0.7.3
   */
  public function addLocationMetabox(): void
  {
    if (!$this->isFieldGroupPostType()) {
      return;
    }

    add_meta_box(
      'mf-location-metabox',
      __('Location', 'modern-fields'),
      [$this, 'renderLocationMetabox'],
      MODERN_FIELDS_CPT_SLUG,
      'normal',
      'high'
    );
  }

  /**
   * @since 0.7.3
   */
  public function renderLocationMetabox(): void
  {
    echo '<div id="mf-location-metabox-root">';
  }

  /**
   * @since 0.7.3
   */
  public function overrideThemeJson(object $theme_json): object
  {
    if (!$this->isFieldGroupPostType()) {
      return $theme_json;
    }

    $theme_json_path = MODERN_FIELDS_PLUGIN_DIR . 'assets/json/theme.json';

    if (!file_exists($theme_json_path)) {
      return $theme_json;
    }

    $plugin_theme   = json_decode(file_get_contents($theme_json_path), true);
    $custom_palette = get_option('modern_fields_color_palette', []);

    if (!empty($custom_palette)) {
      $plugin_theme['settings']['color']['palette']['default'] = $custom_palette;
    }

    return $theme_json->update_with($plugin_theme);
  }
}
