<?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\UserException;
use nur\cli\Application;
use nur\config;
use nur\msg;

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

  const LEVEL_MAP = [
    "1" => msg::CRITICAL, "c" => 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,
  ];

  const LEVELS = [
    msg::CRITICAL => "critical",
    msg::MAJOR => "major",
    msg::NORMAL => "normal",
    msg::MINOR => "minor",
  ];

  const TYPES = [
    msg::ERROR => "error",
    msg::WARNING => "warning",
    msg::INFO => "info",
    msg::DEBUG => "debug",
  ];

  const RESULTS = [msg::NONE, msg::NEUTRAL, msg::SUCCESS, msg::FAILURE];

  function main() {
    msg::info("current profile is " . config::get_profile());

    $level = A::get(self::LEVEL_MAP, $this->level);
    $levels = A::with($level);
    if (!$levels) $levels = array_keys(self::LEVELS);

    $type = A::get(self::TYPE_MAP, $this->type);
    $types = A::with($type);
    if (!$types) $types = array_keys(self::TYPES);

    $result = A::get(self::RESULT_MAP, $this->result);
    $results = A::with($result);
    if (!$results) $results = self::RESULTS;

    $suite = $this->suite;

    $msg = msg::get();
    foreach (self::LEVELS as $level => $levelName) {
      if (!in_array($level, $levels)) continue;

      if ($suite === null || $suite == "1") {
        msg::section("$levelName messages -- user only", $level, msg::INFO);
        foreach (self::TYPES as $type => $typeName) {
          if (!in_array($type, $types)) continue;
          foreach ($results as $result) {
            $msg->addMessage("user only $levelName $typeName", $type + $result, $level);
          }
        }
      }
      if ($suite === null || $suite == "2") {
        msg::section("$levelName messages -- user + tech", $level, msg::INFO);
        foreach (self::TYPES as $type => $typeName) {
          if (!in_array($type, $types)) continue;
          foreach ($results as $result) {
            $msg->addMessage([
              "user $levelName $typeName",
              "tech $levelName $typeName",
            ], $type + $result, $level);
          }
        }
      }
      if ($suite === null || $suite == "3") {
        msg::section("$levelName messages -- UserException", $level, msg::INFO);
        $e = new UserException(["e_user", "e_tech"]);
        foreach (self::TYPES as $type => $typeName) {
          if (!in_array($type, $types)) continue;
          foreach ($results as $result) {
            $msg->addMessage($e, $type + $result, $level);
          }
        }
      }
      if ($suite === null || $suite == "4") {
        msg::section("$levelName messages -- Exception", $level, msg::INFO);
        $e = new Exception("e_message");
        foreach (self::TYPES as $type => $typeName) {
          if (!in_array($type, $types)) continue;
          foreach ($results as $result) {
            $msg->addMessage($e, $type + $result, $level);
          }
        }
      }
      if ($suite === null || $suite == "5") {
        msg::section("$levelName messages -- user, Exception", $level, msg::INFO);
        $e = new Exception("e_message");
        foreach (self::TYPES as $type => $typeName) {
          if (!in_array($type, $types)) continue;
          foreach ($results as $result) {
            $msg->addMessage(["user", $e], $type + $result, $level);
          }
        }
      }
      if ($suite === null || $suite == "6") {
        msg::section("$levelName messages -- user, tech, Exception", $level, msg::INFO);
        $e = new Exception("e_message");
        foreach (self::TYPES as $type => $typeName) {
          if (!in_array($type, $types)) continue;
          foreach ($results as $result) {
            $msg->addMessage(["user", "tech", $e], $type + $result, $level);
          }
        }
      }
      if ($suite === null || $suite == "7") {
        msg::section("$levelName messages -- user, UserException", $level, msg::INFO);
        $e = new UserException(["e_user", "e_tech"]);
        foreach (self::TYPES as $type => $typeName) {
          if (!in_array($type, $types)) continue;
          foreach ($results as $result) {
            $msg->addMessage(["user", $e], $type + $result, $level);
          }
        }
      }
      if ($suite === null || $suite == "8") {
        msg::section("$levelName messages -- user, tech, UserException", $level, msg::INFO);
        $e = new UserException(["e_user", "e_tech"]);
        foreach (self::TYPES as $type => $typeName) {
          if (!in_array($type, $types)) continue;
          foreach ($results as $result) {
            $msg->addMessage(["user", "tech", $e], $type + $result, $level);
          }
        }
      }
    }
  }
});
