<?php

/* * ********************************************************************
 * Wordpress_Manager Product developed. (Dec 7, 2017)
 * *
 *
 *  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\ProxmoxAddon\App\UI\Settings\Pages;

use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\App\UI\Settings\Providers\SettingProvider;
use ModulesGarden\ProxmoxAddon\App\UI\Settings\Sections\CronSection;
use ModulesGarden\ProxmoxAddon\App\UI\Settings\Sections\GeneralSection;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\BaseStandaloneFormExtSections;

/**
 * SettingsContainer
 */
class SettingsContainer extends BaseStandaloneFormExtSections implements AdminArea
{
    protected $id = 'settings';
    protected $name = 'settings';
    protected $title = null;

    public function initContent()
    {
        //SettingProvider
        $this->setProvider(new SettingProvider());
        //alert
        $this->licenseAlert();
        //Crons
        $this->addSection(new CronSection('cron'));
        //General
        $general = new GeneralSection('general');
        //LoadBalancer
        $loadBalancer = new main\App\UI\Settings\Sections\LoadBalancerSection('loadBalancer');
        //Minimum VMID
        $field = new main\Core\UI\Widget\Forms\Fields\Text('proxmoxVPSMinimumVMID');
        $field->addValidator(new main\App\UI\Validators\NumberValidator(1, 100000000 * 100000000 * 100000000));
        $field->setLabelWidth(12);
        $field->setDefaultValue(100);
        $general->addField($field);
        //debug
        $field = new main\Core\UI\Widget\Forms\Fields\Switcher('debug');
        $field->setDescription('description');
        $general->addField($field);
        //Count VMs
        $field = new main\Core\UI\Widget\Forms\Fields\Text('vmsWeight');
        $field->addValidator(new main\App\UI\Validators\NumberValidator(1, null));
        $field->setLabelWidth(12);
        $field->setDescription('description');
        $field->setDefaultValue(1000);
        $loadBalancer->addField($field);
        //Weight CPU
        $field = new main\Core\UI\Widget\Forms\Fields\Text('cpuWeight');
        $field->addValidator(new main\App\UI\Validators\NumberValidator(1, null));
        $field->setLabelWidth(12);
        $field->setDescription('description');
        $field->setDefaultValue(1);
        $loadBalancer->addField($field);
        //Weight Disk
        $field = new main\Core\UI\Widget\Forms\Fields\Text('diskWeight');
        $field->addValidator(new main\App\UI\Validators\NumberValidator(1, null));
        $field->setLabelWidth(12);
        $field->setDescription('description');
        $field->setDefaultValue(1);
        $loadBalancer->addField($field);
        //Weight RAM
        $field = new main\Core\UI\Widget\Forms\Fields\Text('ramWeight');
        $field->addValidator(new main\App\UI\Validators\NumberValidator(1, null));
        $field->setLabelWidth(12);
        $field->setDescription('description');
        $field->setDefaultValue(1);
        $loadBalancer->addField($field);
        //Section
        $this->addSection($general);
        $this->addSection($loadBalancer);
        //console
        $consoleSection = new main\App\UI\Settings\Sections\ConsoleSection();
        $consoleSection->setMainContainer($this->mainContainer);
        $this->addSection($consoleSection);
        //adminArea
        $adminAreaSection = new main\App\UI\Settings\Sections\AdminAreaSection();
        $adminAreaSection->setMainContainer($this->mainContainer);
        $this->addSection($adminAreaSection);
        $this->loadDataToForm();
    }

    public function licenseAlert(){
        $vps = $this->isLicensePayingFullAnnuallyPrice('proxmoxVPS');
        $cloud = $this->isLicensePayingFullAnnuallyPrice('ProxmoxCloudVps');
        if((is_bool($vps) && !$vps) || (is_bool($cloud) && !$cloud) ){
            $this->addSection(new main\App\UI\Settings\Sections\AlertSection("You can use proxy for Proxmox console connections to increase the security of your infrastructure. Please contact <a href='https://www.modulesgarden.com/support/ticket/product-presales/Proxmox%20module%20upgrade%20request%20-%20Proxy%20console' target='_blank'>ModulesGarden support</a> to get more details and upgrade your module."));
        }
    }

    private function isLicensePayingFullAnnuallyPrice($module='proxmoxVPS'){
        $file   = \ModulesGarden\ProxmoxAddon\Core\ModuleConstants::getFullPathWhmcs('modules', 'servers',$module) . DIRECTORY_SEPARATOR .$module. '.php';
        if(!file_exists($file))
        {
            return;
        }
        require_once $file;
        $moduleFunction = $module."_GetLicenseData";
        if(!function_exists($moduleFunction)){
            return true;
        }
        $licenseData = $moduleFunction(true);
        if(is_array($licenseData) && isset($licenseData['isPayingFullAnnuallyPrice']) && $licenseData['isPayingFullAnnuallyPrice'] !="1"){
            return false;
        }
        return true;
    }
}
