<?php


namespace ModulesGarden\ProxmoxAddon\App\Services;


use ModulesGarden\ProxmoxAddon\Core\Api\Whmcs;
use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Admins;
use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\EmailTemplate;

class EmailService
{
    private $api;
    private $templateId;
    private $vars = [];
    private $lastResponse;

    /**
     * EmailService constructor.
     * @param $api
     */
    public function __construct()
    {
        $this->api = new Whmcs(new Admins());
    }

    public function template($templateId)
    {
        if (!$templateId)
        {
            throw new \InvalidArgumentException("Email Template Id cannot be empty");
        }
        $this->templateId          = $templateId;
        $this->vars                = EmailTemplate::select("name", "type")->where("id", $this->templateId)->first()->toArray();
        $this->vars['messagename'] = $this->vars['name'];
        unset($this->vars['name']);
        return $this;
    }

    public function vars(array $vars)
    {
        $this->vars = array_merge($this->vars, $vars);
        return $this;
    }

    public function getVars()
    {
        return $this->vars;
    }

    public function send()
    {
        $this->lastResponse = $this->api->call("sendemail", $this->vars);
        $this->vars         = [];
        $this->templateId   = null;
        return $this;
    }

    public function sendToAdmin()
    {
        if (function_exists('sendAdminMessage'))
        {
            /**
             * sendAdminMessage ($templateName, $adminId = "", $templateVars = array(), $to = "system", $deptid = "", $ticketNotify = "")
             */
            sendAdminMessage($this->vars['messagename'], $this->vars);
            $this->vars       = [];
            $this->templateId = null;
        }
        return $this;
    }

    /**
     * @return mixed
     */
    public function getLastResponse()
    {
        return $this->lastResponse;
    }


}