<?php
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

class Mailer
{
    private $mailEmitter;
    private $mailReceiver;
    private $mailObject;
    private $mailMessage;

    private $mailMemberFirstName;

    private $mailMemberLastName;

    private $mailMemberNo;

    private $mailCourseName;

    private $mailCourseState;

    public function __construct($mailEmitter, $mailReceiver, $mailObject, $mailMessage, $mailMemberFirstName, $mailMemberLastName, $mailMemberNo, $mailCourseName, $mailCourseState)
    {
        $this->mailEmitter = $mailEmitter;
        $this->mailReceiver = $mailReceiver;
        $this->mailObject = $mailObject;
        $this->mailMessage = $mailMessage;
        $this->mailMemberFirstName = $mailMemberFirstName;
        $this->mailMemberLastName = $mailMemberLastName;
        $this->mailMemberNo = $mailMemberNo;
        $this->mailCourseName = $mailCourseName;
        $this->mailCourseState = $mailCourseState;

        //Replace value of message
        $this->replaceParametterValue();
    }

    public function getMailEmitter()
    {
        return $this->mailEmitter;
    }

    public function setMailEmitter($mailEmitter)
    {
        $this->mailEmitter = $mailEmitter;
    }

    public function getMailReceiver()
    {
        return $this->mailReceiver;
    }

    public function setMailReceiver($mailReceiver)
    {
        $this->mailReceiver = $mailReceiver;
    }

    public function getMailObject()
    {
        return $this->mailObject;
    }

    public function setMailObject($mailObject)
    {
        $this->mailObject = $mailObject;
    }

    public function getMailMessage()
    {
        return $this->mailMessage;
    }

    public function setMailMessage($mailMessage)
    {
        $this->mailMessage = $mailMessage;
    }

    public function getMailMemberFirstName()
    {
        return $this->mailMemberFirstName;
    }

    public function setMailMemberFirstName($mailMemberFirstName)
    {
        $this->mailMemberFirstName = $mailMemberFirstName;
    }

    public function getMailMemberLastName()
    {
        return $this->mailMemberLastName;
    }

    public function setMailMemberLastName($mailMemberLastName)
    {
        $this->mailMemberLastName = $mailMemberLastName;
    }

    public function getMailMemberNo()
    {
        return $this->mailMemberNo;
    }

    public function setMailMemberNo($mailMemberNo)
    {
        $this->mailMemberNo = $mailMemberNo;
    }

    public function getMailCourseName()
    {
        return $this->mailCourseName;
    }

    public function setMailCourseName($mailCourseName)
    {
        $this->mailCourseName = $mailCourseName;
    }

    public function getMailCourseState()
    {
        return $this->mailCourseState;
    }

    public function setMailCourseState($mailCourseState)
    {
        $this->mailCourseState = $mailCourseState;
    }

    public function replaceParametterValue()
    {
        $this->mailObject = str_replace("%FIRST%", $this->mailMemberFirstName, $this->mailObject);
        $this->mailObject = str_replace("%LAST%", $this->mailMemberLastName, $this->mailObject);
        $this->mailObject = str_replace("%MEMBERNO%", $this->mailMemberNo, $this->mailObject);
        $this->mailObject = str_replace("%COURSE%", $this->mailCourseName, $this->mailObject);
        $this->mailObject = str_replace("%STATE%", $this->mailCourseState, $this->mailObject);

        $this->mailMessage = str_replace("%FIRST%", $this->mailMemberFirstName, $this->mailMessage);
        $this->mailMessage = str_replace("%LAST%", $this->mailMemberLastName, $this->mailMessage);
        $this->mailMessage = str_replace("%MEMBERNO%", $this->mailMemberNo, $this->mailMessage);
        $this->mailMessage = str_replace("%COURSE%", $this->mailCourseName, $this->mailMessage);
        $this->mailMessage = str_replace("%STATE%", $this->mailCourseState, $this->mailMessage);
    }

    public function sendMail()
    {
        $mail = new PHPMailer(true);

        $setting = Setting::getInstance();
        $setting->loadSetting();


        try {
            //Server settings
            // $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
            $mail->isSMTP(); //Send using SMTP
            $mail->Host =  $setting->getSmtpServer();
            if($setting->getSmtpSSL()==1){
                $mail->SMTPAuth = true;
                $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
            }
            $mail->Username = $setting->getSmtpUser();
            $mail->Password = $setting->getSmtpPassword();
            $mail->Port = $setting->getSmtpPort();

            //Recipients
            $mail->setFrom("modulecourses@clubalpinsion.ch", $setting->getClubName());
            $mail->addAddress($this->mailReceiver); //Add a recipient
            // $mail->addAddress('ellen@example.com'); //Name is optional
            // $mail->addReplyTo('info@example.com', 'Information');
            // $mail->addCC('cc@example.com');
            // $mail->addBCC('bcc@example.com');

            //Attachments
            // $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
            // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name

            //Content
            $mail->isHTML(true); //Set email format to HTML
            $mail->Subject = $this->getMailObject();
            $mail->Body = $this->getMailMessage();
            $mail->AltBody = $this->getMailMessage();

            $mail->send();

        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    }

    public static function getTemplate($templateCode)
    {
        global $wpdb;
        $sql = "
        SELECT * FROM cass_setting WHERE IDSetting = 1;
         ";

        $result = $wpdb->get_row($wpdb->prepare($sql));

        switch ($templateCode) {
            case 'mailContentParticipiantValidationToRLObject':
                return $result->mailContentParticipiantValidationToRLObject;
            case 'mailContentParticipiantValidationToRL':
                return $result->mailContentParticipiantValidationToRL;
            case 'mailContentParticipiantExcusationToRLObject':
                return $result->mailContentParticipiantExcusationToRLObject;
            case 'mailContentParticipiantExcusationToRL':
                return $result->mailContentParticipiantExcusationToRL;
            case 'mailContentParticipiantValidationToMemberObject':
                return $result->mailContentParticipiantValidationToMemberObject;
            case 'mailContentParticipiantValidationToMember':
                return $result->mailContentParticipiantValidationToMember;
            case 'mailContentParticipiantExcusationToMemberObject':
                return $result->mailContentParticipiantExcusationToMemberObject;
            case 'mailContentParticipiantExcusationToMember':
                return $result->mailContentParticipiantExcusationToMember;
            case 'mailContentRaceStateChange':
                return $result->mailContentRaceStateChange;
            case 'mailContentRaceStateChangeObject':
                return $result->mailContentRaceStateChangeObject;
        }

    }

}
