<?php

/**
 * This file contains QUI\Tabler\EventHandler
 */

namespace QUI\Tabler;

use QUI;
use Smarty;

/**
 * Class EventHandler
 *
 * @package QUI\Tabler
 */
class EventHandler
{
    /**
     * Event : on request
     *
     * @param QUI\Rewrite $Rewrite
     * @param string $url
     * @return void
     */
    public static function onRequest(QUI\Rewrite $Rewrite, string $url): void
    {
        if (empty($url)) {
            return;
        }

        if (strpos($url, 'assets/') === 0) {
            self::onAssetRequest($url);

            return;
        }

        $requestDir = 'packages/quiqqer/template-tabler/bin/';

        if (strpos($url, $requestDir) !== 0) {
            return;
        }

        $assetsDir = OPT_DIR . 'tabler/tabler/dist/assets/';

        $requestAsset = str_replace($requestDir, '', $url);
        $requestAsset = $assetsDir . $requestAsset;

        if (file_exists($requestAsset)) {
            echo file_get_contents($requestAsset);
            exit;
        }

        QUI\System\Log::addNotice($requestAsset);
    }

    /**
     * Request for assets/ url
     *
     * @param string $url
     * @return void
     */
    public static function onAssetRequest(string $url): void
    {
        $assetsDir    = OPT_DIR . 'tabler/tabler/dist/';
        $requestAsset = $assetsDir . $url;

        if (file_exists($requestAsset)) {
            echo file_get_contents($requestAsset);
            exit;
        }
    }

    /**
     * Event : on smarty init
     *
     * @param Smarty $Smarty
     * @return void
     */
    public static function onSmartyInit(Smarty $Smarty): void
    {
        if (
            !isset($Smarty->registered_plugins['function']) ||
            !isset($Smarty->registered_plugins['function']['fetch'])
        ) {
            $Smarty->registerPlugin(
                "function",
                "fetch",
                "\\QUI\\Tabler\\EventHandler::fetch"
            );
        }
    }

    /**
     * Helper to fetch templates
     *
     * @param array<string, mixed> $params
     * @param Smarty $Smarty
     * @return string
     */
    public static function fetch(array $params, Smarty $Smarty): string
    {
        if (empty($params['template']) || !is_string($params['template'])) {
            return '';
        }

        $template = $params['template'];
        $path = OPT_DIR . 'quiqqer/template-tabler/';

        try {
            $Engine = QUI::getTemplateManager()->getEngine();
        } catch (QUI\Exception) {
            return '';
        }

        $Engine->assign($params);

        return $Engine->fetch($path . $template);
    }
}
