<?php
namespace nur\sery\tools;

use nur\sery\output\msg;
use nur\sery\php\time\DateTime;
use nur\sery\text\words;
use nur\sery\app;
use nur\sery\app\cli\Application;

class SteamTrainApp extends Application {
  const PROJDIR = __DIR__.'/../..';
  const TITLE = "Train à vapeur";
  const USE_LOGFILE = true;
  const USE_RUNFILE = true;
  const USE_RUNLOCK = true;

  const ARGS = [
    "purpose" => self::TITLE,
    "description" => <<<EOT
Cette application peut être utilisée pour tester le lancement des tâches de fond
EOT,

    ["-c", "--count", "args" => 1,
      "help" => "spécifier le nombre d'étapes",
    ],
    ["-f", "--force-enabled", "value" => true,
      "help" => "lancer la commande même si les tâches planifiées sont désactivées",
    ],
    ["-n", "--no-install-signal-handler", "value" => false,
      "help" => "ne pas installer le gestionnaire de signaux",
    ],
  ];

  protected $count = 100;

  protected bool $forceEnabled = false;

  protected bool $installSignalHandler = true;

  function main() {
    app::check_bgapplication_enabled($this->forceEnabled);
    if ($this->installSignalHandler) app::install_signal_handler();
    $count = intval($this->count);
    msg::info("Starting train for ".words::q($count, "step#s"));
    app::action("Running train...", $count);
    for ($i = 1; $i <= $count; $i++) {
      msg::print("Tchou-tchou! x $i");
      app::step();
      sleep(1);
    }
    msg::info("Stopping train at ".new DateTime());
  }
}
