<?php

class Preloadfonts extends Metabox
{
	/**
	 * Unique of metabox.
	 *
	 * @var string
	 */
	public $unique_id = 'preloadfonts_box_id';

	/**
	 * Unique of metabox.
	 *
	 * @var string
	 */
	public $title = 'Preload Fonts (Optional)';

	/**
	 * Context of metabox.
	 *
	 * @var string
	 */
	public $context = 'normal';

	/**
	 * Method called via autoloader for action.
	 *
	 * @return void
	 */
	public function action()
	{
		// $flex_obj = flo_get_option_buffer('flex_active_stylekits_names');
  // if (!empty($flex_obj)) {
  //   //add and save metabox
  //   add_action('wp_ajax_save_preloadfonts_with_ajax', [$this, 'save_preloadfonts_with_ajax']);
  //   add_action('add_meta_boxes', [$this, 'add'], 5);
  //   add_action('save_post', [$this, 'save']);
  // }
	}
	/**
	 * Priority of metabox.
	 *
	 * @var string
	 */
	public $priority = 'high';

	public function save($post_id)
	{
		$toSave = isset($_POST['stylekit-metabox__preload']) ? $_POST['stylekit-metabox__preload'] : json_encode([]);
		update_post_meta($post_id, 'stylekit-metabox__preload', $toSave);
	}

	public function html($post)
	{
		$post_id = get_the_ID();
		$activation_key = get_option('flotheme_theme_key');
		$preload = get_post_meta($post_id, 'stylekit-metabox__preload', TRUE); ?>
  <?php if ($activation_key != NULL && FLEX_DEV_FEATURES_ENABLED === TRUE) : ?>
    <div class="stylekit-metabox">
      <div class="stylekit-preloaded-info">
        <label>Select Fonts to Preload</label>
        <div class="stylekit-preloaded-desc">
          Font Preloading lets you define specific fonts that a browser must request earlier without blocking the rendering of your page. Find more details on font preloading and how to detect fonts that needs to be preloaded in this tutorial.
          <br />
          <a href="#">Learn more</a>
        </div>
      </div>
      <div class="stylekit-preloaded-fonts">
        <label>Fonts</label>

        <div class="stylekit-preloaded-select-fonts">
          <select multiple="multiple" name="stylekit-metabox__preload[]" id="stylekit-preloaded-fonts-list"
          data-value='<?php echo $preload ? json_encode($preload) : json_encode([]); ?>'
          ></select>
          <a class="stylekit-preloaded-clear-font" href="#">Clear all fonts</a>
        </div>
      </div>
    </div>
  <?php else : ?>
    <div class="stylekit-metabox--no-key">
      <div class="stylekit-metabox--no-key__item">
        <h1>We can't find your purchase code</h1>
        <p>Please add purchase code, or buy product</p>
      </div>
    </div>
  <?php endif; ?>
<?php
	}
}
