<?php declare(strict_types=1);

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

use PHPUnit\Framework\TestCase;

final class QrbMailDataTest extends TestCase
{

    public function testValidateHappyDayWithoutPdfWithAddLang() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;
        
        QrbMailData::validate($mailData, false, array("en"));
        
        // dummy test for testdox (risky)
        $this->assertTrue(array_key_exists(QrbMailData::KEY_MAIL_DATA, $mailData));
    }
    
    public function testValidateHappyDayWithPdfWithAddLang() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        QrbMailData::validate($mailData, true, array("en"));
        
        // dummy test for testdox (risky)
        $this->assertTrue(array_key_exists(QrbMailData::KEY_MAIL_DATA, $mailData));
    }
    
    public function testValidateHappyDayWithoutPdfWithoutAddLang() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        QrbMailData::validate($mailData, false, array());
        
        // dummy test for testdox (risky)
        $this->assertTrue(array_key_exists(QrbMailData::KEY_MAIL_DATA, $mailData));
    }
    
    public function testValidateHappyDayWithPdfWithoutAddLang() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        QrbMailData::validate($mailData, true, array());
        
        // dummy test for testdox (risky)
        $this->assertTrue(array_key_exists(QrbMailData::KEY_MAIL_DATA, $mailData));
    }
    
    public function testValidateMissingSurroundingKey() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        
        $this->expectException(QrbException::class);
        QrbMailData::validate($mailDataContents, true, array());
    }

    public function testValidateMissingFromAddress() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        $this->expectException(QrbException::class);
        QrbMailData::validate($mailData, true, array());
    }
    
    public function testValidateMissingFromName() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        $this->expectException(QrbException::class);
        QrbMailData::validate($mailData, true, array());
    }
    
    public function testValidateMissingSubject() : void
    {
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        $this->expectException(QrbException::class);
        QrbMailData::validate($mailData, true, array());
    }
    
    public function testValidateMissingAttachmentName() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        $this->expectException(QrbException::class);
        QrbMailData::validate($mailData, true, array());
    }
    
    public function testValidateInvalidSubjectAsMissingDefaultKey() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        $this->expectException(QrbException::class);
        QrbMailData::validate($mailData, true, array("en"));
    }
    
    public function testValidateInvalidAttachmentNameAsMissingDefaultKey() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;

        $this->expectException(QrbException::class);
        QrbMailData::validate($mailData, true, array("en"));
    }
    
    public function testGettersWithoutPdfWithAddLang() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;
        $mb = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $suppLang = array("de", "fr", "it", "en");
        $addLang  = array("en");
        $mailBody = QrbMultiLanguageText::fromJsonArray("Test", $mb, $suppLang, $addLang);

        $md = new QrbMailData($mailData, $mailBody, false, $suppLang, $addLang);

        $this->assertEquals("muster@example.com", $md->getFromAddress());
        $this->assertEquals("Hans Muster", $md->getFromName());
        $this->assertEquals("Deutsch", $md->getSubject("de"));
        $this->assertEquals("English", $md->getSubject("en"));
        $this->assertEquals("Deutsch", $md->getSubject("it"));

        // test localized data
        $missingKeys = array();
        $md->existsLocalizedData("en", $missingKeys);
        $this->assertTrue(empty($missingKeys));
        
        $missingKeys = array();
        $md->existsLocalizedData("fr", $missingKeys);
        $this->assertTrue(array_key_exists("MailBody_fr.html", $missingKeys));
        $this->assertTrue($missingKeys["MailBody_fr.html"]);

    }
    
    public function testGettersWithPdfWithAddLangAndToString() : void
    {
        $subject = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $attName = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $mailDataContents = array();
        $mailDataContents[QrbMailData::KEY_FROM_ADDRESS] = "muster@example.com";
        $mailDataContents[QrbMailData::KEY_FROM_NAME]    = "Hans Muster";
        $mailDataContents[QrbMailData::KEY_SUBJECT]      = $subject;
        $mailDataContents[QrbMailData::KEY_ATT_NAME]     = $attName;
        $mailData = array();
        $mailData[QrbMailData::KEY_MAIL_DATA] = $mailDataContents;
        $mb = array(
            [
                QrbMultiLanguageText::KEY_LANGUAGE => QrbMultiLanguageText::DEFAULT_KEY,
                QrbMultiLanguageText::KEY_TEXT     => "Deutsch"
            ],
            [
                QrbMultiLanguageText::KEY_LANGUAGE => "en",
                QrbMultiLanguageText::KEY_TEXT     => "English"
            ]
        );
        $suppLang = array("de", "fr", "it", "en");
        $addLang  = array("en");
        $mailBody = QrbMultiLanguageText::fromJsonArray("Test", $mb, $suppLang, $addLang);

        $md = new QrbMailData($mailData, $mailBody, true, $suppLang, $addLang);

        $this->assertEquals("muster@example.com", $md->getFromAddress());
        $this->assertEquals("Hans Muster", $md->getFromName());
        $this->assertEquals("Deutsch", $md->getSubject("de"));
        $this->assertEquals("English", $md->getSubject("en"));
        $this->assertEquals("Deutsch", $md->getSubject("it"));
        $this->assertEquals("Deutsch", $md->getAttachmentName("de"));
        $this->assertEquals("English", $md->getAttachmentName("en"));
        $this->assertEquals("Deutsch", $md->getAttachmentName("it"));

        // test localized data
        $missingKeys = array();
        $md->existsLocalizedData("en", $missingKeys);
        $this->assertTrue(empty($missingKeys));
        
        $missingKeys = array();
        $md->existsLocalizedData("fr", $missingKeys);
        $this->assertTrue(array_key_exists("MailBody_fr.html", $missingKeys));
        $this->assertTrue($missingKeys["MailBody_fr.html"]);

        echo $md . "\n";
    }
}

?>
