<?php declare(strict_types=1);

require __DIR__ . '/../../src/process/QrbPdfProcessor.php';

use PHPUnit\Framework\TestCase;

final class QrbPdfProcessorTest extends TestCase
{
    const DEFAULT_LANGUAGE_EN = "en";
    const LANGUAGE_DE = "de";

    private QrbAdditionalInformation $qrbAddInfo; 

    protected function setUp() : void
    {
        $addInfoText = new QrbMultiLanguageText("TestAddInfo");
        $addInfoTextJson = QrbMultiLanguageText::toJsonArray($addInfoText, QrbAdditionalInformation::KEY_ADD_INFO_TEXT);
        $jsonArray = array();
        $jsonArray[QrbAdditionalInformation::KEY_ADD_INFO] = $addInfoTextJson;
        $this->qrbAddInfo = new QrbAdditionalInformation($jsonArray, 
                                                         QrbEnvironment::getSupportedLanguages(), 
                                                         array());
    }
    
    public function testBuildAdditionalInformationWithoutAdditionalInformationWithoutAddRef() : void
    {
        $this->assertNull(QrbPdfProcessor::buildAdditionalInformation(NULL, 
                                                                      false, 
                                                                      $this->createRecipient(), 
                                                                      self::DEFAULT_LANGUAGE_EN));
    }
    
    public function testBuildAdditionalInformationWithoutAdditionalInformationWithAddRef() : void
    {
        $this->assertEquals("123456", 
                            QrbPdfProcessor::buildAdditionalInformation(NULL, 
                                                                        true, 
                                                                        $this->createRecipient(), 
                                                                        self::DEFAULT_LANGUAGE_EN));
    }
    
    public function testBuildAdditionalInformationWithAdditionalInformationWithoutAddRef() : void
    {
        $this->assertEquals("TestAddInfo", 
                            QrbPdfProcessor::buildAdditionalInformation($this->qrbAddInfo, 
                                                                        false, 
                                                                        $this->createRecipient(), 
                                                                        self::DEFAULT_LANGUAGE_EN));
    }
    
    public function testBuildAdditionalInformationWithAdditionalInformationWithAddRef() : void
    {
        $this->assertEquals("TestAddInfo\n123456", 
                            QrbPdfProcessor::buildAdditionalInformation($this->qrbAddInfo, 
                                                                        true, 
                                                                        $this->createRecipient(), 
                                                                        self::DEFAULT_LANGUAGE_EN));
    }
    
    public function testBuildAmountNoAmount() : void
    {
        $this->assertNull(QrbPdfProcessor::buildAmount(QrbProfileConfiguration::NO_AMOUNT, 
                                                       0.0, 
                                                       $this->createRecipient()));
    }
    
    public function testBuildAmountGlobalAmount() : void
    {
        $this->assertEquals(100.00, 
                            QrbPdfProcessor::buildAmount(QrbProfileConfiguration::GLOBAL_AMOUNT, 
                                                        100.00,
                                                        $this->createRecipient()));
    }
    
    public function testBuildAmountIndividualAmount() : void
    {
        $this->assertEquals(2222.22, 
                            QrbPdfProcessor::buildAmount(QrbProfileConfiguration::INDIVIDUAL_AMOUNT, 
                                                   100.00, 
                                                   $this->createRecipient()));
    }
    
    public function testBuildNameForDebtorNoCompany() : void
    {
        $this->assertEquals("Hans Muster", 
                            QrbPdfProcessor::buildNameForDebtor($this->createRecipient()));
    }
    
    public function testBuildNameForDebtorWithCompany() : void
    {
        $this->assertEquals("Muster AG", 
                            QrbPdfProcessor::buildNameForDebtor($this->createRecipientWithCompany()));
    }
    
    public function testCreatePdfOutputName() : void
    {
        $this->assertEquals("qr-bill-1-Muster-Hans", 
                            QrbPdfProcessor::createPdfOutputName($this->createRecipient()));
    }
    
    private function createRecipient() : QrbRecipient
    {
        $recipientFile = array(
            array(QrbRecipient::HDR_LANGUAGE, 
                  QrbRecipient::HDR_COMPANY, 
                  QrbRecipient::HDR_SALUTATION, 
                  QrbRecipient::HDR_FIRST_NAME, 
                  QrbRecipient::HDR_LAST_NAME,
                  QrbRecipient::HDR_STREET, 
                  QrbRecipient::HDR_BUILDING_NO,
                  QrbRecipient::HDR_POSTAL_CODE, 
                  QrbRecipient::HDR_CITY,
                  QrbRecipient::HDR_COUNTRY, 
                  QrbRecipient::HDR_REFERENCE,
                  QrbRecipient::HDR_AMOUNT),
            array(self::LANGUAGE_DE, "", "Herr", "Hans", "Muster", "", "", "", "", "", "123456", "2222.22")
        );
        $headerNames = QrbRecipient::parseHeader($recipientFile[0], true, true, true, false);
        return new QrbRecipient(1, $recipientFile[1], $headerNames, true, true, true, false, array("de", "fr", "it", "en"));
    }
    
    private function createRecipientWithCompany() : QrbRecipient
    {
        $recipientFile = array(
            array(QrbRecipient::HDR_LANGUAGE, 
                  QrbRecipient::HDR_COMPANY, QrbRecipient::HDR_SALUTATION, 
                  QrbRecipient::HDR_FIRST_NAME, QrbRecipient::HDR_LAST_NAME,
                  QrbRecipient::HDR_STREET, QrbRecipient::HDR_BUILDING_NO,
                  QrbRecipient::HDR_POSTAL_CODE, QrbRecipient::HDR_CITY,
                  QrbRecipient::HDR_COUNTRY, QrbRecipient::HDR_AMOUNT),
            array(self::LANGUAGE_DE, "Muster AG", "", "", "", "", "", "", "", "", "2222.22")
        );
        $headerNames = QrbRecipient::parseHeader($recipientFile[0], true, true, false, false);
        return new QrbRecipient(1, $recipientFile[1], $headerNames, true, true, false, false, array("de", "fr", "it", "en"));
    }
}

?>
