<?php

namespace Intgrtn\Lib;

/**
 * @author Heminei
 */
class EmailSender
{

    private $fromEmail = null;
    private $fromName = null;
    private $toEmails = [];
    private $subject = null;
    private $html = null;
    private $isSend = 0;
    private $error = null;

    public function send()
    {
        $emails = $this->getToEmails();
        if (empty($emails)) {
            throw new \ErrorException("Add to emails");
        }

        try {
            $mail = new \PHPMailer\PHPMailer\PHPMailer();
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            $mail->Timeout = 5;
            $mail->SMTPDebug = 0;
            $mail->Host = \Intgrtn\Config::instance()->getSMTPHost(); // sets the SMTP server
            $mail->Port = \Intgrtn\Config::instance()->getSMTPPort(); // set the SMTP port for the GMAIL server
            if (\Intgrtn\Config::instance()->getSMTP()) {
                $mail->IsSMTP(); // telling the class to use SMTP
            }
            if (!empty(\Intgrtn\Config::instance()->getSMTPUsername()) || !empty(\Intgrtn\Config::instance()->getSMTPPassword())) {
                $mail->SMTPAuth = true; // enable SMTP authentication
                $mail->Username = \Intgrtn\Config::instance()->getSMTPUsername(); // SMTP account username
                $mail->Password = \Intgrtn\Config::instance()->getSMTPPassword(); // SMTP account password
            }


            $mail->setFrom($this->getFromEmail(), $this->getFromName());

            foreach ($emails as $to) {
                $mail->addAddress($to['email'], $to['name']);
            }
            $mail->isHTML(true);
            $mail->Subject = $this->getSubject();
            $mail->Body = $this->getHtml();
            if (!$mail->send()) {
                throw new \ErrorException($mail->ErrorInfo);
            }

            $this->isSend = 1;
        } catch (\Exception $exc) {
            $this->error = $exc->getMessage();
        }

        return $this->isSend() == 1;
    }

    public function getFromEmail()
    {
        return $this->fromEmail;
    }

    public function getFromName()
    {
        return $this->fromName;
    }

    public function getToEmails()
    {
        return $this->toEmails;
    }

    public function getSubject()
    {
        return $this->subject;
    }

    public function getHtml()
    {
        return $this->html;
    }

    public function getIsSend()
    {
        return $this->isSend;
    }

    public function getError()
    {
        return $this->error;
    }

    public function setFromEmail($fromEmail)
    {
        $this->fromEmail = $fromEmail;
        return $this;
    }

    public function setFromName($fromName)
    {
        $this->fromName = $fromName;
        return $this;
    }

    public function addToEmail($email, $name = null)
    {
        if (empty($email)) {
            throw new ErrorException("To Email is empty");
        }
        $this->toEmails[] = [
            "email" => $email,
            "name" => $name,
        ];
        return $this;
    }

    public function setSubject($subject)
    {
        $this->subject = $subject;
        return $this;
    }

    public function setHtml($html)
    {
        $this->html = $html;
        return $this;
    }

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

}
