<?php

/**
 * Utility class for the Lockee plugin
 */
class Lockee_Utils
{
  private const SUPPORTED_LANGUAGES = ['fr', 'en', 'es'];
  private const DEFAULT_LANGUAGE = 'en';

  /**
   * Generate the HTML for a lock
   */
  public static function generateLock($mode, $id, $type, $codes, $options, $metabox_on_posts = false)
  {
    $path = plugin_dir_url(dirname(__FILE__));
    $ajaxurl = $path . 'fast-ajax.php';
    $lang = $mode === 'edit' ? self::get_language() : self::get_lock_language($id);
    $theme = Lockee_Settings::get_instance()->get_theme();

    if ($mode !== 'edit') {
      $options = self::apply_global_options($options);
    }

    $system_theme = '';

    if ($theme === 'system') {
      $system_theme = 'data-system-theme="true" ';
    }

    $transparent_bg = Lockee_Settings::get_instance()->is_transparent_bg() ? true : false;

    if (in_array($type, ['D8', 'S4', 'G1', 'G2', 'O1', 'O2', 'Q1', 'Q2', 'P'])) {
      $updated = self::process_old_type_and_options($type, $options);
      $type = $updated['type'];
      $options = $updated['options'];
    }

    if ($mode === 'edit') {
      $prefix = 'lck-';
    } else {
      $prefix = uniqid('lck-') . '-';
    }

    if ($metabox_on_posts) {
      $noneOption = ' data-none="true"';
    } else {
      $noneOption = '';
    }

    return '<div ' . $system_theme . 'data-theme="' . $theme . '"><div class="lck-block-container' . ($transparent_bg ? ' no-bg' : '') . '"><div class="lck-wrapperlock" reactComponent="Lock" data-id="' . $id . '" data-type="' . $type . '" data-codes="' . esc_attr($codes) . '" data-options="' . esc_attr($options) . '" data-mode="' . $mode . '" data-lang="' . $lang . '" data-path="' . $path . '" data-ajaxurl="' . $ajaxurl . '" data-prefix="' . $prefix . '" data-exclude="T"' . $noneOption . '></div></div></div>';
  }

  /**
   * Get the language of the site
   */
  public static function get_language()
  {
    $lang = get_locale();
    $lang = explode('_', $lang);
    $lang = count($lang) > 0 ? $lang[0] : self::DEFAULT_LANGUAGE;
    $supported_languages = self::SUPPORTED_LANGUAGES;
    return in_array($lang, $supported_languages) ? $lang : self::DEFAULT_LANGUAGE;
  }

  /**
   * Get the language configured for a lock, or the site default.
   */
  public static function get_lock_language($post_id)
  {
    if (empty($post_id) || $post_id === 'new') {
      return self::get_language();
    }

    $lang = get_post_meta($post_id, 'lockee_lang', true);
    if (empty($lang)) {
      return self::get_language();
    }

    return in_array($lang, self::SUPPORTED_LANGUAGES, true) ? $lang : self::get_language();
  }

  /**
   * Get the supported languages for lock configuration.
   */
  public static function get_supported_languages()
  {
    return self::SUPPORTED_LANGUAGES;
  }

  public static function process_old_type_and_options($lock_type, $lock_options)
  {
    $options = $lock_options ? explode(',', $lock_options) : [];

    switch ($lock_type) {
      case 'D8':
        $type = 'D';
        $options[] = 'D8';
        break;
      case 'S4':
        $type = 'S';
        $options[] = 'S4';
        break;
      case 'G1':
        $type = 'G';
        break;
      case 'G2':
        $type = 'G';
        $options[] = 'G2';
        break;
      case 'O1':
        $type = 'O';
        break;
      case 'O2':
        $type = 'O';
        $options[] = 'O5';
        break;
      case 'Q1':
        $type = 'O';
        $options[] = 'Q';
        break;
      case 'Q2':
        $type = 'O';
        $options[] = 'O5';
        $options[] = 'Q';
        break;
      case 'P':
        $type = 'P';
        if (in_array('CAP', $options)) {
          $options[] = 'C';
          $options[] = 'A';
          $options[] = 'P';
        } else if (in_array('CP', $options)) {
          $options[] = 'C';
          $options[] = 'P';
        } else if (in_array('CA', $options)) {
          $options[] = 'C';
          $options[] = 'A';
        } else if (in_array('AP', $options)) {
          $options[] = 'A';
          $options[] = 'P';
        }
        $options = array_diff($options, ['CAP', 'CP', 'CA', 'AP']);
        break;
      default:
        $type = $lock_type;
        break;
    }

    $options = implode(',', $options);

    return [
      'type' => $type,
      'options' => $options
    ];
  }

  public static function apply_global_options($options)
  {
    $options_array = explode(',', $options);

    $global_options = Lockee_Settings::get_instance()->get_lock_settings();

    $mapOptions = array(
      "confetti" => "CFT",
      "play_sound" => "SND",
      "backspace_button" => "DEL",
    );

    foreach ($mapOptions as $option => $option_code) {
      if ($global_options[$option] == 'on' && !in_array($option_code, $options_array)) {
        $options_array[] = $option_code;
      } else if ($global_options[$option] == 'off' && in_array($option_code, $options_array)) {
        $options_array = array_diff($options_array, [$option_code]);
      }
    }

    return implode(',', $options_array);
  }
}
