<?php
namespace nur\sery\output;

use nur\sery\cl;
use nur\sery\str;
use nur\sery\ValueException;

/**
 * Class _messenger: classe de base pour say, log et msg
 */
abstract class _messenger {
  abstract static function is_setup(): bool;
  abstract static function set_messenger(IMessenger $msg);
  abstract static function get(): IMessenger;

  static function set_messenger_class(string $msg_class, ?array $params=null) {
    if (!is_subclass_of($msg_class, IMessenger::class)) {
      throw ValueException::invalid_class($msg_class, IMessenger::class);
    }
    static::set_messenger(new $msg_class($params));
  }

  static function create_or_reset_params(?array $params=null, string $log_class=null, ?array $create_params=null) {
    if (static::is_setup()) {
      self::reset_params($params);
    } else {
      $params = cl::merge($params, $create_params);
      self::set_messenger_class($log_class, $params);
    }
  }

  /** obtenir une nouvelle instance, avec un nouveau paramétrage */
  static function new(?array $params=null): IMessenger {
    return static::get()->clone($params);
  }

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

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

  const DEBUG = IMessenger::DEBUG;
  const MINOR = IMessenger::MINOR;
  const NORMAL = IMessenger::NORMAL;
  const MAJOR = IMessenger::MAJOR;
  const NONE = IMessenger::NONE;

  static function reset_params(?array $params=null): void { static::get()->resetParams($params); }
  static function section($content, ?callable $func=null, ?int $level=null): void { static::get()->section($content, $func, $level); }
  static function title($content, ?callable $func=null, ?int $level=null): void { static::get()->title($content, $func, $level); }
  static function desc($content, ?int $level=null): void { static::get()->desc($content, $level); }
  static function action($content, ?callable $func=null, ?int $level=null): void { static::get()->action($content, $func, $level); }
  static function step($content, ?int $level=null): void { static::get()->step($content, $level); }
  static function asuccess($content=null, ?int $override_level=null): void { static::get()->asuccess($content, $override_level); }
  static function afailure($content=null, ?int $override_level=null): void { static::get()->afailure($content, $override_level); }
  static function adone($content=null, ?int $override_level=null): void { static::get()->adone($content, $override_level); }
  static function aresult($result=null, ?int $override_level=null): void { static::get()->aresult($result, $override_level); }
  static function print($content, ?int $level=null): void { static::get()->print($content, $level); }
  static function info($content, ?int $level=null): void { static::get()->info($content, $level); }
  static function note($content, ?int $level=null): void { static::get()->note($content, $level); }
  static function warning($content, ?int $level=null): void { static::get()->warning($content, $level); }
  static function error($content, ?int $level=null): void { static::get()->error($content, $level); }
  static function end(bool $all=false): void { static::get()->end($all); }

  static function debug($content): void { self::info($content, self::DEBUG);}
  static function normal($content): void { self::info($content, self::NORMAL);}
  static function minor($content): void { self::info($content, self::MINOR);}
  static function important($content): void { self::info($content, self::MAJOR);}
  static function attention($content): void { self::note($content, self::MAJOR);}
  static function critwarning($content): void { self::warning($content, self::MAJOR);}
  static function criterror($content): void { self::error($content, self::MAJOR);}
}
