<?php

/**
 * This Software is the property of Data Development and is protected
 * by copyright law - it is NOT Freeware.
 * Any unauthorized use of this software without a valid license
 * is a violation of the license agreement and will be prosecuted by
 * civil and criminal law.
 * http://www.shopmodule.com
 *
 * @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
 * @author        D3 Data Development - Daniel Seifert <support@shopmodule.com>
 * @link          http://www.oxidmodule.com
 */

namespace D3\MailConfigChecker\Application\Controller\Admin;

use Assert\Assert;
use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Email;
use OxidEsales\Eshop\Core\Registry;

class MailTester extends AdminDetailsController
{
    protected $_sThisTemplate = 'mailTester.tpl';

    public function sendMail()
    {
        try {
            $request = Registry::getRequest();

            $from       = trim($request->getRequestEscapedParameter('from'));
            $this->addTplParam('sender', $from);
            $to         = trim($request->getRequestEscapedParameter('to'));
            $this->addTplParam('recipient', $to);
            $subject    = trim($request->getRequestEscapedParameter('subject'));
            $this->addTplParam('subject', $subject);
            $body    = trim($request->getRequestEscapedParameter('body'));
            $this->addTplParam('body', $body);

            $this->assertContent();

            $mail = oxNew(Email::class);
            $mail->setFrom($from);
            $mail->sendEmail($to, $subject, $body);
            $this->addTplParam('success', true);
        } catch (\Exception $e) {
            Registry::getUtilsView()->addErrorToDisplay(nl2br($e->getMessage()));
        }
    }

    protected function assertContent()
    {
        $request = Registry::getRequest();
        $lang = Registry::getLang();

        Assert::lazy()
            ->setExceptionClass(d3TranslatableLazyAssertionException::class)
            ->that(
                $request->getRequestEscapedParameter('from'),
                $lang->translateString('D3_MAILCHECKER_SMTPCHECK_SENDER')
            )->email($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
            ->that(
                $request->getRequestEscapedParameter('to'),
                $lang->translateString('D3_MAILCHECKER_SMTPCHECK_RECIPIENT')
            )->email($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
            ->that(
                $request->getRequestEscapedParameter('subject'),
                $lang->translateString('D3_MAILCHECKER_TESTMAIL_SUBJECT')
            )->notBlank($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
            ->that(
                $request->getRequestEscapedParameter('body'),
                $lang->translateString('D3_MAILCHECKER_TESTMAIL_BODY')
            )->notBlank($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
            ->verifyNow();
    }

    public function getMailAddressList()
    {
        /** @var Shop $shop */
        $shop = Registry::getConfig()->getActiveShop();

        return array_filter(
            array_unique(
                [
                    $shop->getFieldData('oxinfoemail'),
                    $shop->getFieldData('oxorderemail'),
                    $shop->getFieldData('oxowneremail'),
                ]
            )
        );
    }
}