<?php

namespace ModulesGarden\Servers\ProxmoxVps\App\UI\Admin\Product\Sections\Qemu;

use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Switcher;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Sections\BoxSection;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Sections\HalfPageSection;

class ClientAreaSection extends BoxSection implements AdminArea
{
    protected $id = 'clientAreaSection';
    protected $name = 'clientAreaSection';
    protected $title = 'clientAreaSection';
    /**
     * @var HalfPageSection
     */
    private $leftSection;
    /**
     * @var HalfPageSection
     */
    private $rightSection;

    public function initContent()
    {
        $this->leftSection  = new HalfPageSection('leftSection');
        $this->rightSection = new HalfPageSection('rightSection');
        $this->addSection($this->leftSection)
            ->addSection($this->rightSection);
        $this->initFields();
    }

    private function initFields()
    {
        //Start
        $field = new Switcher('customconfigoption[permissionStart]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //Reboot
        $field = new Switcher('customconfigoption[permissionReboot]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //Stop
        $field = new Switcher('customconfigoption[permissionStop]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //Shutdown
        $field = new Switcher('customconfigoption[permissionShutdown]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //noVNC Console
        $field = new Switcher('customconfigoption[permissionNovnc]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //SPICE Console
        $field = new Switcher('customconfigoption[permissionSpice]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //xterm.js Console
        $field = new Switcher('customconfigoption[permissionXtermjs]');
        $field->setDescription('tip');
        $this->leftSection->addField($field);
        //Reinstallation
        $field = new Switcher('customconfigoption[permissionReinstall]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //KVM Templates
        $field = new Switcher('customconfigoption[permissionOsTemplate]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //OS Templates
        $field = new Select('customconfigoption[permissionOsTemplates][]');
        $field->setDescription('tip');
        $field->enableMultiple();
        $this->rightSection->addField($field);
        //ISO Images
        $field = new Switcher('customconfigoption[permissionIsoImage]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //ISO Images
        $field = new Select('customconfigoption[permissionIsoImages][]');
        $field->setDescription('tip');
        $field->enableMultiple();
        $this->leftSection->addField($field);
        //MRTG Graphics
        $field = new Switcher('customconfigoption[permissionGraph]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //Backup
        $field = new Switcher('customconfigoption[permissionBackup]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //Scheduled Backup Jobs
        $field = new Switcher('customconfigoption[permissionBackupJob]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //Task History
        $field = new Switcher('customconfigoption[permissionTaskHistory]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //Network
        $field = new Switcher('customconfigoption[permissionNetwork]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //Snapshots
        $field = new Switcher('customconfigoption[permissionSnapshot]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //Firewall
        $field = new Switcher('customconfigoption[permissionFirewall]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //Firewall Options
        $field = new Switcher('customconfigoption[permissionFirewallOption]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //Disks Management
        $field = new Switcher('customconfigoption[permissionDisk]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //sshkeys
        $field = new Switcher('customconfigoption[permissionSshkeys]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->rightSection->addField($field);
        //Snapshot Jobs
        $field = new Switcher('customconfigoption[permissionSnapshotJob]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->leftSection->addField($field);
        //How Often
        $field = new Select('customconfigoption[permissionSnapshotJobPeriod][]');
        $field->setDescription('tip');
        $field->enableMultiple();
        $this->rightSection->addField($field);
        //permissionfirewalOptions
        $field = new Select("customconfigoption[permissionFirewalOptions][]");
        $field->enableMultiple();
        $field->setDescription('description');
        $this->addField($field);
    }

    public function addField($field){
        $total = count($this->leftSection->getFields()) + count($this->rightSection->getFields());
        if($total % 2 == 0){
            $this->leftSection->addField($field);
        }else{
            $this->rightSection->addField($field);
        }
        return $this;
    }
}
