<?php declare(strict_types=1);

// Holds the global QR bill environment.
final class QrbEnvironment
{
    // name of default profile
    public  const DEFAULT_PROFILE        = "Default";

    // keys for JSON file
    private const KEY_MAX_RUNS_TO_KEEP   = "maxRunsToKeep";
    private const KEY_CURRENT_PROFILE    = "currentProfile";
    
    // QR bill payment slips do only support the following languages, DON'T CHANGE!
    private static $supportedLanguages = array("de", "fr", "it", "en");
    
    // maximum number of old runs to keep when cleaning up old runs
    private int    $maxRunsToKeep  = 10;
    // current profile
    private string $currentProfile = self::DEFAULT_PROFILE;

    // returns the languages supported by the QR bill payment slips
    public static function getSupportedLanguages() : array // of string
    {
        return self::$supportedLanguages;
    }
    
    // Creates an environemnt instance with the data provided by the given array.    
    public static function fromJson(array $environmentJson) : QrbEnvironment
    {
        $qrbEnv = new QrbEnvironment();
        $qrbEnv->maxRunsToKeep    = intval($environmentJson[self::KEY_MAX_RUNS_TO_KEEP]);
        $qrbEnv->currentProfile   = $environmentJson[self::KEY_CURRENT_PROFILE];
        return $qrbEnv;
    }
    
    public static function toJson(QrbEnvironment $qrbEnv) : array
    {
        $environmentJson = array();
        $environmentJson[self::KEY_MAX_RUNS_TO_KEEP] = $qrbEnv->maxRunsToKeep;
        $environmentJson[self::KEY_CURRENT_PROFILE]  = $qrbEnv->currentProfile;
        return $environmentJson;
    }
    
    // Constructor.
    public function __construct()
    {
    }
    
    // sets maximum number of old runs to keep when cleaning up old runs
    public function setMaxRunsToKeep(int $maxRunsToKeep) : void
    {
        $this->maxRunsToKeep = $maxRunsToKeep;
    }

    // returns maximum number of old runs to keep when cleaning up old runs
    public function getMaxRunsToKeep() : int
    {
        return $this->maxRunsToKeep;
    }

    // sets the name of the current profile
    public function setCurrentProfile(string $currentProfile)
    {
        $this->currentProfile = $currentProfile;
    }

    // returns the name of the current profile
    public function getCurrentProfile() : string
    {
        return $this->currentProfile;
    }

    public function __toString()
    {
        return "\n\tmaxRunsToKeep  = '{$this->maxRunsToKeep}'"
             . "\n\tcurrentProfile = '{$this->currentProfile}'";
    }
}

?>
