<?php declare(strict_types=1);

require_once __DIR__ . '/../common/QrbException.php';
require_once __DIR__ . '/QrbMultiLanguageText.php';

// Holds the QR bill additional information.
final class QrbAdditionalInformation
{
    public const KEY_ADD_INFO = "AdditionalInformation";
    
    // key constants (public for unit test)
    public const KEY_ADD_INFO_TEXT = "additionalInformation";
    
    private QrbMultiLanguageText $additionalInformation;

    // Validates additional information from the given associative array.
    // The surrounding key "AdditionalInformation" can contain the following fields:
    // "additionalInformation": checked for valid multi language 
    //                          (default key and keys in additionalLanguages must exist)
    public static function validate(array $additionalInformation, array $additionalLanguages) : void
    {
        if (! array_key_exists(self::KEY_ADD_INFO, $additionalInformation))
            throw new QrbException(
                static::class . " - surrounding key 'AdditionalInformation' of additional information does not exist",
                "exception.additionalInformation.surroundingKeyDoesNotExist");
        $additionalInformationContents = $additionalInformation[self::KEY_ADD_INFO];

        QrbMultiLanguageText::validate(self::KEY_ADD_INFO_TEXT, 
                                          $additionalInformationContents[self::KEY_ADD_INFO_TEXT], 
                                          $additionalLanguages);
    }
    
    // Creates an additional information instance from the given associative array.
    // The surrounding key "AdditionalInformation" can contain the following fields:
    // "additionalInformation": checked for valid multi language 
    //                          (default key and keys in additionalLanguages must exist)
    public function __construct(array $additionalInformation, 
                                array $supportedLanguages,
                                array $additionalLanguages)
    {
        self::validate($additionalInformation, $additionalLanguages);
        
        $additionalInformationContents = $additionalInformation[self::KEY_ADD_INFO];
        $this->additionalInformation = 
            QrbMultiLanguageText::fromJsonArray(self::KEY_ADD_INFO_TEXT,
                                                   $additionalInformationContents[self::KEY_ADD_INFO_TEXT], 
                                                   $supportedLanguages,
                                                   $additionalLanguages);
    }

    // Checks whether localized data for the given language exists.
    // If some data is missing then the given missingKeys is updated.
    public function existsLocalizedData(string $language, array &$missingKeys) : void
    {
        if (! $this->existsLocalizedAdditionalInformation($language))
            $missingKeys[self::KEY_ADD_INFO . "json"][self::KEY_ADD_INFO_TEXT] = true;
    }
    
    // checks whether the additional information supports the given language
    private function existsLocalizedAdditionalInformation(string $language) : bool
    {
        return $this->additionalInformation->existsLocalizedText($language);
    }
    
    // Returns the additional information in the given language.
    public function getAdditionalInformation(string $language) : string
    {
        return $this->additionalInformation->getText($language);
    }
    
    public function __toString()
    {
        return  "\n\tadditionalInformation: {$this->additionalInformation}";
    }
}

?>
