<?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\ProxmoxCloudVps\App\UI\Firewall\Providers;

use MGProvision\Proxmox\v2\models\FirewallRule;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;


class FirewallProvider extends BaseDataProvider implements ClientArea
{
    use ProductService;
    use ApiService;

    public function read()
    {
        if ($this->actionElementId)
        {
            $this->data = json_decode(base64_decode($this->actionElementId), true);
            //enable
            $enable               = $this->data['enable'];
            $this->data['enable'] = $enable == "1" ? "on" : "off";
        }
    }

    public function create()
    {
        $this->acl()->firewall();
        $this->resourceGuard()->firewallLimit();
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $firewallRule = new FirewallRule();
        $firewallRule->setApi($this->api());
        $firewallRule->setPath($vm->getPath() . "/firewall/rules/");
        $attributes           = $this->formData;
        $attributes['enable'] = $this->formData['enable'] == "on" ? 1 : 0;
        if ($attributes['proto'] == "0")
        {
            unset($attributes['proto']);
        }
        $firewallRule->setAttributes($attributes);
        $firewallRule->create();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The firewall rule has been created successfully')
            ->addData('createButtonStatus', $this->resourceGuard()->hasfirewallLimit())
            ->setCallBackFunction('pmToggleFirewallButton');
    }

    public function update()
    {
        $this->acl()->firewall();
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $firewallRule = new FirewallRule();
        $firewallRule->setApi($this->api());
        $pos = $this->formData['pos'];
        unset($this->formData['pos']);
        $firewallRule->setPath($vm->getPath() . "/firewall/rules/{$pos}");
        $attributes           = $this->formData;
        $attributes['enable'] = $this->formData['enable'] == "on" ? 1 : 0;
        if ($attributes['proto'] == "0")
        {
            unset($attributes['proto']);
        }
        $firewallRule->setAttributes($attributes);
        $firewallRule->update();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The firewall rule has been updated successfully');
    }

    public function delete()
    {
        $this->acl()->firewall();
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $firewallRule = new FirewallRule();
        $firewallRule->setApi($this->api());
        $pos = $this->formData['pos'];
        $firewallRule->setPath($vm->getPath() . "/firewall/rules/{$pos}");
        $firewallRule->delete();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The firewall rule has been deleted successfully')
            ->addData('createButtonStatus', $this->resourceGuard()->hasfirewallLimit())
            ->setCallBackFunction('pmToggleFirewallButton');
    }


}