#!/usr/bin/php
<?php
require(__DIR__.'/../vendor/autoload.php');

use nulib\output\msg;
use nulib\UserException;
use nur\cli\Application;

Application::run(new class extends Application {
  function main() {
    msg::title("title0");
    msg::title("title1");
    msg::print("print under title1");
    msg::end();
    msg::print("print under title0");
    msg::end();

    msg::desc("action avec step");
    msg::action("action avec step");
    msg::step("step");
    msg::asuccess("action success");

    msg::action("action avec step");
    msg::step("step");
    msg::afailure("action failure");

    msg::action("action avec step");
    msg::step("step");
    msg::adone("action neutral");

    msg::desc("actions sans step");
    msg::action("action sans step");
    msg::asuccess("action success");

    msg::action("action sans step");
    msg::afailure("action failure");

    msg::action("action sans step");
    msg::adone("action neutral");

    msg::desc("actions imbriquées");
    msg::action("action0");
    msg::action("action1");
    msg::action("action2");
    msg::asuccess("action2 success");
    msg::asuccess("action1 success");
    msg::asuccess("action0 success");

    msg::desc("action avec step, sans messages");
    msg::action("action avec step, sans messages, success");
    msg::step("step");
    msg::asuccess();

    msg::action("action avec step, sans messages, failure");
    msg::step("step");
    msg::afailure();

    msg::action("action avec step, sans messages, done");
    msg::step("step");
    msg::adone();

    msg::desc("action sans step, sans messages");
    msg::action("action sans step, sans messages, success");
    msg::asuccess();

    msg::action("action sans step, sans messages, failure");
    msg::afailure();

    msg::action("action sans step, sans messages, done");
    msg::adone();

    msg::desc("actions imbriquées, sans messages");
    msg::action("action0");
    msg::action("action1");
    msg::action("action2");
    msg::asuccess();
    msg::asuccess();
    msg::asuccess();

    msg::info("info");
    msg::note("note");
    msg::warning("warning");
    msg::error("error");

    msg::section("section", function ($msg) {
      $msg->title("title", function ($msg) {
        $msg->desc("desc");
        $msg->print("print");

        $msg->desc("action avec step");
        $msg->action("action avec step", function ($msg) {
          $msg->step("step");
          $msg->asuccess("action success");
        });

        $msg->action("action avec step", function ($msg) {
          $msg->step("step");
          $msg->afailure("action failure");
        });

        $msg->action("action avec step", function ($msg) {
          $msg->step("step");
          $msg->adone("action done");
        });

        $msg->desc("actions sans step");
        $msg->action("action sans step", function ($msg) {
          $msg->asuccess("action success");
        });

        $msg->action("action sans step", function ($msg) {
          $msg->afailure("action failure");
        });

        $msg->action("action sans step", function ($msg) {
          $msg->adone("action done");
        });

        $msg->desc("actions imbriquées");
        $msg->action("action0", function ($msg) {
          $msg->action("action1", function ($msg) {
            $msg->action("action2", function ($msg) {
              $msg->asuccess("action2 success");
            });
            $msg->asuccess("action1 success");
          });
          $msg->asuccess("action0 success");
        });

        $msg->desc("action avec step, sans messages");
        $msg->action("action avec step, sans messages, success", function ($msg) {
          $msg->step("step");
          $msg->asuccess();
        });

        $msg->action("action avec step, sans messages, failure", function ($msg) {
          $msg->step("step");
          $msg->afailure();
        });

        $msg->action("action avec step, sans messages, done", function ($msg) {
          $msg->step("step");
          $msg->adone();
        });

        $msg->desc("action sans step, sans messages");
        $msg->action("action sans step, sans messages, success", function ($msg) {
          $msg->asuccess();
        });

        $msg->action("action sans step, sans messages, failure", function ($msg) {
          $msg->afailure();
        });

        $msg->action("action sans step, sans messages, done", function ($msg) {
          $msg->adone();
        });

        $msg->desc("actions imbriquées, sans messages");
        $msg->action("action0", function ($msg) {
          $msg->action("action1", function ($msg) {
            $msg->action("action2", function ($msg) {
              $msg->asuccess();
            });
            $msg->asuccess();
          });
          $msg->asuccess();
        });

        $msg->desc("action avec step, avec code de retour");
        $msg->action("action avec step, avec code de retour true", function ($msg) {
          $msg->step("step");
          return true;
        });

        $msg->action("action avec step, avec code de retour false", function ($msg) {
          $msg->step("step");
          return false;
        });

        $msg->action("action avec step, avec code de retour autre", function ($msg) {
          $msg->step("step");
          return "autre";
        });

        $msg->action("action avec step, avec code de retour null", function ($msg) {
          $msg->step("step");
        });

        $msg->desc("action sans step, avec code de retour");
        $msg->action("action sans step, avec code de retour true", function ($msg) {
          return true;
        });

        $msg->action("action sans step, avec code de retour false", function ($msg) {
          return false;
        });

        $msg->action("action sans step, avec code de retour autre", function ($msg) {
          return "autre";
        });

        # ici, il n'y aura pas de message du tout
        $msg->action("action sans step, avec code de retour null", function ($msg) {
        });

        $msg->info("info");
        $msg->note("note");
        $msg->warning("warning");
        $msg->error("error");
      });
    });

    msg::section("multi-line\nsection", function ($msg) {
      $msg->title("multi-line\ntitle");
      $msg->title("another\ntitle");

      $msg->print("multi-line\nprint");
      $msg->info("multi-line\ninfo");
      $msg->action("multi-line\naction");
      $msg->asuccess();
      $msg->action("multi-line\naction");
      $msg->step("multi-line\nstep");
      $msg->afailure();
      $msg->action("multi-line\naction");
      $msg->step("multi-line\nstep");
      $msg->asuccess("multi-line\nsuccess");
      $msg->action("multi-line\naction");
      $msg->step("multi-line\nstep");
      $msg->adone("multi-line\ndone");

      $msg->end();
      $msg->end();
    });

    msg::section("Exceptions", function ($msg) {
      $e = new Exception("message");
      $u1 = new UserException("userMessage1");
      $u2 = new UserException("userMessage2", "techMessage2");
      $msg->title("avec message", function ($msg) use ($e, $u1, $u2) {
        $msg->info(["exception message", $e]);
        $msg->info(["userException1 message", $u1]);
        $msg->info(["userException2 message", $u2]);
      });
      $msg->title("sans message 1", function ($msg) use ($e, $u1, $u2) {
        $msg->info([$e, null]);
        $msg->info([$u1, null]);
        $msg->info([$u2, null]);
      });
      $msg->title("sans message 2", function ($msg) use ($e, $u1, $u2) {
        $msg->info([$e]);
        $msg->info([$u1]);
        $msg->info([$u2]);
      });
      $msg->title("sans message 3", function ($msg) use ($e, $u1, $u2) {
        $msg->info($e);
        $msg->info($u1);
        $msg->info($u2);
      });
    });

    msg::title("premier niveau");
    msg::step("premier message");
    msg::title("deuxieme niveau");
    msg::step("deuxième message");
    msg::title("troisième niveau");
    msg::step("troisième message");
    msg::action("action avec plusieurs failure", function ($msg) {
      $msg->afailure("premier échec");
      $msg->afailure("deuxième échec");
      $msg->afailure("troisième échec");
    });
    msg::step("troisième conclusion");
    msg::end();
    msg::step("deuxième conclusion");
    msg::end();
    msg::step("première conclusion");
    msg::end();
    msg::step("conclusion");
  }
});
