<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (26.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\Pages;

use MGProvision\Proxmox\v2\repository\FirewallRulesRepository;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Firewall\Buttons\CreateGroupButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Firewall\Buttons\CreateRuleButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Firewall\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Firewall\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Firewall\Buttons\EnableSwitchButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Firewall\Buttons\RestoreButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Firewall\Buttons\UpdateRuleButton;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataTable;

class FirewallDataTable extends DataTable implements ClientArea
{
    use ApiService;
    use ProductService;
    protected $id = 'firewallDataTable';
    protected $name = 'firewallDataTable';
    protected $title = 'firewallDataTableTitle';

    public function initContent()
    {
        $this->unsetShowTitle();
        //Create Rule
        $createRule = new CreateRuleButton();
        //Create Group
        $createGroup = new CreateGroupButton();
        $createRule->addClass("pmCreateFirewallButton");
        $createGroup->addClass("pmCreateFirewallButton");
        if (!$this->resourceGuard()->hasfirewallLimit())
        {
            $createRule->addClass("hidden");
            $createGroup->addClass("hidden");
        }
        $this->addButton($createRule);
        $this->addButton($createGroup);
        //Update
        $this->addActionButton(new UpdateRuleButton());
        //Delete
        $this->addActionButton(new DeleteButton());
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('enable')))
            ->addColumn((new Column('type'))->setSearchable(true, "string")->setOrderable())
            ->addColumn((new Column('action'))->setSearchable(true, "string")->setOrderable())
            ->addColumn((new Column('iface'))->setSearchable(true, "string")->setOrderable())
            ->addColumn((new Column('comment'))->setSearchable(true, "string")->setOrderable());
    }

    public function customColumnHtmlEnable()
    {
        return (new EnableSwitchButton())->getHtml();
    }

    public function replaceFieldEnable($key, $row)
    {
        return $row[$key] == "1" ? "on" : "off";
    }

    public function replaceFieldMacro($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    public function replaceFieldIface($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    public function replaceFieldSource($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    public function replaceFieldDest($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    public function replaceFieldProto($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    public function replaceFieldDport($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    public function replaceFieldSport($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    public function replaceFieldComment($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    protected function loadData()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $repository = new FirewallRulesRepository();
        $repository->setApi($this->api());
        $repository->findByVm($vm);
        $data = [];
        foreach ($repository->fetch() as $entity)
        {
            $row    = $entity->toArray();
            $data[] = array_merge(['id' => base64_encode(json_encode($row))], $row);
        }
        $this->setCallBackFunction('pmToggleDropDownButtons');

        $dataProv = new ArrayDataProvider();
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


}