<?php declare(strict_types=1);

require_once __DIR__ . '/../../src/common/QrbFiles.php';

use PHPUnit\Framework\TestCase;

final class QrbFilesTest extends TestCase
{
    const TEST_FILE_OUT_DIR = "../";
    
    public function testFixPath() : void
    {
        // TODO some results are strange => rework!
        $this->assertEquals("C:/path/file",      QrbFiles::fixPath("C:/path/file"));
        $this->assertEquals("//somePath",        QrbFiles::fixPath("./somePath"));
        $this->assertEquals("qr-bills-output",   QrbFiles::fixPath("..\/qr-bills-output"));
        $this->assertEquals("//qr-bills-output", QrbFiles::fixPath("somePath/../qr-bills-output"));
        $this->assertEquals("//qr-bills-output", QrbFiles::fixPath("/somePath/../qr-bills-output"));
    }
    
    public function testReadMultiLanguageText() : void
    {
        $fileNameDefault = self::TEST_FILE_OUT_DIR . "QrBillsFilesTestFile.txt";
        $fileNameEn      = self::TEST_FILE_OUT_DIR . "QrBillsFilesTestFile_en.txt";
        $textDefault = "Default";
        $textEn      = "English";
        $supportedLanguages = [ "de", "fr", "it", "en"];
        $additionalLanguages = [ "en"];
        
        QrbFiles::writeTextFile($fileNameDefault, $textDefault);
        QrbFiles::writeTextFile($fileNameEn, $textEn);
        
        $mlText = QrbFiles::readMultiLanguageText($fileNameDefault, $supportedLanguages, $additionalLanguages);

        $this->assertInstanceOf(QrbMultiLanguageText::class, $mlText);
        $this->assertEquals($textDefault, $mlText->getText("it"));    // unknown langauge should return default text
        $this->assertEquals($textDefault, $mlText->getText("ru"));    // unknown langauge should return default text
        $this->assertEquals($textEn,      $mlText->getText("en"));

        unlink($fileNameDefault);
        unlink($fileNameEn);
    }
    
    public function testReadMultiLanguageTextThrows() : void
    {
        $fileNameDefault = self::TEST_FILE_OUT_DIR . "QrBillsFilesTestFile.txt";
        $textDefault = "Default";
        $supportedLanguages = [ "de", "fr", "it", "en"];
        $additionalLanguages = [ "en"];
        
        QrbFiles::writeTextFile($fileNameDefault, $textDefault);
        
        try
        {
            $this->expectException(QrbException::class);
            $mlText = QrbFiles::readMultiLanguageText($fileNameDefault, $supportedLanguages, $additionalLanguages);
        }
        finally
        {
            unlink($fileNameDefault);
        }
    }
    
    public function testWriteReadJsonFile() : void
    {
        $fileName = self::TEST_FILE_OUT_DIR . "QrBillsFilesTestFile.json";
        $jsonTestData = [
            "item1" => "data1",
            "item2" => "data2"
        ];
        
        QrbFiles::writeJsonFile($fileName, $jsonTestData);

        $jsonReadData = QrbFiles::readJsonFile($fileName);
        
        $this->assertEquals($jsonTestData["item1"], $jsonReadData["item1"]);
        $this->assertEquals($jsonTestData["item2"], $jsonReadData["item2"]);
        
        unlink($fileName);
    }
    
    public function testReadCsvFile() : void
    {
        $fileName = self::TEST_FILE_OUT_DIR . "QrBillsFilesTestFile.csv";
        $csvTestData = "Name,City\nSaba,Zürich";
        
        QrbFiles::writeTextFile($fileName, $csvTestData);
    
        $csvReadData = QrbFiles::readCsvFile($fileName);

        $this->assertEquals("Name",   $csvReadData[0][0]);
        $this->assertEquals("City",   $csvReadData[0][1]);
        $this->assertEquals("Saba",   $csvReadData[1][0]);
        $this->assertEquals("Zürich", $csvReadData[1][1]);
        
        unlink($fileName);
    }
}

?>
