<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class Notify extends Buyers
{
    public $admin_notify_dir;
    public $user_notify_dir;

    public function __construct()
    {
        require 'modules/mail/PHPMailer.php';
        require 'modules/mail/SMTP.php';
        require 'modules/mail/Exception.php';

        $this->admin_notify_dir = 'admin/design/html/notify/';
        $this->user_notify_dir  = 'design/'.$this->settings->theme.'/html/notify/';
    }

    public function email($to, $subject, $message)
    {
        $mail = new PHPMailer();
        $mail->isSMTP();
        //$mail->SMTPDebug = 2;
        //$mail->Debugoutput = 'html';

        //если нет хоть одной переменой отправим обычной функцией mail
        if(!$this->settings->mail_from || !$this->settings->mail_pass || !$this->settings->mail_host || !$this->settings->mail_port)
            $mail->isMail();

        $mail->Host = $this->settings->mail_host;
        $mail->Port = $this->settings->mail_port;
        $mail->SMTPSecure = 'ssl';
        $mail->SMTPAuth = true;
        $mail->CharSet = 'utf-8';
        $mail->Username = $this->settings->mail_from;
        $mail->Password = $this->settings->mail_pass;

        $mail->setFrom($this->settings->mail_from, $this->settings->mail_alias);
        $mail->addReplyTo($this->settings->mail_from);
        $mail->addAddress($to);
        $mail->Subject = $subject;
        $mail->msgHTML($message);
        //заменяем <br> на перенос строки
        $message = preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $message);
        //убираем все теги
        $mail->AltBody = strip_tags($message);
        return $mail->send();
    }

    public function telegram($message)
    {
        $chat_id = null;    //ID пользователя в ТГ
        $token = null;      //ТОКЕН бота в ТГ

        if($token && $chat_id)
        {
            $data = array('chat_id' => $chat_id, 'text' => $message);
            $url = sprintf('https://api.telegram.org/bot%s/sendMessage?%s', $token, http_build_query($data));
            file_get_contents($url);
        }
    }

    public function email_comment_admin($comment_id)
    {
        $template = $this->admin_notify_dir . 'email_comment_admin.tpl';

        if(!($comment = $this->comments->get_comment(intval($comment_id))))
            return false;

        $this->design->assign('comment', $comment);
        $this->send($template, $this->settings->admin_email);
    }

    public function email_feedback_admin($feedback_id)
    {
        $template = $this->admin_notify_dir . 'email_feedback_admin.tpl';

        if(!($feedback = $this->feedbacks->get_feedback(intval($feedback_id))))
            return false;

        $this->design->assign('feedback', $feedback);
        $this->send($template, $this->settings->admin_email);
    }

    public function email_password_remind_admin($manager_login)
    {
        $template = $this->admin_notify_dir . 'email_password_remind_admin.tpl';

        $new_password = $this->managers->generate_password();
        $passhash = password_hash($new_password, PASSWORD_BCRYPT);
        $this->managers->update_manager($manager_login, array("passhash" => $passhash));

        $this->design->assign('login', $manager_login);
        $this->design->assign('new_password', $new_password);
        $this->send($template, $manager_login);
    }

    private function send($template, $email)
    {
        $message = $this->design->fetch($this->config->root_dir . $template);
        $subject = $this->design->get_var('subject');
        $this->email($email, $subject, $message);
    }
}
