<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (27.03.19)
* *
*
*  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
*  CONTACT                        ->       contact@modulesgarden.com
*
*
* This software is furnished under a license and may be used and copied
* only  in  accordance  with  the  terms  of such  license and with the
* inclusion of the above copyright notice.  This software  or any other
* copies thereof may not be provided or otherwise made available to any
* other person.  No title to and  ownership of the  software is  hereby
* transferred.
*
*
* ******************************************************************** */

namespace ModulesGarden\ProxmoxAddon\App\Services;

use MGProvision\Proxmox\v2\Api;
use MGProvision\Proxmox\v2\models\Kvm;
use MGProvision\Proxmox\v2\models\Lxc;
use ModulesGarden\ProxmoxAddon\App\Enum\Vps;
use ModulesGarden\ProxmoxAddon\App\Enum\Cloud;
use ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings;

/**
 * Trait ApiService
 * @package ModulesGarden\ProxmoxAddon\App\Services
 */
trait ApiService
{
    /**
     * @return Api
     */
    public function api()
    {
        if (!empty($this->api))
        {
            return $this->api;
        }
        $host      = $this->getWhmcsParamByKey('serverip') ? $this->getWhmcsParamByKey('serverip') : $this->getWhmcsParamByKey('serverhostname');
        if(is_numeric($this->getWhmcsParamByKey('serverport'))){
            $host  .=":".$this->getWhmcsParamByKey('serverport');
        }
        $username  = $this->getWhmcsParamByKey('serverusername');
        $realm     = $this->getWhmcsParamByKey('serveraccesshash');
        $password  = $this->getWhmcsParamByKey('serverpassword');
        $this->api = new Api($host, $username, $realm, $password);
        $this->api->setInstance();
        $this->api->debug(ModuleSettings::isDebug());
        return $this->api;
    }

    /**
     * @return Kvm|Lxc
     * @throws \Exception
     */
    public function vm()
    {
        if (!empty($this->vm))
        {
            return $this->vm;
        }
        $virtualization = $this->configuration()->getVirtualization();
        $vmid           = $this->getWhmcsCustomField(Vps\CustomField::VMID);
        $node           = $this->getWhmcsCustomField(Vps\CustomField::NODE);
        switch (strtolower($virtualization))
        {
            case 'kvm':
            case 'qemu':
                $this->vm = new Kvm($node, $vmid);
                break;
            case 'lxc':
                $this->vm = new Lxc($node, $vmid);
                break;
            default:
                throw new \Exception(sprintf("Invalid virtualization type  '%s'", $virtualization));
        }
        $hostname = $this->getWhmcsParamByKey('domain') ? $this->getWhmcsParamByKey('domain') : $this->getWhmcsCustomField(Vps\CustomField::HOSTNAME);
        $this->vm->setName($hostname);
        $this->vm->setApi($this->api());
        $this->vm->setApiParser();
        return $this->vm;
    }

    public function hasVm()
    {
        return $this->getWhmcsCustomField(Vps\CustomField::VMID) && $this->getWhmcsCustomField(Vps\CustomField::NODE);
    }

    public function setVm($vm)
    {
        $this->vm = $vm;
        return $this;
    }


}