<?php

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmUpdate\Sections\Lxc;

use MGProvision\Proxmox\v2\models\MountPoint;
use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators\DiskSizeValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Switcher;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Text;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Sections\BaseSection;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Sections\BoxSection;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Sections\HalfPageSection;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class AdditionalDiskSection extends BaseSection implements ClientArea
{
    use ProductService;

    public function initContent()
    {

        $this->initIds('additionalDiskSection');
        $this->initFields();
        $this->loadAdditionalDisks();
    }

    public function initFields()
    {
        //size
        $field = new Text('additionalDiskSize');
        $field->addValidator(new DiskSizeValidator('disk',false));
        $field->replaceClasses(['lu-col-md-6']);
        $this->addField($field);
        //bus
        if($this->configuration()->isQemu()){
            $field = new Select('additionalDiskBus');
            $options=[];
            foreach( $this->configuration()->getAdditionalDiskType() as $entery){
                $options[$entery] = sl("lang")->abtr($entery);
            }
            $field->setAvailableValues($options);
            $field->replaceClasses(['lu-col-md-6']);
            $this->addField($field);
        }
        //format
        if($this->configuration()->isQemu()){
            $field = new Select('additionalDiskFormat');
            $options=[];
            foreach( $this->configuration()->getAdditionalDiskFormat() as $entery){
                $options[$entery] = sl("lang")->abtr($entery);
            }
            $field->setAvailableValues($options);
            $field->replaceClasses(['lu-col-md-6']);
            $this->addField($field);
        }
        if($this->configuration()->isLxc()){
            //mp
            $field = new Text('additionalDiskMp');
            $field->setPlaceholder("/some/path");
            $field->replaceClasses(['lu-col-md-6']);
            $this->addField($field);
        }
        //backup
        if ($this->configuration()->isPermissionMountPointBackup())
        {
            $field = new Select('additionalDiskBackup');
            $field->setAvailableValues([
                'on' => 'Enable',
                'off'=>'Disable',
            ]);
            $field->replaceClasses(['lu-col-md-6']);
            $this->addField($field);
        }
    }

    protected function loadAdditionalDisks(){
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $this->customTplVars['additionalDisks']=[];
        foreach ($vm->getHardDiskRepostiory()->fetch() as $disk){
            if($disk->isMaster()){
                continue;
            }
            $this->customTplVars['additionalDisks'][]=
                [   'id' => $disk->getId() ,
                    'size' => (int) $disk->getGb(),
                    'backup'=> $disk->isBackup() ? "on" : "off",
                    'mp' => $disk instanceof  MountPoint ? $disk->getMp() : null
                ];
        }

    }
}