<?php

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

use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\CpulimitValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\HostnameValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\IpAddressValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\NumberValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\SshPublicKeyValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators\PasswordValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Range;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Text;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Textarea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Sections\BaseSection;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\isAdmin;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class GeneralSection extends BaseSection
{
    use ProductService;

    /**
     * @var ResourceManager
     */
    protected $resourceManager;

    public function initContent()
    {
        $this->initIds('generalSection');
        $this->unsetShowTitle();
        $this->resourceManager = new ResourceManager();
        $this->resourceManager->notVmIds([\ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id]);
        $this->initFields();


    }

    public function initFields()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $cpuPrioryty = $this->configuration()->hasCpuPriority();
        //name
        $field = new Text('name');
        $field->addValidator(new HostnameValidator());
        $field->setDescription('description');
        $this->addField($field);
        //description
        $field = new Textarea('description');
        $field->setDescription('description');
        $this->addField($field);
        if(isAdmin() || $this->configuration()->isPermissionIsoImage() && $vm->getCdRom()->count()){
            $field = new Select("iso");
            $field->notEmpty();
            $this->addField($field);
        }
        //password
        if($this->configuration()->isPermissionPassword()){
            $field = new Text('password');
            $field->addValidator( new PasswordValidator());
            $field->setDescription('description');
            $this->addField($field);
        }
        //sshkeys
        if($this->configuration()->isPermissionSshkeys()){
            $field = new Textarea("sshkeys");
            $field->addValidator(new SshPublicKeyValidator(false));
            $this->addField($field);
        }
        //sockets
        if($this->configuration()->isPermissionSockets() && !$this->configuration()->isCalculateSocketsAndCores()){
            $field = new Text('sockets');
            $field->addValidator(new NumberValidator($this->resourceManager->sockets()->getMin(), $this->resourceManager->sockets()->free(),true));
            $field->setDescription('description');
            $this->addField($field);
        }
        //cores
        if($this->configuration()->isPermissionCores() && !$this->configuration()->isCalculateSocketsAndCores()){
            $field = new Text('cores');
            $field->addValidator(new NumberValidator($this->resourceManager->cores()->getMin(), $this->resourceManager->cores()->free(),true));
            $field->setDescription('description');
            $this->addField($field);
        }
        //vcpus
        if($this->configuration()->isPermissionVcpus() || $this->configuration()->isCalculateSocketsAndCores()){
            $field = new Range('vcpus',$this->resourceManager->vcpus()->getMin(), $this->resourceManager->vcpus()->free());
            $field->addValidator(new NumberValidator($this->resourceManager->vcpus()->getMin(), $this->resourceManager->vcpus()->free(),true));
            $field->setDescription('description');
            $this->addField($field);
        }
        //cpu priority
        if($cpuPrioryty){
            $field = new Range('cpuPriority', $this->resourceManager->cpuPriority()->getMin(), $this->resourceManager->cpuPriority()->free());
            $field->addValidator(new NumberValidator($this->resourceManager->cpuPriority()->getMin(), $this->resourceManager->cpuPriority()->free(),true));
            $field->setDescription('description');
            $field->addHtmlAttribute('data-label1', sl('lang')->abtr("Very Low"));
            $field->addHtmlAttribute('data-label2', sl('lang')->abtr("Low"));
            $field->addHtmlAttribute('data-label3', sl('lang')->abtr("Normal"));
            $field->addHtmlAttribute('data-label4', sl('lang')->abtr("High"));
            $field->addHtmlAttribute('data-label5', sl('lang')->abtr("Medium"));
            $this->addField($field);
        }
        //cpulimit
        if($this->configuration()->isPermissionCpuLimit() && !$cpuPrioryty){
            $field = new Text('cpulimit');
            $field->addValidator(new CpulimitValidator($this->resourceManager->cpulimit()->getMin(), $this->resourceManager->cpulimit()->free(),true));
            $field->setDescription('description');
            $this->addField($field);
        }
        //cpuunits
        if($this->configuration()->isPermissionCpuunits( ) && !$cpuPrioryty){
            $field = new Text('cpuunits');
            $field->addValidator(new NumberValidator($this->resourceManager->cpuunits()->getMin(), $this->resourceManager->cpuunits()->free(),true));
            $field->setDescription('description');
            $this->addField($field);
        }
        //memory
        $field = new Text('memory');
        $field->addValidator(new NumberValidator($this->resourceManager->memory()->getMin(), $this->resourceManager->memory()->free(),true));
        $field->setDescription('description');
        $this->addField($field);
        if($this->configuration()->isDetailsCombinedView()){
            //disk
            $field = new Text('disk');
            $field->addValidator(new NumberValidator($this->resourceManager->disk()->getMin(), $this->resourceManager->disk()->free(),true));
            $field->setDescription('description');
            $this->addField($field);
        }
        /**
         * @deprecated
        //ipv4
        if($this->configuration()->isPermissionIpv4()){
            $field = new Text('ipv4');
            $field->addValidator(new NumberValidator($this->resourceManager->ipv4()->getMin(), $this->resourceManager->ipv4()->free(),false));
            $field->setDescription('description');
            $this->addField($field);
        }
        //ipv6
        if($this->configuration()->isPermissionIpv6()){
            $field = new Text('ipv6');
            $field->addValidator(new NumberValidator($this->resourceManager->ipv6()->getMin(), $this->resourceManager->ipv6()->free(),false));
            $field->setDescription('description');
            $this->addField($field);
        }
         *
         */
        //dnsdomain
        if($this->configuration()->isPermissionSearchdomain()){
             $field = new Text('searchdomain');
             $field->addValidator(new HostnameValidator(false));
             $field->setDescription('description');
             $this->addField($field);
        }
        if($this->configuration()->isPermissionNameservers()){
            //ns1
            $field = new Text('nameserver[0]');
            $field->addValidator(new IpAddressValidator());
            $field->setDescription('description');
            $this->addField($field);
            //ns2
            $field = new Text('nameserver[1]');
            $field->addValidator(new IpAddressValidator());
            $field->setDescription('description');
            $this->addField($field);
        }
        //boot device 1
        $field = new Select("bootOrder0");
        $field->notEmpty();
        $this->addField($field);
        //boot device 2
        $field = new Select("bootOrder1");
        $field->notEmpty();
        $this->addField($field);
        //boot device 3
        $field = new Select("bootOrder2");
        $field->notEmpty();
        $this->addField($field);
    }
}