<?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\FirewallOption\Providers;

use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;


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

    public function read()
    {
        $this->data              = $this->vm()->firewallOptions()->read()->getAttributes();
        $this->data["enable"]    = str_replace(["1", "0"], ["on", "off"], $this->data["enable"]);
        $this->data["dhcp"]      = str_replace(["1", "0"], ["on", "off"], $this->data["dhcp"]);
        $this->data["ndp"]       = str_replace(["1", "0"], ["on", "off"], $this->data["ndp"]);
        $this->data["radv"]      = str_replace(["1", "0"], ["on", "off"], $this->data["radv"]);
        $this->data["macfilter"] = str_replace(["1", "0"], ["on", "off"], $this->data["macfilter"]);
        $this->data["ipfilter"]  = str_replace(["1", "0"], ["on", "off"], $this->data["ipfilter"]);
    }


    public function update()
    {
        $attributes              = $this->formData;
        $attributes["enable"]    = str_replace(["on", "off"], ["1", "0"], $attributes["enable"]);
        if(!$this->acl()->hasFirewallOption("enable")){
            unset($attributes["enable"] );
        }
        $attributes["dhcp"]      = str_replace(["on", "off"], ["1", "0"], $attributes["dhcp"]);
        if(!$this->acl()->hasFirewallOption("dhcp")){
            unset($attributes["dhcp"] );
        }
        $attributes["ndp"]       = str_replace(["on", "off"], ["1", "0"], $attributes["ndp"]);
        if(!$this->acl()->hasFirewallOption("ndp")){
            unset($attributes["ndp"] );
        }
        $attributes["radv"]      = str_replace(["on", "off"], ["1", "0"], $attributes["radv"]);
        if(!$this->acl()->hasFirewallOption("radv")){
            unset($attributes["radv"] );
        }
        $attributes["macfilter"] = str_replace(["on", "off"], ["1", "0"], $attributes["macfilter"]);
        if(!$this->acl()->hasFirewallOption("macfilter")){
            unset($attributes["macfilter"] );
        }
        $attributes["ipfilter"]  = str_replace(["on", "off"], ["1", "0"], $attributes["ipfilter"]);
        if(!$this->acl()->hasFirewallOption("ipfilter")){
            unset($attributes["ipfilter"] );
        }
        $this->vm()->firewallOptions()->setAttributes($attributes)->update();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The firewall options has been updated successfully')
            ->setRefreshTargetIds(["firewallOption"]);
    }


}