<?php

namespace engine;

use engine\skin\BaseSkin;

class GlobalContext
{
    public readonly string $project;
    public readonly http\RequestHandler $requestHandler;
    public readonly logging\Logger $logger;
    public readonly bool $isDevelopmentEnvironment;

    private array $setProperties = [];

    private static ?GlobalContext $instance = null;

    private function __construct() {}

    public static function getInstance(): static {
        if (is_null(self::$instance))
            self::$instance = new static();
        return self::$instance;
    }

    public function getSkin(): BaseSkin {
        return $this->requestHandler->skin;
    }

    public function getStrings(): lang\Strings {
        return $this->requestHandler->skin->strings;
    }

    public function setProject(string $project) {
        $this->setProperty('project', $project);
    }

    public function setRequestHandler(http\RequestHandler $requestHandler) {
        $this->setProperty('requestHandler', $requestHandler);
    }

    public function setLogger(logging\Logger $logger) {
        $this->setProperty('logger', $logger);
    }

    public function setIsDevelopmentEnvironment(bool $isDevelopmentEnvironment) {
        $this->setProperty('isDevelopmentEnvironment', $isDevelopmentEnvironment);
    }

    private function setProperty(string $name, mixed $value) {
        if (isset($this->setProperties[$name]))
            throw new \RuntimeException("$name can only be set once");
        $this->$name = $value;
        $this->setProperties[$name] = true;
    }
}