<?php

class Email
{

    private $registry;

    public $template;
    public $twig;
    public $to;
    public $sender;
    public $subject;
    public $text = false;
    public $language;

    public $mail;

    public function __construct($registry)
    {
        $this->registry = $registry;
        $this->load = $registry->get('load');
        $this->language = $registry->get('language');
        $this->config = $registry->get('config');
        $this->template = new TemplateController($registry);
        $this->mail = new Mail();
    }

    public function __get($name)
    {
        return $this->registry->get($name);
    }

    public function getConfigMail()
    {
        $this->mail->protocol = $this->config->get('config_mail_protocol');
        $this->mail->parameter = $this->config->get('config_mail_parameter');
        $this->mail->hostname = $this->config->get('config_smtp_host');
        $this->mail->username = $this->config->get('config_smtp_username');
        $this->mail->password = $this->config->get('config_smtp_password');
        $this->mail->port = $this->config->get('config_smtp_port');
        $this->mail->timeout = $this->config->get('config_smtp_timeout');
        $this->mail->setFrom($this->config->get('config_email'));
    }


    public function sendAdditionalEmail($attribute_value)
    {

        $attribute_value = json_decode($attribute_value);

        // Send to additional alert emails
        if (isset($attribute_value->mkt_integracao_tipo) && $attribute_value->mkt_integracao_tipo === Type_MarketPlace::Tp_Mercadolivre) {

            if($this->config->get('mercadolivre_order_email_status') == Type_MercadoLivreEmail::Tp_Habilitado){
                $emails = explode(',', $this->config->get('mercadolivre_alert_emails'));
            } elseif ($this->config->get('mercadolivre_order_email_status') == Type_MercadoLivreEmail::Tp_Default) {
                $emails = explode(',', $this->config->get('config_alert_emails'));
            } else {
                $emails = false;
            }

        } else {
            $emails = explode(',', $this->config->get('config_alert_emails'));
        }



        if ($emails) {
            foreach ($emails as $email) {
                $email = trim($email);
                try {
                    if ($email && preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $email)) {
                        $this->mail->setTo($email);
                        $this->mail->send(true);
                    }
                } catch (Exception $e) {
                    throw new exception('Error ao Enviar email finalização do pedido para e-mails extras: ' . $e);
                }
            }
        }

    }

    public function getTemplate($typetask)
    {

        $html = '';

        switch ($typetask) {
            case Type_SendTask::TpSe_PedidoLoja:
                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order.tpl')) {
                    $html = $this->template->loadOutPut($this->config->get('config_template') . '/template/mail/order.tpl', $this->template->data);
                } else {
                    $html = $this->template->loadOutPut('default/template/mail/order.tpl', $this->template->data);
                }
                break;
            case Type_SendTask::TpSe_PedidoMercadolivre:
                break;
            case Type_SendTask::TpSe_PedidoListaPresente:
                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order_p_comp.tpl')) {
                    $html = $this->template->loadOutPut($this->config->get('config_template') . '/template/mail/order_p_comp.tpl', $this->template->data);
                } else {
                    $html = $this->template->loadOutPut('default/template/mail/order_p_comp.tpl', $this->template->data);
                }
                break;
            case Type_SendTask::TpSe_PedidoDonoListaPresente:
                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order_p_cli.tpl')) {
                    $html = $this->template->loadOutPut($this->config->get('config_template') . '/template/mail/order_p_cli.tpl', $this->template->data);
                } else {
                    $html = $this->template->loadOutPut('default/template/mail/order_p_cli.tpl', $this->template->data);
                }
                break;
            case Type_SendTask::TpSe_PedidoDonoLoja:
                $template = ($this->lista) ? '/template/mail/order_p_admin.tpl' : '/template/mail/order_admin.tpl';

                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . $template)) {
                    $html = $this->template->loadOutPut($this->config->get('config_template') . $template, $this->template->data);
                } else {
                    $html = $this->template->loadOutPut('default/template/mail/order_p_admin.tpl', $this->template->data);
                }
                break;
            case Type_SendTask::TpSe_AddHistory:
                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order_status.tpl')) {
                    $html = $this->template->loadOutPut($this->config->get('config_template') . '/template/mail/order_status.tpl', $this->template->data);
                } else {
                    $html = $this->template->loadOutPut('mail/order_status.tpl', $this->template->data);
                }
                break;
            case Type_SendTask::TpSe_Cliente:
                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . 'mail/email.twig')) {
                    $html = $this->ftrgeralregistry->twigToString($this->config->get('config_template') . 'mail/email.twig', $this->template->data['content'], $this->template->data);
                } else {
                    $html = $this->ftrgeralregistry->twigToString('mail/email.twig', $this->template->data['content'], $this->template->data);
                }
                break;
            case Type_SendTask::TpSe_DevolucaoProdutoDonoLoja:
                $html = $this->template->loadOutPut($this->config->get('config_template') . '/template/mail/return.twig', $this->template->data);
                break;
        }

        return $html;

    }


}