<?php

namespace engine\skin;

use Twig\Loader\FilesystemLoader;
use Twig\Loader\LoaderInterface;

class ErrorSkin
    extends BaseSkin
{
    const string TEMPLATES_ROOT = APP_ROOT.'/src/skins/error';

    protected function getTwigLoader(): LoaderInterface {
        return new FilesystemLoader(self::TEMPLATES_ROOT, APP_ROOT);
    }

    protected function getCacheDir(): string {
        global $config;
        return $config['skin_cache_'.(isDev() ? 'dev' : 'prod').'_dir'].'/error';
    }

    public function renderNotFound(): never {
        global $globalContext;
        if (isset($globalContext->project) && file_exists(self::TEMPLATES_ROOT.'/notfound_'.$globalContext->project.'.twig'))
            $template = 'notfound_'.$globalContext->project.'.twig';
        else
            $template = 'notfound.twig';
        echo $this->twig->render($template);
        exit;
    }

    public function renderError(string $title, ?string $message = null, ?string $stacktrace = null): never {
        echo $this->twig->render('error.twig', [
            'title' => $title,
            'stacktrace' => $stacktrace,
            'message' => $message,
        ]);
        exit;
    }
}