#!perl6

use Sparrow6::Task::Repository;
use Sparrow6::Task;
use Sparrow6::RakuTask;
use Sparrow6::SparrowTask;

sub MAIN (
  $thing?,
  Bool :$debug            = False,
  Bool :$verbose          = False,
  Bool :$clean            = False,
  Bool :$help             = False,
  Bool :$index-update     = False,
  Bool :$list             = False,
  Bool :$search           = False,
  Bool :$upload           = False,
  Bool :$repo-init        = False,
  Bool :$repo-info        = False,
  Bool :$install          = False,
  Bool :$uninstall        = False,
  Bool :$plg-run          = False,
  Bool :$plg-info         = False,
  Bool :$plg-man          = False,
  Bool :$task-run         = False,
  Bool :$task-cat         = False,
  Bool :$task-list        = False,
  Bool :$task-del         = False,
  Bool :$rt-run         = False,
  Bool :$rt-set         = False,
  Bool :$rt-cat         = False,
  Bool :$rt-list        = False,
  Bool :$rt-del         = False,
  Bool :$module-run       = False,
  Bool :$force            = False,
  Str  :$prefix,
)

{

  my $repo-api = Sparrow6::Task::Repository::Api.new(
    debug   => $debug,
    prefix  => $prefix
  );

  my $task-cli = Sparrow6::Task::Cli.new(
    debug   => $debug,
    prefix  => $prefix
  );

  my $raku-task-cli = Sparrow6::RakuTask::Cli.new(
    debug   => $debug,
    prefix  => $prefix
  );

  my $sparrow-task-cli = Sparrow6::SparrowTask::Cli.new(
    debug   => $debug,
    prefix  => $prefix
  );

  if ($help) {
    $task-cli.help;
    exit;
  }

  if $repo-init {

    $repo-api.console("repo initialization");
    $repo-api.repo-init($thing);

  } elsif $repo-info {

    $repo-api.console("repo information");
    $repo-api.repo-info();

  } elsif $upload {

    $repo-api.console("upload plugin");

    $repo-api.plugin-upload( %( force => $force ) );

  } elsif $install && $thing {

    $repo-api.console("install plugin $thing");

    $repo-api.plugin-install($thing, %( force => $force, verbose => $verbose ));

  } elsif $uninstall && $thing {

    $repo-api.console("uninstall plugin $thing");

    $repo-api.plugin-uninstall($thing, %( verbose => $verbose ));

  } elsif $plg-info && $thing {

    $repo-api.console("plugin $thing info");

    $repo-api.plugin-info($thing, %( verbose => $verbose ));

  } elsif $task-run && $thing {

    $sparrow-task-cli.console("run sparrow task $thing");

    for $thing.split(/ "+" /) -> $i {
      $sparrow-task-cli.task-run($i);
    }

  } elsif $rt-run && $thing {

    $raku-task-cli.console("run raku task $thing");

    $raku-task-cli.task-run($thing);

  } elsif $module-run && $thing {

    $task-cli.console("run module $thing");

    for $thing.split(/ "+" /) -> $i {
      $task-cli.module-run($i);
    }

  } elsif $plg-run && $thing {

    $task-cli.console("run plg $thing");

    for $thing.split(/ "+" /) -> $i {
      $task-cli.plg-run($i);
    }

  } elsif $plg-man && $thing {

    $repo-api.console("man plg $thing");

    $repo-api.plugin-man($thing);

  } elsif $module-run && $thing {

    $task-cli.console("run module $thing");

  } elsif $rt-set && $thing {

    $raku-task-cli.console("set raku task $thing");

    $raku-task-cli.task-set($thing);

  } elsif $task-cat && $thing {

    $sparrow-task-cli.console("show sparrow task $thing");

    $sparrow-task-cli.task-cat($thing);

  } elsif $rt-cat && $thing {

    $raku-task-cli.console("show raku task $thing");

    $raku-task-cli.task-cat($thing);

  } elsif $task-del && $thing {

    $sparrow-task-cli.console("delete sparrow task $thing");

    $sparrow-task-cli.task-del($thing);

  } elsif $rt-del && $thing {

    $raku-task-cli.console("delete raku task $thing");

    $raku-task-cli.task-del($thing);

  } elsif $task-list {

    $sparrow-task-cli.console("list sparrow tasks");

    $sparrow-task-cli.task-list($thing||'.');

  } elsif $rt-list {

    $raku-task-cli.console("list raku tasks");

    $raku-task-cli.task-list;

  } elsif $index-update {

    $repo-api.console("update local index");

    $repo-api.index-update();

  } elsif $list {

    $repo-api.console("installed plugins");

    for $repo-api.installed-plugins() -> $i {
      say "{$i[0]} ... $i[1] ... $i[2]"
    }

  } elsif $search {

    $repo-api.console("search plugins");
    for $repo-api.search-plugins($thing) -> $i {
      if $i[1].defined {
        say "{$i[0]} ... {$i[1]} ... {$i[2]}"
      } else {
        say "{$i[0]} ... Nil ... {$i[2]}"
      }
    }
  } else {

    $task-cli.help();

    exit(10);

  }

}
