<?php
    if(!class_exists('PHPMailer')){
        require("$_SERVER[DOCUMENT_ROOT]/sistema/addons/PHPMailer/PHPMailerAutoload.php");
    }

    class EmailTemplates {
    
        private $template = null;
        private $params = array();

        public function __construct($template) {
            $this->template = file_get_contents($_SERVER["DOCUMENT_ROOT"].'/public/email/' . $template . '.html');
        }

        public function bindParam($param, $value){
            $this->params['{{' . $param . '}}'] = $value;
            return $this;
        }

        public function bindFragment($param, $template, $arrValues){
            $temp = new self($template);

            foreach($arrValues as $key => $value){
                $temp->bindParam($key, $value);
            }

            $builded = $temp->build();

            if(array_key_exists('{{' . $param . '}}', $this->params)){
                $this->params['{{' . $param . '}}'] .= $builded;
            }else{
                $this->params['{{' . $param . '}}'] = $builded;
            }
        }

        public function build(){
            $template = $this->template;
            foreach ($this->params as $key => $value) {
                $template = str_replace($key, $value, $template);
            }
            return $template;
        }

    }

    class Email extends PHPMailer {

        private $template = null;

        public function __construct() {
            
            $this->isHTML(true);
            $this->isSMTP();
            $this->Host = 'smtp.zoho.com';
            $this->Port = 465;
            $this->SMTPAuth = true;
            $this->SMTPSecure = 'ssl';
            $this->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer'  => false,
                    'verify_peer_name'  => false,
                    'allow_self_signed' => true
                )
            );
            $this->Username = 'nao-responda@araraazulweb.com';
            $this->Password = '12qwaszx2017';
            $this->setFrom('nao-responda@araraazulweb.com', utf8_decode('Arara Azul Web'));
        }

        public function useTemplate($template = 'email') {
            $this->template = new EmailTemplates($template);
        }

        public function bindParam($param, $value) {
            $this->template->bindParam($param, $value);
            return $this;
        }

        public function bindFragment($param, $template, $arrValues){
            $this->template->bindFragment($param, $template, $arrValues);
            return $this;
        }

        public function go($name, $email, $subject, $param5 = null, $param6 = null) {
            if($this->template != null) {
                $body  = $this->template->build();
                $debug = $param5;
            } else {
                $body  = $param5;
                $debug = $param6;
            }

            $this->Subject = utf8_decode($subject);
            $this->Body = utf8_decode($body);

            $this->addAddress($email, utf8_decode($name));

            $send = $this->send();

            if(!$send) {
                if($debug == true)
                    return $this->ErrorInfo;
                return false;
            }

            return true;
        }
    }