<?php

namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;


use MGProvision\Proxmox\v2\models\HardDisk;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Utility;
use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;

class AdditionalDiskService
{
    use WhmcsParams;
    use ApiService;
    use ProductService;

    public function hasDisk(){
        foreach ($this->vm()->getHardDisks() as $hardDisk)
        {
            if (!$hardDisk->isMaster())
            {
                return true;
            }
        }
        return false;
    }

    public function create(){
        $diskSize = $this->getSize();
        if(!$diskSize || $diskSize == "-1" ){
            return;
        }
        $formats = $this->configuration()->getAdditionalDiskFormat();
        $typeArray = $this->configuration()->getAdditionalDiskType();
        $type = $typeArray[0];
        foreach($typeArray as $t){
            $bus = $this->vm()->findFreeDeviceId($t);
            if($bus===null){
                $type = $t;
                break;
            }
        }
        $harDisk = new HardDisk($type . $bus);
        $harDisk->setSize($diskSize)
            ->setApi($this->api())
            ->setPath($this->vm()->getPath() . "/config")
            ->setMedia("disk")
            ->setBackup( 0)//off
            ->setStorage($this->configuration()->getAdditionalDiskStorage())
            ->setCache($this->configuration()->getAdditionalDiskCache())
            ->setFormat($formats[0])
            ->setMbps_rd($this->configuration()->getAdditionalDiskMbpsRd())
            ->setMbps_wr($this->configuration()->getAdditionalDiskMbpsRd())
            ->setDiscard($this->configuration()->isAdditionalDiskDiscard() ? "on" : null)
            ->setIops_rd($this->configuration()->getAdditionalDiskIopsRd())
            ->setIops_rd_max($this->configuration()->getAdditionalDiskIopsRdMax())
            ->setIops_wr($this->configuration()->getAdditionalDiskIopsWr())
            ->setIops_wr_max($this->configuration()->getAdditionalDiskIopsWrMax())
            ->setReplicate($this->configuration()->isAdditionalDiskReplicate() ? 0 : null);
        if ($this->configuration()->isAdditionalDiskIoThread() && in_array($type, ['virtio', 'scsi']))
        {
            $harDisk->setIothread(1);
        }
        if($this->configuration()->isPermissionAdditionalDiskBackup()){
            $harDisk->setBackup(null);//on
        }
        $harDisk->create();
    }

    public function resize(){
        $diskSize = $this->getSize();
        if(!$diskSize || $diskSize == "-1" ){
            return;
        }
        foreach ($this->vm()->getHardDisks() as $hardDisk)
        {
            if ($hardDisk->isMaster())
            {
                continue;
            }
            if($hardDisk->getGb() == $diskSize){
                return true;
            }
            if ( (int) $hardDisk->getGb() > (int) $diskSize){
                throw new \InvalidArgumentException("Downgrading the additional disk size is restricted");
            }
            $size = "+" . abs((int)$diskSize - $hardDisk->getGb()) . "G";
            $hardDisk->resize($size);
            return true;
        }
    }
    private function getSize(){
        $diskSize = $this->configuration()->getAdditionalDiskSize();
        if ($this->isWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE))
        {
            $diskSize = $this->getWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE);
            Utility::unitFormat($diskSize, $this->configuration()->getAdditionalDiskUnit(), 'gb');
        }
        return $diskSize;
    }
}