<?php


namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Forms;

use MGProvision\Proxmox\v2\models\Kvm;
use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\JobPeriod;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\SnapshotValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\StartTimeValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Hidden;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Number;
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\Textarea;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

trait JobForm
{

    public function setJobConfirmMessage(){
        $limit = $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOTS, $this->configuration()->getSnapshotMaxFiles());
        if ($limit && $limit != "-1")
        {
            sl("lang")->addReplacementConstant("snapshotFilesLimit" , $limit);
            $this->setInternalAlertMessage( sl("lang")->tr('Your snapshot limit is :snapshotFilesLimit:. When you exceed this limit, the last snapshot will be replaced with a new one.'));
            $this->setInternalAlertMessageRaw(true);
        }
    }

    protected function initFields()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $formData = $this->getRequestValue("formData");
        //id
        $id = false;
        if(is_numeric($this->getRequestValue("actionElementId"))){
            $id = $this->getRequestValue("actionElementId");
        }
        if ($formData && $formData['id']){
            $id =  $formData['id'];
        }
        //hourly
        $isHourly = true;
        if($formData &&  $formData['period'] ){
            $isHourly = $formData['period'] == JobPeriod::HOURLY;
        } elseif (!$formData && $id){
            $this->loadProvider();
            $this->dataProvider->read();
            $isHourly = $this->dataProvider->getValueById('period') == JobPeriod::HOURLY ;
        }elseif (!$formData && !$id){
            $period = $this->configuration()->getPermissionSnapshotJobPeriod();
            if($period && in_array(JobPeriod::HOURLY, $period)){
                $isHourly = true;
            }elseif ($period ){
                $isHourly = false;
            }
        }
        //id
        if(is_numeric($this->getRequestValue("actionElementId"))){
            $this->addField(new Hidden("id"));
        }
        //period
        $field = new Select('period');
        $field->addHtmlAttribute('bi-event-change', "reloadVueModal");
        $this->addField($field);
        //run_every
        if($isHourly){
            $field = new Number('run_every',1,48);
            $field->setPlaceholder("1");
            $this->addField($field);
        }
        //days
        if(!$isHourly){
            $field = new Select('days');
            $field->enableMultiple();
            $this->addField($field);
        }
        //start_time
        if(!$isHourly){
            $field = new Text('start_time');
            $field->setPlaceholder("00:00");
            $field->addValidator(new StartTimeValidator(false));
            $this->addField($field);
        }
        //name
        $field = new Text('name');
        $field->notEmpty();
        $field->addValidator(new SnapshotValidator(true));
        $this->addField($field);
        //vmstate
        if ($vm instanceof Kvm)
        {
            $field = new Switcher("vmstate");
            $this->addField($field);
        }
        //description
        $field = new Textarea("description");
        $this->addField($field);

    }
}