<?php

namespace common\components;

use domain\Producer\Producer\Producer;
use domain\Setting\AdminSettingBag;
use yii\base\ErrorException;
use yii\mail\MessageInterface;

class MailerService
{
    public function sendFromProducer(string $subject, string $view, array $params, string $toEmail, Producer $producer)
    {
        $message = $this->composeBase($toEmail, $view, $params);
        $this->initMessageFromProducer($message, $subject, $producer);
        return $message->send();
    }

    public function sendFromSite(string $subject, string $view, array $params, string $toEmail)
    {
        $message = $this->composeBase($toEmail, $view, $params);
        $this->initMessageFromSite($message, $subject);
        return $message->send();
    }

    public function sendProducer(Producer $producer, string $subject, string $view, array $params = [], string $replyTo = null)
    {
        $message = $this->composeBase($producer->contact_email, $view, $params);
        if($replyTo) {
            $message->setReplyTo($replyTo);
        }
        $this->initMessageFromSite($message, $subject);
        return $message->send();
    }

    public function sendAdmin(string $subject, string $view, array $params = [], string $replyTo = null)
    {
        $message = $this->composeBase(\Yii::$app->parameterBag->get('adminEmail'), $view, $params);
        if($replyTo) {
            $message->setReplyTo($replyTo);
        }
        $this->initMessageFromSite($message, $subject);
        return $message->send();
    }

    public function getMailer()
    {
        $mailerSetting = AdminSettingBag::getInstance()->get('mailer');
        if(!$mailerSetting) {
            throw new ErrorException("Aucun mailer défini pour les emails courants.");
        }

        if(isset(\Yii::$app->$mailerSetting)) {
            return \Yii::$app->$mailerSetting;
        }
        else {
            throw new ErrorException("La configuration pour le mailer $mailerSetting est introuvable dans main-local.php");
        }
    }

    private function composeBase(string $toEmail, string $view, array $params = [])
    {
        return $this->getMailer()
            ->compose([
                'html' => '@common/mail/'.$view.'-html.php',
                'text' => '@common/mail/'.$view.'-text.php'
            ], $params)
            ->setTo($toEmail);
    }

    private function initMessageFromSite(MessageInterface $message, string $subject)
    {
        $message->setFrom([\Yii::$app->parameterBag->get('adminEmail') => \Yii::$app->parameterBag->get('siteName')]);
        $message->setSubject($this->buildSubject(\Yii::$app->parameterBag->get('siteName'), $subject));
        return $message;
    }

    private function initMessageFromProducer(MessageInterface $message, string $subject, Producer $producer)
    {
        $message->setFrom([$this->getProducerEmailPlatform($producer) => $producer->name]);
        $message->setSubject($this->buildSubject($producer->name, $subject));
        return $message;
    }

    private function getProducerEmailPlatform(Producer $producer): string
    {
        return $producer->slug . '@souke.fr';
    }

    private function buildSubject(string $prefix, string $subject)
    {
        return '['.$prefix.'] '.$subject;
    }
}