<?php
/**********************************************************************
 * ProxmoxVPS developed. (26.03.19)
 * *
 *
 *  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
 *  CONTACT                        ->       contact@modulesgarden.com
 *
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  with  the  terms  of such  license and with the
 * inclusion of the above copyright notice.  This software  or any other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 *
 **********************************************************************/

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Forms;

use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Buttons\VirtualizationChangButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Providers\ProductProvider;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\AdminNotificationSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\BackupSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\ClientNotificationSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\ConsoleSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\CpuPrioritySection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\FirewallOptionSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\FirewallSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\HighAvailabilityClusterSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\LoadBalancerSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\Lxc;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\MainSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\MiscellaneousSection;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\Qemu;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Sections\UserSection;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\MainContainer;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Switcher;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\FormIntegration;

class MainForm extends FormIntegration implements AdminArea
{

    public function initContent()
    {
       /**
         *
         */
        $this->initIds('mainForm');
        $this->setProvider(new ProductProvider($this->getRequestValue('id')));
        $this->addSection(new VirtualizationChangButton());
        $this->dataProvider->initData();
        /**
         *
         */
        $isQemu = !$this->dataProvider->getValueById('customconfigoption[virtualization]') || $this->dataProvider->getValueById('customconfigoption[virtualization]') == "qemu";
        /**
         * Main Section
         */
        $section = new MainSection();
        $section->setMainContainer($this->mainContainer);
        $this->addSection($section);

        if ($isQemu)
        {
            // KVM
            $this->addSection((new Qemu\DefaultConfigurationSection())->enableToggler())
                ->addSection((new Qemu\ServerSection())->enableToggler())
                ->addSection((new Qemu\NetworkSection())->enableToggler())
                ->addSection((new Qemu\DiskSection())->enableToggler())
                ->addSection((new Qemu\DiskSpeedSection())->enableTogglerAndHide())
                ->addSection((new Qemu\AdditonalDisk())->enableToggler())
                ->addSection((new Qemu\AdditonalDiskSpeedSection())->enableToggler())
                ->addSection((new BackupSection())->enableToggler())
                ->addSection((new Qemu\BootSection())->enableTogglerAndHide())
                ->addSection((new FirewallSection())->enableTogglerAndHide())
                ->addSection((new FirewallOptionSection())->enableTogglerAndHide())
                ->addSection((new Qemu\CloudInitSection())->enableTogglerAndHide())
                ->addSection((new Qemu\GuestAgentSection())->enableTogglerAndHide())
                ->addSection((new UserSection())->enableTogglerAndHide())
                ->addSection((new MiscellaneousSection())->enableTogglerAndHide())
                ->addSection((new LoadBalancerSection())->enableTogglerAndHide())
                ->addSection((new Qemu\ConfigurationSection())->enableTogglerAndHide())
                ->addSection((new Qemu\AdvancedSection())->enableTogglerAndHide())
                ->addSection((new HighAvailabilityClusterSection())->enableTogglerAndHide())
                ->addSection((new ConsoleSection())->enableTogglerAndHide())
                ->addSection((new Qemu\ClientAreaSection())->enableToggler())
                ->addSection((new Qemu\ConfigurableOptionUnitsSection())->enableToggler());
        }
        else
        {
            //LXC
            $this->addSection((new Lxc\DefaultConfigurationSection())->enableToggler())
                ->addSection((new Lxc\ServerSection())->enableToggler())
                ->addSection((new Lxc\MountPointSection())->enableToggler())
                ->addSection((new BackupSection())->enableToggler())
                ->addSection((new Lxc\NetworkSection())->enableToggler())
                ->addSection((new FirewallSection())->enableTogglerAndHide())
                ->addSection((new FirewallOptionSection())->enableTogglerAndHide())
                ->addSection((new UserSection())->enableTogglerAndHide())
                ->addSection((new MiscellaneousSection())->enableTogglerAndHide())
                ->addSection((new LoadBalancerSection())->enableTogglerAndHide())
                ->addSection((new Lxc\ConfigurationSection())->enableTogglerAndHide())
                ->addSection((new HighAvailabilityClusterSection())->enableTogglerAndHide())
                ->addSection((new ConsoleSection())->enableTogglerAndHide())
                ->addSection((new Lxc\ClientAreaSection())->enableToggler())
                ->addSection((new Lxc\ConfigurableOptionUnitsSection())->enableToggler());
        }
        $this->addSection((new CpuPrioritySection())->enableToggler());
        $this->loadDataToForm();
    }

    public function getSwitcherFields()
    {
        $this->setMainContainer(new MainContainer());
        $this->runInitContentProcess();
        $fieldNames = [];
        foreach ($this->getSections() as $section)
        {
            foreach ($section->getSections() as $s)
            {
                foreach ($s->getFields() as $field)
                {
                    if (!$field instanceof Switcher)
                    {
                        continue;
                    }
                    $name         = str_replace(["customconfigoption[", "]"], ["", ""], $field->getName());
                    $fieldNames[] = $name;
                }
            }
        }
        return array_unique($fieldNames);
    }
}