<?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\BaseField;
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\Fields\Text;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Textarea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Sections\BoxSection;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Sections\HalfPageSection;

class CloudInitSection extends BoxSection implements AdminArea
{
    protected $id = 'cloudInitSection';
    protected $name = 'cloudInitSection';
    protected $title = 'cloudInitSection';
    private $sectionFields=[];
    /**
     * @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()
    {
        //Enable Cloud-Init
        $field = new Switcher('customconfigoption[cloudInit]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->addSectionField($field);
        //Service Username
        $field = new Switcher('customconfigoption[cloudInitServiceUsername]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->addSectionField($field);
        //Service Password
        $field = new Switcher('customconfigoption[cloudInitServicePassword]');
        $field->setDescription('tip');
        $field->setDefaultValue("on");
        $this->addSectionField($field);
        //Service Nameservers
        $field = new Switcher('customconfigoption[cloudInitServiceNameservers]');
        $field->setDescription('tip');
        $this->addSectionField($field);
        //DNS Domain
        $field = new Text('customconfigoption[searchdomain]');
        $field->setDescription('tip');
        $this->addSectionField($field);
        //Default User
        $field = new Text('customconfigoption[ciuser]');
        $field->setDescription('tip');
        $this->addSectionField($field);
        //cicustom
        $field = new Textarea('customconfigoption[cicustom]');
        $field->setDescription('tip');
        $this->addSectionField($field);
        //cloudInitScript
        $field = new Select('customconfigoption[cloudInitScript]');
        $field->setDescription('tip');
        $this->addSectionField($field);
        $this->addFieldsToSections();
    }

    public function addSectionField(BaseField $field)
    {
        $this->sectionFields[] = $field;
        return $this;
    }

    private function addFieldsToSections(){
        foreach($this->sectionFields as $k => $field){
            if($k % 2 == 0 ){
                $this->leftSection->addField($field);
            }else{
                $this->rightSection->addField($field);
            }
        }
        unset($this->sectionFields);
    }

}
