<?php

namespace app\ic;

use app\MarkupUtil;
use engine\http\errors\NotFound;
use engine\http\HtmlResponse;
use engine\http\HTTPCode;

class MainHandler
    extends BaseHandler
{
    protected function beforeDispatch(string $http_method, string $action) {
        if (!isAdmin() && $action != 'index')
            throw new NotFound();
    }

    public function GET_index() {
        if (!isAdmin())
            return new HtmlResponse($this->skin->render('soon.twig'), code: HTTPCode::NotFound);

        $this->skin->meta->title = lang('meta_index_title');
        $this->skin->meta->description = lang('meta_index_description');
        $this->skin->meta->url = 'https://'.$_SERVER['HTTP_HOST'].'/';
        // $this->skin->meta->image = 'https://'.$config['domain'].'/img/4in1-preview.jpg';
        $this->skin->meta->setSocial('og:type', 'website');

        $this->skin->options->isIndex = true;
        $this->skin->set([
            'tzo' => new TZO()
        ]);

        return $this->skin->renderPage('index.twig');
    }

    public function GET_books() {
        $html = MarkupUtil::markdownToHtml(file_get_contents(APP_ROOT.'/data/ic_books.md'));
        $this->skin->title = lang('books');
        $this->skin->options->headSection = 'books';
        $this->skin->set(['html' => $html]);;
        return $this->skin->renderPage('books.twig');
    }

    public function GET_tzo() {
        $tzo = new TZO();
        $this->skin->set([
            'tzo' => $tzo,
            'strings' => $tzo->strings
        ]);
        return $this->skin->renderPage('tzo.twig');
    }

    public function GET_tzo_part() {
        list($part) = $this->input('i:part');
        $tzo = new TZO();
        if (!$tzo->isPartValid($part))
            throw new NotFound();
        $title = $tzo->getPartWithDots($part).'_'.$tzo->strings['part_'.$part]['label'];
        $html = $tzo->getPartHtml($part);
        $this->skin->title = $title.' - '.lang('tzo');
        $this->skin->set([
            'tzo' => $tzo,
            'part' => $part,
            'title' => $title,
            'html' => $html,
            'nav_html' => $tzo->getPartNavigationTree($part)
        ]);
        return $this->skin->renderPage('tzo_part.twig');
    }
}