<?php declare(strict_types=1);

require_once __DIR__ . '/QrbProcessStatus.php';

require_once __DIR__ . '/../common/QrbTranslator.php';
require_once __DIR__ . '/../common/QrbDataType.php';

require_once __DIR__ . '/../storage/QrbStorage.php';

require_once __DIR__ . '/../data/QrbProfileConfiguration.php';
require_once __DIR__ . '/../data/QrbPdfData.php';
require_once __DIR__ . '/../data/QrbMailData.php';
require_once __DIR__ . '/../data/QrbAdditionalInformation.php';
require_once __DIR__ . '/../data/QrbRecipient.php';

final class QrbLanguageChecker
{
    private QrbStorage              $qrbStorage;
    private QrbProfileConfiguration $qrbCfg;
    
    // Creates a new instance of the data container.
    //
    // qrbStorage: storage interface
    // qrbCfg:     profile configuration
    public function __construct(QrbStorage $qrbStorage, QrbProfileConfiguration $qrbCfg)
    {
        $this->qrbStorage = $qrbStorage;
        $this->qrbCfg     = $qrbCfg;
    }

    // Checks if any recipient has a language that is not present in any input file.
    //
    // status: status writer
    // qrxl:   translator
    public function checkLanguage(QrbProcessStatus $status, QrbTranslator $qrxl) : void
    {
        $amountMandatory    = $this->qrbCfg->getPrintAmount() == QrbProfileConfiguration::INDIVIDUAL_AMOUNT;
        $createPdf          = $this->qrbCfg->getCreatePdf();
        $referenceMandatory = $this->qrbCfg->getPrintPaymentSlip();    // in fact not true if no QR IBAN ...
        $sendMail           = $this->qrbCfg->getSendMail();
        
        $pdfData            = $createPdf ? $this->qrbStorage->readPdfData($this->qrbCfg->getAdditionalLanguages()) 
                                         : null;
        $mailData           = $sendMail  ? $this->qrbStorage->readMailData($this->qrbCfg->getAdditionalLanguages(), $createPdf) 
                                         : null;
        // additional info is anyway optional
        $additionalInfo     = $this->qrbStorage->readAddInfo($this->qrbCfg->getAdditionalLanguages());

        $recipients = 
            $this->qrbStorage->readRecipients(QrbDataType::RECIPIENTS, 
                                              $amountMandatory, 
                                              $createPdf, 
                                              $referenceMandatory, 
                                              $sendMail);

        $this->qrbStorage->writeLog("Checking language of " . count($recipients) . " recipients");
        $msg1 = $qrxl->xl("execution.languageCheckStart1");
        $msg2 = $qrxl->xl("execution.languageCheckStart2");
        $status->write($msg1 . count($recipients) . $msg2);

        // for each recipient
        foreach ($recipients as $recipient)
        {
            $language = $recipient->getLanguage();
            if ($language == $this->qrbCfg->getDefaultLanguage())
                continue;
            
            $missingKeys = array();
            
            if ($pdfData)
                $pdfData->existsLocalizedData($language, $missingKeys);
            
            if ($mailData)
                $mailData->existsLocalizedData($language, $missingKeys);
            
            if ($additionalInfo)
                $additionalInfo->existsLocalizedData($language, $missingKeys);
            
            if (! empty($missingKeys))
                $this->logMissingLanguage($status, $qrxl, $recipient->getIndex(), $language, $missingKeys);
        }
    }

    // Writes the missing keys for the given language to the status.
    //
    // status:      status writer
    // qrxl:        translator
    // index:       current index
    // language:    current langauge
    // missingKeys: missing fields for given language
    private function logMissingLanguage(QrbProcessStatus $status, 
                                        QrbTranslator    $qrxl,
                                        int              $index, 
                                        string           $language, 
                                        array            $missingKeys
        ) : void
    {
        $msg1 = $qrxl->xl("execution.languageCheckWarn1");
        $msg2 = $qrxl->xl("execution.languageCheckWarn2");
        $msg3 = $qrxl->xl("execution.languageCheckWarn3");
        $status->write($msg1 . $index . $msg2 . $language . $msg3);
        
        $msg = "";

        foreach ($missingKeys as $missingFile => $missingKey )
        {
            $fileName = $missingFile;
            $keys = "";

            if (is_array($missingKey))    // MailBody.html for example has no keys but several files
            {
                foreach ($missingKey as $key => $value)
                {
                    $keys .= " {$key}";
                }
            }

            $msg .= "\t{$fileName}" . ($keys != "" ? ":\t{$keys}" : "") . "<br />\n" ;
        }
        
        $status->write($msg);
    }
}

?>
