<?php
namespace App\Shell;

use App\Utility\ManageLog;
use Cake\Console\Shell;
use Cake\Mailer\Email;
use Cake\Log\Log;
/**
 * App shell command.
 */
class AppShell extends Shell
{
    
    public function getSystemCurrentTimeStamp($formate='',$time_zone = "UTC")
    {
        date_default_timezone_set($time_zone);
        if($formate==''){
            return date("Y-m-d H:i:s");
        } else {
            return date($formate);
        }
    }

    /**
     * Manage the available sub-commands along with their arguments and help
     *
     * @see http://book.cakephp.org/3.0/en/console-and-shells.html#configuring-options-and-generating-help
     *
     * @return \Cake\Console\ConsoleOptionParser
     */
    public function getOptionParser()
    {
        $parser = parent::getOptionParser();

        return $parser;
    }

    /**
     * main() method.
     *
     * @return bool|int|null Success or error code.
     */
    public function main()
    {
        $this->out($this->OptionParser->help());
    }
    
    public function sendEmail($templateName, $templateContent, $to,$sub="New Client Portal Registration",$attachment = "",$from_name = APP_SMTP_FROM_NAME,$personalize = null)
    {
        $APP_SMTP_USERNAME = APP_SMTP_USERNAME;
        $APP_SMTP_FROM_NAME = APP_SMTP_FROM_NAME;
        $APP_EMAIL_CLASS = APP_EMAIL_CLASS;
        $APP_SMTP_HOST = APP_SMTP_HOST;
        $APP_SMTP_PORT = APP_SMTP_PORT;
        $APP_SMTP_TIMEOUT = APP_SMTP_TIMEOUT;
        $APP_SMTP_PASSWORD = APP_SMTP_PASSWORD;
        $APP_SMTP_IS_TLS = APP_SMTP_IS_TLS;

        $APP_SMTP_FROM_NAME = $from_name;
        
        
        
        if(!empty($personalize) && $personalize->is_smtp == 1){
            $APP_SMTP_USERNAME = $personalize->username;
            $APP_SMTP_FROM_NAME = $personalize->smtp_from;
            $APP_SMTP_HOST = $personalize->smtp_host;
            $APP_SMTP_PORT = $personalize->smtp_port;
            $APP_SMTP_PASSWORD = $personalize->password;
            $templateContent['email_signature'] = $personalize->email_signature;
        }
        
        $SMTP_FORM_EMAIL = $APP_SMTP_USERNAME;
        
        if($APP_SMTP_HOST == 'sandbox.smtp.mailtrap.io'){
            $SMTP_FORM_EMAIL = $APP_SMTP_FROM_NAME;
        }
        
        if(isset($templateContent['company_email']) && !empty($templateContent['company_email'])){
            $SMTP_FORM_EMAIL = $templateContent['company_email'];
        }
        
        if(isset($templateContent['company_name']) && !empty($templateContent['company_name'])){
            $APP_SMTP_FROM_NAME = $templateContent['company_name'];
        }
        
        $default = [
            'className' => $APP_EMAIL_CLASS,
            'host' => $APP_SMTP_HOST,
            'port' => $APP_SMTP_PORT,
            'timeout' => $APP_SMTP_TIMEOUT,
            'username' => $APP_SMTP_USERNAME,
            'password' => $APP_SMTP_PASSWORD,
            'tls' => $APP_SMTP_IS_TLS
            
        ];
        try {

            $email = new Email();
            //$email = $email->reset();
//            if(!empty($personalize) && $personalize->is_smtp == 1){
//                $email->config($default);
//            } else {
//                $email->transport('default');
//            }
//            if (isset($templateContent['no_reply']) && !empty($templateContent['no_reply'])) {
//                $email = $email->from($APP_SMTP_FROM_NAME);
//            } else {
//                $email = $email->from($SMTP_FORM_EMAIL,$APP_SMTP_FROM_NAME);
//            }


            $email->setTemplate($templateName)
                ->setEmailFormat('html')
                ->setTo($to)
                ->setSubject($sub)
                ->setViewVars($templateContent);

            if(!empty($attachment)){
                $email->addAttachments($attachment);
            }
            $email ->send();

        } catch (\Exception $ex) {
            $this->log($ex);
        }
    }
    
    
    


    public function sendMultipleEmail($templateName, $templateContentArray, $personalize = null) {
        $APP_SMTP_USERNAME = APP_SMTP_USERNAME;
        $APP_SMTP_FROM_NAME = APP_SMTP_FROM_NAME;
        $APP_EMAIL_CLASS = APP_EMAIL_CLASS;
        $APP_SMTP_HOST = APP_SMTP_HOST;
        $APP_SMTP_PORT = APP_SMTP_PORT;
        $APP_SMTP_TIMEOUT = APP_SMTP_TIMEOUT;
        $APP_SMTP_PASSWORD = APP_SMTP_PASSWORD;
        $APP_SMTP_IS_TLS = APP_SMTP_IS_TLS;
        
       
        if (!empty($personalize)) {
            $templateContent['email_signature'] = $personalize->email_signature;
            if ($personalize->is_smtp == 1) {
                $APP_SMTP_USERNAME = $personalize->username;
                $APP_SMTP_FROM_NAME = $personalize->smtp_from;
                $APP_SMTP_HOST = $personalize->smtp_host;
                $APP_SMTP_PORT = $personalize->smtp_port;
                $APP_SMTP_PASSWORD = $personalize->password;
            }
        }

        $default = [
            'className' => $APP_EMAIL_CLASS,
            'host' => $APP_SMTP_HOST,
            'port' => $APP_SMTP_PORT,
            'timeout' => $APP_SMTP_TIMEOUT,
            'username' => $APP_SMTP_USERNAME,
            'password' => $APP_SMTP_PASSWORD,
            'tls' => $APP_SMTP_IS_TLS,
        ];
        
        $SMTP_FORM_EMAIL = $APP_SMTP_USERNAME;
        if($APP_SMTP_HOST == 'smtp.mailtrap.io'){
            $SMTP_FORM_EMAIL = $APP_SMTP_FROM_NAME;
        }
        if(isset($templateContent['company_email']) && !empty($templateContent['company_email'])){
            $SMTP_FORM_EMAIL = $templateContent['company_email'];
        }
        
        if(isset($templateContent['company_name']) && !empty($templateContent['company_name'])){
            $APP_SMTP_FROM_NAME = $templateContent['company_name'];
        }

        try {

            $email = new Email();
            if (!empty($personalize) && $personalize->is_smtp == 1) {
                $email->config($default);
            } else {
                $email->transport('default');
            }
            
            if(!empty($templateContentArray)){
                foreach($templateContentArray as $templateContent){
                    
                    $to = $templateContent['to'];
                    $sub = $templateContent['sub'];
                    $attachment = $templateContent['attachment'];
                    
                    $cli_logData = [];
                    $cli_logData['action'] = "CRONJOB_TO_EMAIL_INFO_setupEmailAutomation";
                    $L['automtion_schedule_id'] = $templateContent['automtion_schedule_id'];
                    $L['email'] = $to;
                    $L['sub'] = $sub;
                    $L['member_id'] = $personalize->member_id > 0 ? $personalize->member_id : 0;
                    $cli_logData['data'] = $L;
                    $this->cronjobLog($cli_logData);
                    
                    if (isset($templateContent['no_reply']) && !empty($templateContent['no_reply'])) {
                        $email = $email->from($APP_SMTP_FROM_NAME);
                    } else {
                        $email = $email->from($SMTP_FORM_EMAIL,$APP_SMTP_FROM_NAME);
                    }

                    $email->template($templateName)
                            ->emailFormat('html')
                            ->to($to)
                            ->viewVars($templateContent)
                            ->subject($sub);

                    if (!empty($attachment)) {
                        $email->addAttachments($attachment);
                    }
                    $email->send();
                }
            }
        } catch (Exception $ex) {
            $this->log($ex);
        }
    }

   
    
    public function cronjobLog($data){
        ManageLog::createLog($data);
    }
    
    
    
}
