<?php

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Sections;

use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
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\Fields\Range;
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();
    }

    public function initFields()
    {
        //size
        $this->resourceManager = new ResourceManager();
        $field = new Range('additionalDiskSize', $this->configuration()->serverDiskSize->min, $this->resourceManager->disk()->free());
        $field->addValidator(new DiskSizeValidator('additionalDiskSize',true));
        $field->setDefaultValue($this->configuration()->serverDiskSize->min);
        $field->setDescription('description');
        $field->replaceClasses(['lu-col-md-6']);
        $this->addField($field);
        //backup
        if ($this->configuration()->isPermissionAdditionalDiskBackup())
        {
            $field = new Switcher('additionalDiskBackup');
            $field->replaceClasses(['lu-form-check lu-m-b-2x']);
            $this->addField($field);
        }
        //bus
        $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']);
        if($this->configuration()->isQemu() && count($this->configuration()->getAdditionalDiskType()) < 2){
            $field->addClass('hidden');
        }
        $this->addField($field);
        //format
        $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']);
        if($this->configuration()->isQemu() && count($this->configuration()->getAdditionalDiskFormat()) < 2){
            $field->addClass('hidden');
        }
        $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);
        }
    }
}