<?php

declare(strict_types=1);

namespace XBotControl;


class Config
{

    /**
     * @var Config|null
     */
    protected static $instance;
    public $db;
    public $smarty;

    public function __construct()
    {

        $this->smarty = new \Smarty\Smarty();
        $this->smarty->setTemplateDir(__DIR__ . '/../smarty/template/');
        $this->smarty->setConfigDir(__DIR__ . '/../smarty/config/');
        $this->smarty->setCompileDir(__DIR__ . '/../smarty/compile/');
        $this->smarty->setCacheDir(__DIR__ . '/../smarty/cache/');
        $this->smarty->setEscapeHtml(true);
        $this->smarty->assign([
            'baseURI' => $_ENV['BASEURI'],
        ]);
        $this->smarty->compile_check = 1;
/*         $this->db =  (new \Clue\React\SQLite\Factory())->openLazy($_ENV['APP_DIR'] . '/bots.db');
        'uaCache' => new React\Cache\ArrayCache(1000),
        'ipCache' => new React\Cache\ArrayCache(1000),
        'headerCache' => new React\Cache\ArrayCache(1000),
        'domainCache' => new React\Cache\ArrayCache(1000),
        'pathCache' => new React\Cache\ArrayCache(1000), */
    }

    /**
     * @return Config
     */
    public static function getInstance()
    {
        if (empty(self::$instance)) self::$instance = new self();
        return self::$instance;
    }

    public static function registerAssetRoutes(&$app)
    {
        // Define the directory to scan
        $assetsDir = realpath(__DIR__ . '/../public/assets');

        if ($assetsDir === false) {
            throw new \Exception('Assets directory not found');
        }

        // Create a recursive directory iterator to scan all files
        $iterator = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($assetsDir)
        );

        // Iterate through all files in the assets directory
        foreach ($iterator as $file) {
            if ($file->isFile()) {

                // Get the relative path of the file
                $relativePath = str_replace($assetsDir, '', $file->getRealPath());

                // Register the route
                $route = $_ENV['BASEURI'] . '/assets' . str_replace('\\', '/', $relativePath);

                $app->get($route, Controllers\StaticFilesController::class);
            }
        }
    }


}
