#!/usr/bin/env php
<?php

/**
 * @file
 * Controller for the visual sitemap.
 */

use AKlump\LoftLib\Bash\Bash;
use AKlump\LoftLib\Bash\Color;
use AKlump\LoftLib\Bash\Output;
use AKlump\LoftLib\Storage\FilePath;
use AKlump\VisualSitemap\VisualSitemap;

define('ROOT', dirname(__FILE__));

require_once ROOT . '/includes/autoload.inc';

try {
  $cli = new Bash($argv);
  $format = $cli->getParam('format', 'html');

  // Support for --state=* to produce all files.
  if ($cli->getParam('state') === '*') {
    $definition = FilePath::create($cli->getArg(1))->load()->getJson(TRUE);
    $all_states = VisualSitemap::getDefinedStates($definition);
    $all_states[] = NULL;
    $key = array_search('--state=*', $argv);
    $files_saved = [];
    foreach ($all_states as $state) {
      $edited_argv = $argv;
      $edited_argv[$key] = '--state=' . $state;
      if (!$state) {
        unset($edited_argv[$key]);
      }
      $cli = new Bash($edited_argv);
      require ROOT . '/includes/bootstrap.inc';
      $files_saved = array_merge($files_saved, $vismap->generate()->save());
    }
  }
  else {
    require ROOT . '/includes/bootstrap.inc';
    switch ($format) {
      case 'json':
        $files_saved = $vismap->generateJson()->save();
        break;

      case 'list':
        $files_saved = $vismap->generateTextList()->save();
        break;

      case 'html':
        $files_saved = $vismap->generate()->save();
        break;

    }
  }
}
catch (\Exception $exception) {
  echo Color::wrap('red', $exception->getMessage() . PHP_EOL);
  exit(1);
}

echo Color::wrap('green', 'Files created:') . PHP_EOL;
echo Output::tree($files_saved);

exit(0);
