<?php

namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;

use MGProvision\Proxmox\v2\repository\FileRepository;
use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\Resource;
use ModulesGarden\ProxmoxAddon\App\Services\Utility;
use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;

class ResourceManager
{
    use WhmcsParams;
    use ProductService;
    use ApiService;
    /**
     * @var Resource[]
     */
    protected $items = [];

    protected $backupFileRepository;

    /**
     * @return FileRepository
     * @throws \Exception
     */
    protected function backupFileRepository(){
        if(!is_null($this->backupFileRepository)){
            return $this->backupFileRepository;
        }
        $this->backupFileRepository = new FileRepository();
        $this->backupFileRepository->setApi($this->api());
        $this->backupFileRepository->findBackup($this->vm())
            ->findByStorages([$this->configuration()->getBackupStorage()]);
        return $this->backupFileRepository;
    }
    /**
     * @return Resource|Resource
     */
    public function backupSize()
    {
        //cache
        if ($this->items[__FUNCTION__]) {
            return $this->items[__FUNCTION__];
        }
        $resurce = new Resource(__FUNCTION__);
        $resurce->setUnit("bytes");
        if ($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_SIZE)) {
            $total = (int)$this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_SIZE);
        } else {
            $total = (int)$this->configuration()->getBackupMaxSize();
        }
        if($total != -1){
            Utility::unitFormat($total,"gb","bytes");
        }
        $resurce->setTotal($total);
        $resurce->setMin(0);
        $resurce->setMax($total);
        //Backup repository
        $resurce->setUsed($this->backupFileRepository()->size());
        return $this->items[__FUNCTION__] = $resurce;
    }

    /**
     * @return Resource|Resource
     */
    public function backupFiles()
    {
        //cache
        if ($this->items[__FUNCTION__]) {
            return $this->items[__FUNCTION__];
        }
        $resurce = new Resource(__FUNCTION__);
        $resurce->setUnit("int");
        if ($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)) {
            $total = (int)$this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
        } else {
            $total = (int)$this->configuration()->getBackupMaxFiles();
        }
        $resurce->setTotal($total);
        $resurce->setMin(0);
        $resurce->setMax($total);
        //Backup repository
        $resurce->setUsed($this->backupFileRepository()->count());
        return $this->items[__FUNCTION__] = $resurce;
    }
}