<?php
namespace Misuzu;

use Index\ByteFormat;
use Index\DateTime;
use Index\Environment;
use Misuzu\MisuzuContext;
use Misuzu\Comments\CommentsParser;
use Misuzu\Parsers\Parser;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\Environment as TwigEnvironment;

final class TwigMisuzu extends AbstractExtension {
    private MisuzuContext $ctx;

    public function __construct(MisuzuContext $ctx) {
        $this->ctx = $ctx;
    }

    public function getFilters() {
        return [
            new TwigFilter('html_colour', 'html_colour'),
            new TwigFilter('country_name', 'get_country_name'),
            new TwigFilter('parse_text', fn(string $text, int $parser): string => Parser::instance($parser)->parseText($text)),
            new TwigFilter('parse_comment', fn(string $text): string => CommentsParser::parseForDisplay($text)),
            new TwigFilter('perms_check', 'perms_check'),
            new TwigFilter('time_format', [$this, 'timeFormat']),
        ];
    }

    public function getFunctions() {
        return [
            new TwigFunction('url_construct', 'url_construct'),
            new TwigFunction('url', 'url'),
            new TwigFunction('forum_may_have_children', 'forum_may_have_children'),
            new TwigFunction('forum_may_have_topics', 'forum_may_have_topics'),
            new TwigFunction('csrf_token', fn() => CSRF::token()),
            new TwigFunction('git_commit_hash', fn(bool $long = false) => GitInfo::hash($long)),
            new TwigFunction('git_tag', fn() => GitInfo::tag()),
            new TwigFunction('git_branch', fn() => GitInfo::branch()),
            new TwigFunction('startup_time', fn(float $time = MSZ_STARTUP) => microtime(true) - $time),
            new TwigFunction('sql_query_count', fn() => $this->sqlQueryCount()),
            new TwigFunction('ndx_version', fn() => Environment::getIndexVersion()),
            new TwigFunction('byte_symbol', fn($bytes, $decimal = ByteFormat::DECIMAL_DEFAULT) => ByteFormat::format($bytes, $decimal)),
        ];
    }

    public function sqlQueryCount(): array {
        $ndx = $this->ctx->getDbQueryCount();
        $pdo = DB::queries();
        $total = $ndx + $pdo;
        return compact('ndx', 'pdo', 'total');
    }

    public function timeFormat(DateTime|string|int $dateTime): string {
        if(is_string($dateTime))
            $dateTime = new DateTime($dateTime);
        elseif(is_int($dateTime))
            $dateTime = DateTime::fromUnixTimeSeconds($dateTime);

        $string = '';
        $now = DateTime::now();

        $isDiffYear = $now->getYear() !== $dateTime->getYear();
        if($isDiffYear || $now->getMonth() !== $dateTime->getMonth() || $now->getDay() !== $dateTime->getDay()) {
            $string .= $dateTime->format('M jS');
            if($isDiffYear)
                $string .= $dateTime->format(' Y');
            $string .= ', ';
        }

        $string .= $dateTime->format('G:i ');
        $string .= $dateTime->isUTC() ? 'UTC' : $dateTime->format('T');

        return $string;
    }
}
