<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (27.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\ProxmoxVps\App\UI\BackupJob\Forms;

use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\App\Enum\ConfigurableOption;
use ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Providers\BackupJobProvider;
use ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Providers\FirewallOptionProvider;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Validators\StartTimeValidator;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Helpers\AlertTypesConstants;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\BaseForm;
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 function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;


class CreateForm extends BaseForm implements ClientArea
{
    use ProductService;

    public function initContent()
    {
        $this->initIds('createForm');
        $this->setFormType('create');
        $this->setProvider(new BackupJobProvider());
        $backupFilesLimit = $this->configuration()->getBackupMaxFiles();
        if ($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) &&
            $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) != "-1")
        {
            $backupFilesLimit = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
        }
        if($backupFilesLimit == "-1"){
            $backupFilesLimit = null;
        }
        if ($this->configuration()->isBackupRouting() && $backupFilesLimit)
        {
            sl("lang")->addReplacementConstant("backupFilesLimit" , $backupFilesLimit);
            $this->setInternalAlertMessage( sl("lang")->tr('Your routing backup limit is :backupFilesLimit:. When you exceed this limit, the last backup will be replaced with a new one.'));
            $this->setInternalAlertMessageRaw(true);
            $this->setInternalAlertMessageType(AlertTypesConstants::INFO);
        }
        $this->initFields();
        $this->loadDataToForm();
    }

    public function getAllowedActions()
    {
        return ['create'];
    }

    private function initFields()
    {
        //starttime
        $field = new Text('starttime');
        $field->setPlaceholder("00:00");
        $field->addValidator(new StartTimeValidator());
        $this->addField($field);
        //dow
        $field = new Select('dow');
        $field->enableMultiple();
        $field->setAvailableValues([
            "mon" => sl("lang")->tr("mon"),
            "tue" => sl("lang")->tr("tue"),
            "wed" => sl("lang")->tr("wed"),
            "thu" => sl("lang")->tr("thu"),
            "fri" => sl("lang")->tr("fri"),
            "sat" => sl("lang")->tr("sat"),
            "sun" => sl("lang")->tr("sun")
        ]);
        $field->notEmpty();
        $this->addField($field);
        //compress
        $field = new Select('compress');
        $field->setAvailableValues([
            "0"    => sl("lang")->tr("0"),
            "lzo"  => sl("lang")->tr("lzo"),
            "gzip" => sl("lang")->tr("gzip"),
            "zstd" => sl("lang")->abtr("ZSTD (fast and good)")
        ]);
        $field->setDefaultValue('zstd');
        $field->notEmpty();
        $this->addField($field);
        //mode
        $field = new Select('mode');
        $field->setAvailableValues([
            "snapshot" => sl("lang")->tr("snapshot"),
            "suspend"  => sl("lang")->tr("suspend"),
            "stop"     => sl("lang")->tr("stop")
        ]);
        $field->notEmpty();
        $this->addField($field);
        //mailto
        $field = new Switcher("mailto");
        $this->addField($field);

    }
}