<?php

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

use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
use ModulesGarden\ProxmoxAddon\Core\Helper\AdvancedUserHelper;
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\Fields\IsoImageSelect;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Fields\OsTemplateSelect;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Fields\SecondaryIsoImageSelect;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators\DiskSizeValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators\IsoImageValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators\NameserverValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators\PasswordValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators\SearchdomainValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators\UserValidator;
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 ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Sections\HalfPageSection;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class GeneralSection extends HalfPageSection
{
    use ProductService;

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

    public function initContent()   {
        $this->initIds('generalSection');
        $this->resourceManager = new ResourceManager();
        $this->initFields();
    }

    public function initFields()  {
        $advancedUser = new AdvancedUserHelper($this->getWhmcsParamByKey('userid'));
        /**
         * @deprecated
        //name
        $field = new Text('name');
        $field->setDescription('description');
        $field->addValidator(new HostnameValidator());
        $this->addField($field);
        //description
        $field = new Textarea('description');
        $field->setDescription('tip');
        $this->addField($field);
         //osTemplate
        if($this->configuration()->isPermissionOsTemplate()){
        $field =new OsTemplateSelect('osTemplate');
        $field->notEmpty();
        $this->addField( $field);
        }
         */
        $cpuPrioryty = $this->configuration()->hasCpuPriority();
        //location
        if($this->configuration()->getLocations()){
            $field = new Select('location');
            $field->notEmpty();
            $field->setDescription('description');
            $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->setDefaultValue($this->configuration()->serverSockets->min);
            $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->setDefaultValue($this->configuration()->serverCores->min);
            $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->setDefaultValue($this->configuration()->serverVcpus->min);
            $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->setDefaultValue($this->resourceManager->cpuPriority()->free() >= 3 ? 3 : 1);
            $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"));
            $field->setDescription('description');
            $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->setDefaultValue($this->configuration()->serverCpulimit->min);
            $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->setDefaultValue($this->configuration()->serverCpuunit->min);
            $field->setDescription('description');
            $this->addField($field);
        }
        //memory
        $field = new Range('memory', $this->resourceManager->memory()->getMin(), $this->resourceManager->memory()->free());
        $field->addValidator(new NumberValidator($this->resourceManager->memory()->getMin(), $this->resourceManager->memory()->free(),true));
        $field->setDefaultValue($this->configuration()->serverMemory->min);
        $field->setDescription('description');
        $this->addField($field);
        //disk
        $field = new Range('disk', $this->configuration()->serverDiskSize->min, $this->resourceManager->disk()->free());
        $field->addValidator(new DiskSizeValidator('additionalDiskSize',true));
        $field->setDefaultValue($this->configuration()->serverDiskSize->min);
        $field->setDescription('description');
        $this->addField($field);
        //username
        if($this->configuration()->isPermissionUsername()){
            $field = new Text('ciuser');
            $field->addValidator(new UserValidator());
            $field->setDefaultValue($this->configuration()->getCiuser());
            $field->setDescription('description');
            $this->addField($field);
        }
        //password
        if($this->configuration()->isPermissionPassword()){
            $field = new Text('password');
            $field->addValidator( new PasswordValidator());
            $field->setDescription('description');
            $this->addField($field);
        }
        //sshkey
        if($this->configuration()->isPermissionSshkeys()){
            $field = new Textarea('sshkeys');
            $field->addValidator(new SshPublicKeyValidator(false));
            $field->setDescription('description');
            $this->addField($field);
        }
        //dnsdomain
        if($this->configuration()->isPermissionSearchdomain()){
             $field = new Text('searchdomain');
             $field->addValidator(new SearchdomainValidator(false));
             $field->setDescription('description');
             $this->addField($field);
        }
        if($this->configuration()->isPermissionNameservers()){
            //ns1
            $field = new Text('nameserver[0]');
            $field->addValidator(new NameserverValidator());
            $field->setDescription('description');
            $this->addField($field);
            //ns2
            $field = new Text('nameserver[1]');
            $field->addValidator(new NameserverValidator());
            $field->setDescription('description');
            $this->addField($field);
        }
        //ostype
        if(($advancedUser->isAdvanced() || $this->configuration()->isPermissionIsoImage()) && $this->configuration()->getPermissionOstype()){
            $field = new  Select('ostype');
            $this->addField($field);
        }
        //isoImage
        if($this->configuration()->isPermissionIsoImage() && ($advancedUser->isAdvanced() || $this->configuration()->isPermissionIsoImage())){
            $field = new IsoImageSelect('isoImage');
            $field->addValidator(new IsoImageValidator());
            $this->addField($field);
        }
        //secondaryIsoImage
        if($this->configuration()->isPermissionSecondaryIsoImages() && $advancedUser->isAdvanced()){
            $this->addField(new SecondaryIsoImageSelect('secondaryIsoImage'));

        }
        //cloudInitScript
        if($this->configuration()->getCloudInitScript()){
            $field = new Select('cloudInitScript');
            $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->setDefaultValue($this->configuration()->serverIpv4->min);
            $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->setDefaultValue($this->configuration()->serverIpv6->min);
            $field->setDescription('description');
            $this->addField($field);
        }
         */
    }
}