#!/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\say;

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" => say::CRITICAL, "X" => say::CRITICAL,
    "2" => say::MAJOR, "M" => say::MAJOR,
    "3" => say::NORMAL, "n" => say::NORMAL,
    "4" => say::MINOR, "m" => say::MINOR,
  ];
  const TYPE_MAP = [
    "1" => say::ERROR, "e" => say::ERROR,
    "2" => say::WARNING, "w" => say::WARNING,
    "3" => say::INFO, "i" => say::INFO,
    "4" => say::DEBUG, "d" => say::DEBUG,
  ];
  const RESULT_MAP = [
    "0" => say::NONE, "n" => say::NONE,
    "1" => say::NEUTRAL, "s" => say::NEUTRAL,
    "2" => say::SUCCESS, "ok" => say::SUCCESS,
    "3" => say::FAILURE, "ko" => say::FAILURE,
  ];

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

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

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