<?php

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Providers;

use MGProvision\Proxmox\v2\repository\ClusterResourcesRepository;
use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\UserService;
use ModulesGarden\ProxmoxAddon\App\Services\CloudService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\AppParams;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;

class ImportVmProvider extends BaseDataProvider implements ClientArea
{
    use ProductService;
    use ApiService;
    use UserService;

    public function read()
    {
        $this->api();
        $clusterResurces = new ClusterResourcesRepository();
        $clusterResurces->findVm();
        //qemu or lxc
        $this->configuration()->isQemu() ? $clusterResurces->findQemu() : $clusterResurces->findLxc();
        foreach ($clusterResurces->fetch() as $resource){
            if($resource->getTemplate()){
                continue;
            }
            if(VmModel::where("vmid", $resource->getVmid())->count()){
                continue;
            }
            if(Hosting::ofCustomFielVmid($resource->getVmid())->count()){
                continue;
            }
            $this->availableValues['vmid'][$resource->getVmid()] = sprintf("VMID: %s (%s)", $resource->getVmid(), $resource->getName());
        }

    }

    public function update()
    {
    }

    public function delete()
    {

    }

    public function import()
    {
        $this->api();
        $clusterResurces = new ClusterResourcesRepository();
        $clusterResurces->findVm();
        $clusterResurces->findVmid($this->getFormDataValues()['vmid']);
        $resurce = $clusterResurces->firstOrFail();
        $vm =   $resurce->getVm();
        $vmModel = new VmModel();
        $vmModel->hosting_id = $this->getWhmcsParamByKey('serviceid');
        $vmModel->vmid = $vm->getVmid();
        $vmModel->node = $vm->getNode();
        $vmModel->name = $resurce->getName();
        $vmModel->virtualization = $this->configuration()->getVirtualization();
        $vmModel->password = encrypt($this->getFormDataValues()['password']);
        $vmModel->cores = $vm->config()['cores'];
        $vmModel->sockets = (int) $vm->config()['sockets'];
        $vmModel->vcpus = (int) $vm->config()['vcpus'];
        $vmModel->cpulimit  =  $vm->config()['cpulimit'];
        $vmModel->cpuunits =  $vm->config()['cpuunits'];
        $vmModel->memory =  $vm->config()['memory'];
        $vmModel->swap  = (int)  $vm->config()['swap'];
        $vmModel->disk   = (int)  $vm->getMasterHddSize();
        if($this->configuration()->isQemu()){
            $vmModel->disks  = (int)  $vm->getHardDiskRepostiory()->additionalSize();
        }
        if($this->configuration()->isLxc()){
            $vmModel->disks  = (int)  $vm->getMounPoints()->additionalSize();
        }
        $this->userUpdatePermission([$vmModel->vmid]);
        $vmModel->save();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Virtual Machine has been imported successfully');
    }
}