<?php

namespace app\ic;

use app\foreignone\ForeignOneSkinOptions;
use app\ThemesUtil;
use engine\http\Response;
use Twig\Loader\FilesystemLoader;
use Twig\Loader\LoaderInterface;

class InvisibleCollegeSkin
    extends \engine\skin\FeaturedSkin
{
    public InvisibleCollegeSkinOptions $options;
    public string $title {
        get {
            $title = isset($this->title) && $this->title ? $this->title : lang('site_title');
            if (!$this->options->isIndex)
                $title = $title.' - '.lang('invisible_college');
            return $title;
        }

        set(string $title) {
            $this->title = $title;
        }
    }

    public function __construct() {
        parent::__construct();
        $this->options = new InvisibleCollegeSkinOptions();
    }

    protected function getTwigLoader(): LoaderInterface {
        $twig_loader = new FilesystemLoader(APP_ROOT.'/src/skins/ic', APP_ROOT);
        $twig_loader->addPath(APP_ROOT.'/src/skins/foreignone', 'foreignone');
        return $twig_loader;
    }

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

    protected function renderHeader(): string {
        global $config;

        $vars = [
            'title' => $this->title,
            'meta_html' => $this->meta->getHtml(),
            'static_html' => $this->getHeaderStaticTags(),
            'svg_html' => $this->getSVGTags(),
            'render_options' => $this->options->getOptions(),
            'home_url' => 'https://'.(isDev() ? 'dev.' : '').$config['domain'],
            'app_config' => [
                'domain' => $config['domain'],
                'devMode' => isDev(),
                'cookieHost' => $config['cookie_host'],
            ],
            'theme' => ThemesUtil::getUserTheme(),
        ];

        return $this->doRender('header.twig', $vars);
    }

    protected function renderBody(string $template, array $vars): string {
        return $this->doRender($template, $this->vars + $vars);
    }

    protected function renderFooter(): string {
        global $config;
        $footer_vars = [
            'exec_time' => getExecutionTime(),
            'render_options' => $this->options->getOptions(),
            'admin_email' => $config['ic_email'],
            'script_html' => $this->getFooterScriptTags(),
            'theme' => ThemesUtil::getUserTheme(),
        ];
        return $this->doRender('footer.twig', $footer_vars);
    }
}