<?php


namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;

use MGProvision\Proxmox\v2\models\MountPoint;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Utility;
use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

class AdditionalMountPointService
{
    use WhmcsParams;
    use ApiService;
    use ProductService;

    public function hasMountPoint(){
        foreach (sl('Vm')->getVm()->getMounPoints()->fetch() as $mounPoint){
            if($mounPoint->isMaster()){
                continue;
            }
            return true;
        }
        return false;
    }

    public function create($config){
        for ($i=1; $i<=10; $i++){
            $size = (int) $config['additionalDiskSize'.$i];
            if(!$size){
                continue;
            }
            $storage = $this->configuration()->getMountPointStorage();
            $mountPointRepository = sl('Vm')->getVm()->getMounPoints();
            $hdd = new MountPoint($mountPointRepository->nextId());
            $hdd->setLocation($storage . ":" . $size )
                ->setAcl($this->configuration()->getMountPointAcl() == "default" ? null : $this->configuration()->getMountPointAcl())
                ->setRo($this->configuration()->isMountPointRo() ? 1 : null)
                ->setQuota($this->configuration()->isMountPointQuota() ? 1 : null)
                ->setBackup($config['additionalDiskBackup'.$i]== "on" ? 1 : null)
                ->setReplicate($this->configuration()->isMountPointReplicate() ? '0' : null)
                ->setMp($config['additionalDiskMp'.$i]? $config['additionalDiskMp'.$i] :'/additional')
                ->setPath(sl('Vm')->getVm()->getPath() . '/config')
                ->setApi($this->api())
                ->create();
        }

    }

}