<?php declare(strict_types=1);

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

use PHPUnit\Framework\TestCase;

final class QrbMailProcessorTest extends TestCase
{

    public function testReplacePlaceHoldersChf() : void
    {
        $originalMailBody = QrbMailProcessor::PLACE_HOLDER_GREETING . " - " . QrbMailProcessor::PLACE_HOLDER_AMOUNT;
        $recipient = $this->createRecipient("Sehr geehrter Herr Muster", 100.25);
        
        $resultingMailBody = QrbMailProcessor::replacePlaceHolders($originalMailBody, $recipient, "CHF");
        
        $this->assertEquals("Sehr geehrter Herr Muster - CHF 100.25", $resultingMailBody);
    }
    
    public function testReplacePlaceHoldersEur() : void
    {
        $originalMailBody = 
            QrbMailProcessor::PLACE_HOLDER_GREETING 
            . "<br /> Du hast sagenhafte " 
            . QrbMailProcessor::PLACE_HOLDER_AMOUNT
            . " gespendet!";
        $expectedMailBody = 
            "Hallo Eugen"
            . "<br /> Du hast sagenhafte " 
            . "EUR 100"
            . " gespendet!";
        $recipient = $this->createRecipient("Hallo Eugen", 100);
        
        $resultingMailBody = QrbMailProcessor::replacePlaceHolders($originalMailBody, $recipient, "EUR");
        
        $this->assertEquals($expectedMailBody, $resultingMailBody);
    }
    
    public function testReplacePlaceHolderNoGreetingAmountEur() : void
    {
        $originalMailBody = 
            QrbMailProcessor::PLACE_HOLDER_GREETING 
            . "<br /> Du hast sagenhafte " 
            . QrbMailProcessor::PLACE_HOLDER_AMOUNT
            . " gespendet!";
        $expectedMailBody = 
            "<br /> Du hast sagenhafte " 
            . "EUR 100"
            . " gespendet!";
        $recipient = $this->createRecipient("", 100);
        
        $resultingMailBody = QrbMailProcessor::replacePlaceHolders($originalMailBody, $recipient, "EUR");
        
        $this->assertEquals($expectedMailBody, $resultingMailBody);
        
        $originalMailBody = 
            "Du hast sagenhafte " 
            . QrbMailProcessor::PLACE_HOLDER_AMOUNT
            . " gespendet!";
        $expectedMailBody = 
            "Du hast sagenhafte " 
            . "EUR 100"
            . " gespendet!";
        
        $resultingMailBody = QrbMailProcessor::replacePlaceHolders($originalMailBody, $recipient, "EUR");
        
        $this->assertEquals($expectedMailBody, $resultingMailBody);
    }
    
    public function testReplacePlaceHolderGreetingNoAmount() : void
    {
        $originalMailBody = 
            QrbMailProcessor::PLACE_HOLDER_GREETING 
            . "<br /> Du hast sagenhafte " 
            . QrbMailProcessor::PLACE_HOLDER_AMOUNT
            . " gespendet!";
        $expectedMailBody = 
            "Hallo Eugen"
            . "<br /> Du hast sagenhafte " 
            . QrbMailProcessor::PLACE_HOLDER_AMOUNT
            . " gespendet!";
        $recipient = $this->createRecipient("Hallo Eugen", NULL);
        
        $resultingMailBody = QrbMailProcessor::replacePlaceHolders($originalMailBody, $recipient, "EUR");
        
        $this->assertEquals($expectedMailBody, $resultingMailBody);

        $originalMailBody = 
            QrbMailProcessor::PLACE_HOLDER_GREETING 
            . "<br />";
        $expectedMailBody = 
            "Hallo Eugen"
            . "<br />";
        
        $resultingMailBody = QrbMailProcessor::replacePlaceHolders($originalMailBody, $recipient, "EUR");
        
        $this->assertEquals($expectedMailBody, $resultingMailBody);
    }
    
    private function createRecipient(string $greeting, ?float $amount) : QrbRecipient
    {
        $amountMandatory = ($amount != NULL);
        
        $recipient = array(
            $greeting,
            "Muster GmbH",
            "Herr",
            "Hans",
            "Muster",
            "Musterstrasse",
            "99",
            "9999",
            "Musterstadt",
            "CH",
            "de",
            $amountMandatory ? $amount : 0
        );
        $headerNames = array(
            QrbRecipient::HDR_GREETING,
            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_LANGUAGE,    // mandatory
            QrbRecipient::HDR_AMOUNT
        );
        
        $hdr = QrbRecipient::parseHeader($headerNames, $amountMandatory, true, false, false);
    
        $r = new QrbRecipient(1, $recipient, $hdr, $amountMandatory, true, false, false, array("de", "fr", "it", "en"));
        
        $this->assertEquals(1, $r->getIndex());
        $this->assertEquals($greeting, $r->getGreeting());
        $this->assertEquals($amount, $r->getAmount());
        return $r;
    }
}

?>
