<?php

class MbComponentBlocks
{
  private $common_fields = null;

  /** @var self */
  private static $_instance;

  public static function instance(): self
  {
    if (!self::$_instance) {
      self::$_instance = new self();
    }
    return self::$_instance;
  }

  function init_hooks()
  {
    add_filter("rwmb_block_classes", [$this, "add_block_classes"], 10, 2);
  }

  function get_common_fields()
  {
    if (!$this->common_fields) {
      $this->init_common_fields();
    }
    return $this->common_fields;
  }

  function add_block_classes($cls, $attr)
  {
    if (Inoby_Config::block_dark_mode()) {
      $cls .= $attr["data"]["dark_mode"] ?? false ? " dark" : " light";
    }
    return $cls;
  }

  private function init_common_fields()
  {
    $this->common_fields = [];

    if (Inoby_Config::block_dark_mode()) {
      $this->init_appearance_section();
      $this->init_dark_light_theme();
    }

    // return apply_filters("component_mb_common_fields", $this->common_fields);
    $this->common_fields = apply_filters("component_mb_common_fields", $this->common_fields);
  }

  private function init_appearance_section()
  {
    $this->common_fields[] = [
      "type" => "divider",
    ];
    $this->common_fields[] = [
      "type" => "custom_html",
      "name" => "<h3>" . __("Appearance") . "</h3>",
    ];
  }

  private function init_dark_light_theme()
  {
    $this->common_fields[] = [
      "id" => "dark_mode",
      "name" => __("Color mode"),
      "type" => "switch",
      "style" => "rounded",
      "on_label" => __("Dark") . "&nbsp;<i class=\"fas fa-moon\"></i>",
      "off_label" => "<i class=\"fas fa-sun\"></i>&nbsp;" . __("Light"),
      "std" => false,
    ];
  }
}

MbComponentBlocks::instance()->init_hooks();