<?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\ProxmoxVps\App\UI\Network\Pages;

use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Network\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Network\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Network\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Network\Buttons\InfoButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Network\Buttons\RestoreButton;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataTable;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;

class NetworkQemuDataTable extends DataTable implements ClientArea
{
    use ProductService;
    use ApiService;
    protected $id = 'networkQemuDataTable';
    protected $title = 'networkQemuDataTable';

    public function initContent()
    {
        //Create
        $createButton = new CreateButton();
        $createButton->addClass("pmCreateButton");
        if (!$this->configuration()->getPrivateBridge()  || $this->vm()->getNetworkDevices($this->configuration()->getPrivateBridge()))
        {
            $createButton->addClass("hidden");
        }
        $this->addButton($createButton);
        //Info
        $this->addActionButton(new InfoButton());
        //Delete
        $this->addActionButton(new DeleteButton());
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('id'))->setSearchable(true, "string")->setOrderable('ASC'))
            ->addColumn((new Column('firewall'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('macAddress'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('ip'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('gw'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('rate'))->setSearchable(true)->setOrderable());
    }

    public function replaceFieldFirewall($key, $row)
    {
        if ($row['firewall'])
        {
            return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr("Active") . '</span>';
        }
        return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr("Disabled") . '</span>';
    }

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

    public function replaceFieldIp($key, $row)
    {
        if (!$row['ip'] && !$row['ip6'])
        {
            return '-';
        }
        $ip = [];
        if ($row['ip'] && $row['cidr'])
        {
            $ip[] = sprintf("%s/%s", $row['ip'], $row['cidr']);
        }
        else
        {
            if ($row['ip'])
            {
                $ip[] = $row['ip'];
            }
        }
        if ($row['ip6'] && $row['cidr6'])
        {
            $ip[] = sprintf("%s/%s", $row['ip6'], $row['cidr6']);
        }
        else
        {
            if ($row['ip6'])
            {
                $ip[] = $row['ip6'];
            }
        }
        return implode("<br/>", $ip);
    }

    public function replaceFieldGw($key, $row)
    {
        if ($row['gw'] && $row['gw6'])
        {
            return sprintf('%s<br>%s', $row['gw'], $row['gw6']);
        }
        if ($row['gw'])
        {
            return $row['gw'];
        }
        if ($row['gw6'])
        {
            return $row['gw6'];
        }
        return "-";
    }

    public function replaceFieldRate($key, $row)
    {
        if (isset($row['rate']))
        {
            return sprintf('%s MB/s %s (Mbps)', $row['rate'], $row['rate'] * 8);
        }
        return sl('lang')->abtr("Unlimited");
    }

    protected function loadData()
    {
        $data = [];
        foreach ($this->vm()->getNetworkDevices() as $nd)
        {
            $ipConfig = $this->vm()->getIpConfig()->findByNetworkId($nd->getId())->fetch()[0];
            if ($ipConfig)
            {
                $data[] = array_merge($nd->getAttributes(), $ipConfig->getAttributes());
            }
            else
            {
                $data[] = $nd->getAttributes();
            }
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting("id", 'ASC');
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


}