<?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;
use function _\find;

class IndexControl
{
  public static function start(
    ServerRequestInterface $request,
    ResponseInterface $response,
    array $args
  ): ResponseInterface {
    //unset($_SESSION);
    $config = new Settings();
    $settings = $config->getSettings();
    $view = Twig::fromRequest($request);
    //checks dynamic render flag for site render status
    if ($settings["global"]["dynamicRender"]) {
      if ($settings["global"]["dynamicRender"] == "true") {
        $loader = new \Twig\Loader\FilesystemLoader("../content/themes");
        $display = new \Twig\Environment($loader, []);
        $template = "";
        $pageOptions = [];

        $pageInfo = [
          "keywords" => isset($settings["global"]["keywords"])
            ? $settings["global"]["keywords"]
            : "fipamo, blog, jamstack, php, markdown, js",
          "description" => $settings["global"]["descriptions"],
          "image" =>
            $settings["global"]["base_url"] . $settings["global"]["background"],
          "baseURL" => $settings["global"]["base_url"],
        ];

        if (isset($args["first"])) {
          switch ($args["first"]) {
            case "tags":
              $template = $settings["global"]["theme"] . "/tags.twig";
              $tag = trim($args["second"]);
              $taglist = Sorting::tags();
              $item = find($taglist, ["tag_name" => $tag]);
              $pageOptions = [
                "title" => "Pages Tagged as " . $item["tag_name"],
                "background" => $pageInfo["image"],
                "tag_list" => $item["pages"],
                "info" => $pageInfo,
                "menu" => $settings["menu"],
                "dynamicRender" => $settings["global"]["dynamicRender"],
              ];
              break;
            case "archives":
              $archive = Sorting::archive();
              $template = $settings["global"]["theme"] . "/archive.twig";
              $pageOptions = [
                "title" => "Archive",
                "background" => $pageInfo["image"],
                "archives" => $archive,
                "info" => $pageInfo,
                "menu" => $settings["menu"],
                "dynamicRender" => $settings["global"]["dynamicRender"],
              ];

              break;
            default:
              //check layout to see what page should be rendered
              $template = $settings["global"]["theme"] . "/page.twig";
              $book = new Book("../content/pages");
              $page = $book->findPageBySlug($args["third"]);
              $pageOptions = Sorting::page($page);

              break;
          }
        } else {
          //index
          $template = $settings["global"]["theme"] . "/index.twig";
          $book = new Book("../content/pages");
          $page = $book->findPageBySlug();
          $pageOptions = Sorting::page($page);
        }

        $html = $display->render($template, $pageOptions);
        $response->getBody()->write($html);
        return $response;
      } else {
        $view = Twig::fromRequest($request);
        $html = file_get_contents("../public/index.html");
        $response->getBody()->write($html);
        return $response;
      }
    } else {
      //if flag is not present, default to static html
      $view = Twig::fromRequest($request);
      $html = file_get_contents("../public/index.html");
      $response->getBody()->write($html);
      return $response;
    }
  }
}
