<?php declare(strict_types=1);

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

use PHPUnit\Framework\TestCase;

final class QrbPdfDataTest extends TestCase
{
    public function testValidateHappyDay() : void
    {
        $textAttribute = array(
            QrbPdfTextAttribute::KEY_TEXT_COLOR => 0,
            QrbPdfTextAttribute::KEY_LINE_HEIGHT => 6,
            QrbPdfTextAttribute::KEY_FONT_FAMILY => "FontFamily",
            QrbPdfTextAttribute::KEY_FONT_STYLE => "",
            QrbPdfTextAttribute::KEY_FONT_SIZE => 12
        );
        $pos = array(
            QrbPdfPosition::KEY_POS_X => 0,
            QrbPdfPosition::KEY_POS_Y => 0
        );
        $mlText = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
    
        $entry[QrbPdfDataEntry::KEY_NAME]           = "default";
        $entry[QrbPdfDataEntry::KEY_TEXT_ATTRIBUTE] = $textAttribute;
        $entry[QrbPdfDataEntry::KEY_POSITION]       = $pos;
        $entry[QrbPdfDataEntry::KEY_TEXT]           = $mlText;
        
        $pdfData[QrbPdfData::KEY_PDF_DATA]["test"] = $entry;
        QrbPdfData::validate($pdfData, array("en"));
        
        // dummy test for testdox (risky)
        $this->assertTrue(array_key_exists("test", $pdfData[QrbPdfData::KEY_PDF_DATA]));
    }
    
    public function testConstructHappyDayAndToString() : void
    {
        $pdfTemplate = new QrbMultiLanguageText("1.");
        $pdfTemplate->addText("en", "2.");
    
        $pos = array(
            QrbPdfPosition::KEY_POS_X => 0,
            QrbPdfPosition::KEY_POS_Y => 0
        );
        $textAttribute = array(
            QrbPdfTextAttribute::KEY_TEXT_COLOR => 0,
            QrbPdfTextAttribute::KEY_LINE_HEIGHT => 6,
            QrbPdfTextAttribute::KEY_FONT_FAMILY => "FontFamily",
            QrbPdfTextAttribute::KEY_FONT_STYLE => "",
            QrbPdfTextAttribute::KEY_FONT_SIZE => 12
        );
        $mlText = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
    
        $entry[QrbPdfDataEntry::KEY_NAME]           = "default";
        $entry[QrbPdfDataEntry::KEY_POSITION]       = $pos;
        $entry[QrbPdfDataEntry::KEY_TEXT_ATTRIBUTE] = $textAttribute;
        $entry[QrbPdfDataEntry::KEY_TEXT]           = $mlText;
        
        $pdfData[QrbPdfData::KEY_PDF_DATA]["test"] = $entry;

        $pd = new QrbPdfData($pdfTemplate, $pdfData, array("de","fr","it","en"), array("en"));
        
        $this->assertEquals("2.", $pd->getTemplate("en"));
        $this->assertNotNull($pd->getPosition("default", "en"));
        $this->assertEquals(0,            $pd->getPosition("default", "en")->getPosX());
        $this->assertEquals("FontFamily", $pd->getTextAttribute("default", "en")->getFontFamily());
        $this->assertEquals("English",    $pd->getText("default", "en"));
        
        echo $pd . "\n";
    }    
    
    public function testExistsLocalizedData() : void
    {
        $pdfTemplate = new QrbMultiLanguageText("1.");
        $pdfTemplate->addText("en", "2.");
    
        $textAttribute = array(
            QrbPdfTextAttribute::KEY_TEXT_COLOR => 0,
            QrbPdfTextAttribute::KEY_LINE_HEIGHT => 6,
            QrbPdfTextAttribute::KEY_FONT_FAMILY => "FontFamily",
            QrbPdfTextAttribute::KEY_FONT_STYLE => "",
            QrbPdfTextAttribute::KEY_FONT_SIZE => 12
        );
        $pos = array(
            QrbPdfPosition::KEY_POS_X => 0,
            QrbPdfPosition::KEY_POS_Y => 0
        );
        $mlText = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
    
        $entry[QrbPdfDataEntry::KEY_NAME]           = "default";
        $entry[QrbPdfDataEntry::KEY_TEXT_ATTRIBUTE] = $textAttribute;
        $entry[QrbPdfDataEntry::KEY_POSITION]       = $pos;
        $entry[QrbPdfDataEntry::KEY_TEXT]           = $mlText;
        
        $pdfData[QrbPdfData::KEY_PDF_DATA]["test"] = $entry;

        $pd = new QrbPdfData($pdfTemplate, $pdfData, array("de","fr","it","en"), array("en"));
        
        $missingKeys = array();
        $pd->existsLocalizedData("en", $missingKeys);
        $this->assertTrue(empty($missingKeys));
        
        $missingKeys = array();
        $pd->existsLocalizedData("fr", $missingKeys);
        $this->assertTrue(array_key_exists("PdfTemplate_fr.pdf", $missingKeys));
        $this->assertTrue($missingKeys["PdfTemplate_fr.pdf"]);
    }
}

?>
