<?php declare(strict_types=1);

require_once __DIR__ . '/../../src/data/QrbMultiLanguageText.php';

use PHPUnit\Framework\TestCase;

final class QrbMultiLanguageTextTest extends TestCase
{

    public function testValidateHappyDayWithoutAdditionalLanguages() : void
    {
        $jsonArray = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $addLang  = array();
            
        QrbMultiLanguageText::validate("Test", $jsonArray, $addLang);
        
        // dummy test for testdox (risky)
        $this->assertEquals(QrbMultiLanguageText::DEFAULT_KEY, $jsonArray[0][QrbMultiLanguageText::KEY_LANGUAGE]);
    }
    
    public function testValidateHappyDayWithAdditionalLanguages() : void
    {
        $jsonArray = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $addLang = array("en");
            
        QrbMultiLanguageText::validate("Test", $jsonArray, $addLang);
        
        // dummy test for testdox (risky)
        $this->assertEquals("Deutsch", $jsonArray[0][QrbMultiLanguageText::KEY_TEXT]);
    }
    
    public function testValidateMissingLanguageKey() : void
    {
        $jsonArray = array(
            [
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $addLang = array();
            
        $this->expectException(QrbException::class);
        QrbMultiLanguageText::validate("Test", $jsonArray, $addLang);
    }
    
    public function testValidateMissingTextKey() : void
    {
        $jsonArray = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
            ]
        );
        $addLang = array();
            
        $this->expectException(QrbException::class);
        QrbMultiLanguageText::validate("Test", $jsonArray, $addLang);
    }
    
    public function testValidateMissingDefaultKey() : void
    {
        $jsonArray = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $addLang = array();
            
        $this->expectException(QrbException::class);
        QrbMultiLanguageText::validate("Test", $jsonArray, $addLang);
    }
    
    public function testValidateMissingAdditionalLanguage() : void
    {
        $jsonArray = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $addLang = array("en");
            
        $this->expectException(QrbException::class);
        QrbMultiLanguageText::validate("Test", $jsonArray, $addLang);
    }
    
    public function testFromJsonArrayHappyDay() : void
    {
        $jsonArray = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $suppLang = array("fr", "it", "en");
        $addLang  = array("en");
        
        $mlText = QrbMultiLanguageText::fromJsonArray("Test", $jsonArray, $suppLang, $addLang);
        
        $this->assertEquals("Deutsch", $mlText->getText("de"));
        $this->assertEquals("Deutsch", $mlText->getText("fr"));
        $this->assertEquals("Deutsch", $mlText->getText("it"));
        $this->assertEquals("English", $mlText->getText("en"));
        $this->assertEquals("Deutsch", $mlText->getText("ru"));
    }
    
    public function testFromJsonArrayHappyDayWithUnknownLanguage() : void
    {
        $jsonArray = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "es",
                QrbMultiLanguageText::KEY_TEXT     => "Espagnol"
            ]
        );
        $suppLang = array("fr", "it", "en");
        $addLang  = array("en");
        
        $mlText = QrbMultiLanguageText::fromJsonArray("Test", $jsonArray, $suppLang, $addLang);
        
        $this->assertEquals("Deutsch", $mlText->getText("de"));
        $this->assertEquals("Deutsch", $mlText->getText("fr"));
        $this->assertEquals("Deutsch", $mlText->getText("it"));
        $this->assertEquals("English", $mlText->getText("en"));
        $this->assertEquals("Deutsch", $mlText->getText("ru"));
    }
    
    public function testToJsonArray() : void
    {
        $mlText = new QrbMultiLanguageText("Deutsch");
        $mlText->addText("en", "English");
        $jsonArray = QrbMultiLanguageText::toJsonArray($mlText, "Container");

        $foundLanguages = 0;
        
        foreach ($jsonArray as $key => $value)
        {
            $this->assertEquals("Container", $key);
            
            foreach ($value as $value2)
            {
                $expectedText = "";
                foreach ($value2 as $key3 => $value3)
                {
                    switch ($key3)
                    {
                    case "language":
                        switch ($value3)
                        {
                        case QrbMultiLanguageText::DEFAULT_KEY:
                            $expectedText = "Deutsch";
                            break;
                        case "en": 
                            $expectedText = "English";
                            break;
                        }
                        
                        $foundLanguages++;
                        break;
                    
                    case "text":
                        $this->assertEquals($expectedText, $value3);
                        break;
                    }
                }
            }
        }
        
        $this->assertEquals(2, $foundLanguages);
    }
    
    public function testConstructAndExistsLocalizedTextAndToString() : void
    {
        $mlText = new QrbMultiLanguageText("Deutsch");
        $mlText->addText("en", "English");
        
        $this->assertFalse($mlText->existsLocalizedText("de"));
        $this->assertTrue($mlText->existsLocalizedText("en"));
        
        $this->assertEquals("Deutsch", $mlText->getText("de"));
        $this->assertEquals("Deutsch", $mlText->getText("fr"));
        $this->assertEquals("Deutsch", $mlText->getText("it"));
        $this->assertEquals("English", $mlText->getText("en"));
        $this->assertEquals("Deutsch", $mlText->getText("ru"));
        
        echo $mlText . "\n";
    }
}

?>
