<?php

namespace app\foreignone;

use app\ThemesUtil;
use engine\http\HtmlResponse;
use engine\http\Response;
use engine\skin\FeaturedSkin;
use Twig\Loader\FilesystemLoader;
use Twig\Loader\LoaderInterface;

class ForeignOneSkin
    extends FeaturedSkin
{
    public ForeignOneSkinOptions $options;
    public string $title {
        get {
            $title = isset($this->title) && $this->title ? $this->title : lang('site_title');
            if (!$this->options->isIndex)
                $title = $title.' - '.lang('4in1');
            return $title;
        }

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

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

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

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

    public function renderPage(string $template, array $vars = []): Response {
        $this->exportStrings(['4in1']);
        $this->applyGlobals();

        // render body first
        $b = $this->renderBody($template, $vars);

        // then everything else
        $h = $this->renderHeader();
        $f = $this->renderFooter();

        return new HtmlResponse($h.$b.$f);
    }

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

        $body_class = [];
        if ($this->options->fullWidth)
            $body_class[] = 'full-width';
        else if ($this->options->wide)
            $body_class[] = 'wide';

        $vars = [
            'title' => $this->title,
            'meta_html' => $this->meta->getHtml(),
            'static_html' => $this->getHeaderStaticTags(),
            'svg_html' => $this->getSVGTags(),
            'render_options' => $this->options->getOptions(),
            'ic_url' => 'https://ic'.(isDev() ? 'dev' : '').'.'.$config['domain'],
            'app_config' => [
                'domain' => $config['domain'],
                'devMode' => isDev(),
                'cookieHost' => $config['cookie_host'],
            ],
            'body_class' => $body_class,
            '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['admin_email'],
            'script_html' => $this->getFooterScriptTags(),
            'theme' => ThemesUtil::getUserTheme(),
        ];
        return $this->doRender('footer.twig', $footer_vars);
    }
}