<?php

namespace ModulesGarden\ProxmoxAddon\App\Services;

use MGProvision\Proxmox\v2 as proxmox;
use ModulesGarden\ProxmoxAddon as main;

/**
 * Doe labels datatable controler
 *
 */
trait BaseService
{
    private $serverId;

    /**
     *
     * @var main\Core\Models\Whmcs\Server
     */
    private $server;
    private $api;

    /**
     *
     * @return proxmox\Api
     */
    public function getApi()
    {
        if ($this->api)
        {
            return $this->api;
        }
        $host      = $this->getServer()->ipaddress ? $this->getServer()->ipaddress : $this->getServer()->hostname;
        if(is_numeric($this->getServer()->port)){
            $host  .=":".$this->getServer()->port;
        }
        $password = html_entity_decode(decrypt($this->getServer()->password),ENT_QUOTES);
        $this->api = new proxmox\Api($host, $this->getServer()->username, $this->getServer()->accesshash,$password );
        $this->api->debug(\ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings::isDebug());
        return $this->api ;
    }

    /**
     *
     * @return main\Core\Models\Whmcs\Server
     */
    public function getServer()
    {
        if ($this->server)
        {
            return $this->server;
        }
        return $this->server = main\Core\Models\Whmcs\Server::findOrFail($this->getServerId());
    }

    public function getServerId()
    {
        return $this->serverId;
    }

    public function setServerId($serverId)
    {
        $this->serverId = $serverId;
        return $this;
    }
}
