<?php
namespace TourOperatorProFrontend\app\helpers;

use TourOperatorPro\Factory;
use TourOperatorPro\Mvc\Model\Model;
use TourOperatorPro\Mvc\Model\Model as ModelAlias;
use TourOperatorPro\Utilities\Utility;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


require_once (TOUROPERATORPRO_PATH_ROOT."vendor/phpmailer/phpmailer/src/Exception.php");
require_once (TOUROPERATORPRO_PATH_ROOT."vendor/phpmailer/phpmailer/src/PHPMailer.php");
require_once (TOUROPERATORPRO_PATH_ROOT."vendor/phpmailer/phpmailer/src/SMTP.php");

class EmailHelper extends FrontendHelper
{

    public static function connect_mail_SMTP(){
        $model = Model::getInstance("emailaccounts");
        $config = $model->getList();
        foreach ( $config as $config) {
            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->Mailer = $config->mailer;
            $mail->SMTPDebug = 0;
            $mail->SMTPAuth = true;
            $mail->SMTPSecure = 'tls';
            $mail->Port = $config->port;
            $mail->Host = $config->host;
            $mail->Username = $config->username;
            $mail->Password = $config->password;
        }
        return $mail;
    }

    public static function get_email_info_tour(){
        $db=Factory::getDBO();
        $query=$db->getQuery(true);
        $send_info_tour=static::get_email_key_info_tour();
        $query->select("*")
            ->from(Utility::getTableName('email'))
            ->where('type='.$query->quote($send_info_tour))
            ;
        $email=$db->setQuery($query)->loadObject();
        return $email;

    }
    public static function get_email_key_info_tour(){
        $appConfig = Factory::getAppConfig();
        $email_type = $appConfig->get('email_tyle','');
        return $email_type;
    }
    public static function get_list_email_type(){
        $list_email_type=array();
        $item=new \stdClass();
        $item->type="send_info_tour";
        $item->description="send info tour";
        $list_email_type[]=$item;
        $item=new \stdClass();
        $item->type="send_info_tour_config";
        $item->description="send info tour config";
        $list_email_type[]=$item;
        return $list_email_type;
    }

    public static function check_in_array_mail_type($type)
    {
        $list_type=self::get_list_email_type();
        foreach ($list_type as $item){
            if($item->type===$type){
                return true;
            }
        }
        return false;
    }

    public static function getDescriptionType($type="")
    {
        $list_type=self::get_list_email_type();
        foreach ($list_type as $item){
            if($item->type===$type){
                return $item->description;
            }
        }
        return "";
    }

}