<?php

/* * ********************************************************************
 * ProxmoxAddon product developed. (Sep 12, 2018)
 * *
 *
 *  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\Providers;

use ModulesGarden\ProxmoxAddon as main;

/**
 * Description of SettingProvider
 *
 * @author Pawel Kopec <pawelk@modulesgardne.com>
 */
class SettingProvider extends main\Core\UI\Widget\Forms\DataProviders\BaseDataProvider
{

    public function read()
    {
        $this->data                          = [];
        //console proxy
        $moduleSettings = new main\App\Repositories\ModuleSettingRepository();
        $this->data = $moduleSettings->all();

        $this->data['proxmoxVPSMinimumVMID'] = main\Core\Models\Whmcs\Configuration::where("setting", "proxmoxVPSMinimumVMID")->value('value');
        $this->data['vmsWeight']             = main\Core\Models\ModuleSettings\Model::where("setting", 'vmsWeight')->value("value");
        $this->data['cpuWeight']             = main\Core\Models\ModuleSettings\Model::where("setting", "cpuWeight")->value("value");
        $this->data['diskWeight']            = main\Core\Models\ModuleSettings\Model::where("setting", 'diskWeight')->value("value");
        $this->data['ramWeight']             = main\Core\Models\ModuleSettings\Model::where("setting", 'ramWeight')->value("value");
        $this->data['debug'] = main\Core\Models\ModuleSettings\Model::where("setting", 'debug')->value("value");


    }

    public function update()
    {
        if($this->formData['consoleHost'] && !$this->formData['consoleApiKey'] ){
            return (new main\Core\UI\ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('The  API Key field cannot be empty')
                ->setStatusError()
                ->setCallBackFunction($this->callBackFunction);
        }
        if(main\Core\Models\Whmcs\Configuration::where("setting", "proxmoxVPSMinimumVMID")->count()){
            main\Core\Models\Whmcs\Configuration::where("setting", "proxmoxVPSMinimumVMID")
                ->update(['value' => $this->formData['proxmoxVPSMinimumVMID']]);
        }else{
            main\Core\Models\Whmcs\Configuration::where("setting", "proxmoxVPSMinimumVMID")
                ->insert(["setting" => "proxmoxVPSMinimumVMID",'value' => $this->formData['proxmoxVPSMinimumVMID']]);
        }
        $filable = ["vmsWeight", "cpuWeight", "diskWeight", "ramWeight", 'debug'];
        foreach ($filable as $key)
        {
            $entity = main\Core\Models\ModuleSettings\Model::where("setting", $key)->first();
            if (!$entity)
            {
                $entity = new main\Core\Models\ModuleSettings\Model();
            }
            $entity->fill(['setting'=>  $key, "value" =>  $this->formData[$key]])
                   ->save();
        }
        //console proxy
        $moduleSettings = new main\App\Repositories\ModuleSettingRepository();
        $moduleSettings->fillAndSave($this->formData);
        return (new main\Core\UI\ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('changesHasBeenSaved')
            ->setStatusSuccess()
            ->setCallBackFunction($this->callBackFunction);
    }
}
