<?php
namespace nur;

use nur\b\ExitError;
use nur\b\ui\AbstractMessenger;
use nur\b\ui\IMessenger;
use nur\b\ValueException;
use Throwable;

class _messenger {
  /** @var string */
  protected static $messenger_class;

  /** @var IMessenger */
  protected static $messenger;

  static final function get(): IMessenger {
    return self::$messenger;
  }

  static final function set_messenger_class(string $messenger_class, bool $unless_nn=false): void {
    if ($unless_nn && self::$messenger_class !== null) return;
    ValueException::check_class($messenger_class, IMessenger::class);
    self::$messenger_class = $messenger_class;
    self::$messenger = new $messenger_class();
  }

  static final function push(?IMessenger &$old_messenger=null, ?IMessenger $messenger=null, $params=null): IMessenger {
    if ($old_messenger === null) {
      $old_messenger = self::$messenger;
      if ($messenger === null) $messenger = new self::$messenger_class($params);
      self::$messenger = $messenger;
    }
    return self::$messenger;
  }

  static final function pop(IMessenger $old_messenger): void {
    self::$messenger = $old_messenger;
  }

  static final function __callStatic($name, $args) {
    $name = str::us2camel($name);
    return call_user_func_array([self::get(), $name], $args);
  }

  #############################################################################

  # Types de messages
  const ERROR = IMessenger::TYPE_ERROR;
  const WARNING = IMessenger::TYPE_WARNING;
  const INFO = IMessenger::TYPE_INFO;
  const DEBUG = IMessenger::TYPE_DEBUG;
  const NONE = IMessenger::RESULT_NONE;
  const NEUTRAL = IMessenger::RESULT_NEUTRAL;
  const SUCCESS = IMessenger::RESULT_SUCCESS;
  const FAILURE = IMessenger::RESULT_FAILURE;

  # Niveaux de messages
  const NEVER = IMessenger::LEVEL_NEVER;
  const CRITICAL = IMessenger::LEVEL_CRITICAL;
  const MAJOR = IMessenger::LEVEL_MAJOR;
  const NORMAL = IMessenger::LEVEL_NORMAL;
  const MINOR = IMessenger::LEVEL_MINOR;

  # Composantes d'un message
  const USER = IMessenger::KEY_USER;
  const TECH = IMessenger::KEY_TECH;
  const EXCEPTION = IMessenger::KEY_EXCEPTION;

  # Levels
  const PRINT_LEVELS = AbstractMessenger::PRINT_LEVELS;
  const LOG_LEVELS = AbstractMessenger::LOG_LEVELS;
  const TYPE_LEVELS = AbstractMessenger::TYPE_LEVELS;
  const DEBUG_LEVELS = [
    self::USER => self::MINOR,
    self::TECH => self::MINOR,
    self::EXCEPTION => self::MINOR,
  ];

  # Destination
  const DEFAULT_DEST = null;

  protected static function _type(?int $result, ?int $type, int $default_type): int {
    if ($type === null) $type = $default_type;
    if (($type & IMessenger::RESULT_MASK) === 0 && $result !== null) $type += $result;
    if (($type & IMessenger::DEST_MASK) === 0) $type += static::DEFAULT_DEST;
    return $type;
  }
  static function _error(?int $result=null, ?int $type=null): int { return self::_type($result, $type, self::ERROR); }
  static function _warning(?int $result=null, ?int $type=null): int { return self::_type($result, $type, self::WARNING); }
  static function _info(?int $result=null, ?int $type=null): int { return self::_type($result, $type, self::INFO); }
  static function _debug(?int $result=null, ?int $type=null): int { return self::_type($result, $type, self::DEBUG); }

  protected static function _level(?int $level, int $default_level): int {
    if ($level === null) $level = $default_level;
    return $level;
  }
  static function _critical(?int $level=null): int { return self::_level($level, self::CRITICAL); }
  static function _major(?int $level=null): int { return self::_level($level, self::MAJOR); }
  static function _normal(?int $level=null): int { return self::_level($level, self::NORMAL); }
  static function _minor(?int $level=null): int { return self::_level($level, self::MINOR); }

  static final function section($title=null, ?int $level=null): IMessenger { return self::get()->startSection($title, self::_info(), self::_major($level)); }
  static final function group($prefix, ?int $count=null, ?int $level=null): IMessenger { return self::get()->startGroup($prefix, $count, self::_info(), self::_major($level)); }
  static final function action($message, $result=null, ?array $args=null, ?int $level=null): IMessenger { return self::get()->action($message, $result, $args, self::_info(), self::_major($level)); }
  static final function aresult($result, ?array $args=null, $message=null): IMessenger { return self::get()->aresult($result, $args, $message, self::_info()); }
  static final function astep($message=null): IMessenger { return self::get()->astep($message, self::_info()); }
  static final function asuccess($message=null): IMessenger { return self::get()->asuccess($message, self::_info()); }
  static final function afailure($message=null, ?Throwable $e=null): IMessenger { return self::get()->afailure($message, $e, self::_info()); }
  static final function end(bool $all=false): IMessenger { return self::get()->end($all); }

  static final function error($message, ?int $level=null): IMessenger { return self::get()->addMessage($message, self::_error(), self::_major($level)); }
  static final function warning($message, ?int $level=null): IMessenger { return self::get()->addMessage($message, self::_warning(), self::_major($level)); }
  static final function note($message, ?int $level=null): IMessenger { return self::get()->addMessage($message, self::_info(), self::_critical($level)); }
  static final function info($message, ?int $level=null): IMessenger { return self::get()->addMessage($message, self::_info(), self::_major($level)); }
  static final function step($message, ?int $level=null): IMessenger { return self::get()->addMessage($message, self::_info(self::NEUTRAL), self::_major($level)); }
  static final function success($message, ?int $level=null): IMessenger { return self::get()->addMessage($message, self::_info(self::SUCCESS), self::_major($level)); }
  static final function failure($message, ?int $level=null): IMessenger { return self::get()->addMessage($message, self::_info(self::FAILURE), self::_major($level)); }
  static final function debug($message, ?int $level=null, ?int $result=null): IMessenger { return self::get()->addMessage($message, self::_debug($result), self::_major($level)); }

  static final function print($message): IMessenger {
    return self::get()->addMessage([self::USER => $message], self::_info(), self::_normal());
  }

  static final function die($message): void { throw new ExitError(1, $message); }
}
