<?php

namespace app\ic;

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

class MainHandler
    extends BaseHandler
{
    public function GET_index() {
        $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://'.$_SERVER['HTTP_HOST'].'/images/simurgh.jpg';
        $this->skin->meta->setSocial('og:type', 'website');

        $this->skin->options->isIndex = true;
        $this->skin->set([
            'parts' => TZO::getParts()
        ]);

        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() {
        $this->skin->set([
            'parts' => TZO::getParts(),
            'appendix' => new TZOPart(appendix: true)
        ]);
        $this->skin->options->headSection = 'tbc';
        return $this->skin->renderPage('tzo.twig');
    }

    public function GET_tzo_part() {
        list($part) = $this->input('part');
        $tzo = new TZO();

        switch ($part) {
            case 'appendix':
                $is_appendix = true;
                $part = null;
                break;
            default:
                $is_appendix = false;
                break;
        }
        try {
            $tzo_part = new TZOPart($part, $is_appendix);
        } catch (\ValueError $e) {
            throw new NotFound();
        }

        $title = $tzo_part->getFullLabel();
        $html = $tzo_part->getHtml();

        $this->skin->meta->title = $title.' - '.lang('tzo');
        $this->skin->meta->description = $tzo_part->getDescriptionPreview(155);
        $this->skin->meta->url = $tzo_part->getUrl();
        $image = $tzo_part->getFirstImageUrl();
        if ($image)
            $this->skin->meta->image = $image;

        $this->skin->title = $title.' - '.lang('tzo');
        $this->skin->set([
            'title' => $title,
            'html' => $html,
            'nav_html' => $tzo_part->getNavigationTreeHtml()
        ]);
        return $this->skin->renderPage('tzo_part.twig');
    }
}