#!/usr/bin/php
<?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
require(__DIR__.'/../vendor/autoload.php');

use nur\A;
use nur\b\ValueException;
use nur\cli\Application;
use nur\msg;

Application::run(new class extends Application {
  const ARGS = [
    "merge" => Application::ARGS,
    ["-l:", "--level"],
    ["-t:", "--type"],
    ["-r:", "--result"],
  ];
  private $args;
  private $level, $type, $result;

  const LEVEL_MAP = [
    "1" => msg::CRITICAL, "X" => msg::CRITICAL,
    "2" => msg::MAJOR, "M" => msg::MAJOR,
    "3" => msg::NORMAL, "n" => msg::NORMAL,
    "4" => msg::MINOR, "m" => msg::MINOR,
  ];
  const TYPE_MAP = [
    "1" => msg::ERROR, "e" => msg::ERROR,
    "2" => msg::WARNING, "w" => msg::WARNING,
    "3" => msg::INFO, "i" => msg::INFO,
    "4" => msg::DEBUG, "d" => msg::DEBUG,
  ];
  const RESULT_MAP = [
    "0" => msg::NONE, "n" => msg::NONE,
    "1" => msg::NEUTRAL, "s" => msg::NEUTRAL,
    "2" => msg::SUCCESS, "ok" => msg::SUCCESS,
    "3" => msg::FAILURE, "ko" => msg::FAILURE,
  ];

  function main() {
    msg::info("Profil sélectionné: ".self::profile());

    msg::action("calcul des arguments");
    $level = A::get(self::LEVEL_MAP, $this->level, msg::NORMAL);
    $type = A::get(self::TYPE_MAP, $this->type, msg::INFO);
    $result = A::get(self::RESULT_MAP, $this->result, msg::NONE);
    msg::asuccess();

    if ($this->args) {
      $msg = msg::get();
      foreach($this->args as $message) {
        $msg->addMessage($message, $type + $result, $level);
      }
    } else {
      msg::error("error message");
      msg::warning("warning message");
      msg::note("note message");
      msg::info("info message");
      msg::step("step message");
      msg::success("success message");
      msg::failure("failure message");
      msg::debug("debug message");
      msg::group("action multiple 1");
      msg::step("étape 1");
      msg::asuccess("effectué avec succès");
      msg::group("action multiple 2");
      msg::step("étape 2.1");
      msg::step("étape 2.2");
      msg::afailure("erreur d'étape");
      msg::action("fonction sans valeur de retour", function () {
        echo "sortie de la fonction sans valeur de retour\n";
      });
      msg::action("fonction true", function () {
        return true;
      });
      msg::action("fonction false", function () {
        return false;
      });
      msg::action("fonction string", function () {
        return "résultat de la fonction string";
      });
      msg::action("fonction exception", function () {
        throw new ValueException("message de l'exception");
      });
    }
  }
});