<?php

/* * ********************************************************************
 * ProxmoxAddon product developed. (Oct 1, 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\Servers\ProxmoxCloudVps\App\UI\ServiceGraph\Pages;

use ModulesGarden\ProxmoxAddon\App\Models\RrdData;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Graphs\Line;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Graphs\Models\DataSet;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class ServiceNetworkGraph extends BaseGraph
{
    use ProductService;

    protected $id = 'serviceNetworkGraph';
    protected $name = 'serviceNetworkGraph';

    public function initContent()
    {
        $this->updateChartScale('yAxes', [
            [
                'scaleLabel' => [
                    'display'     => true,
                    'labelString' => sl('lang')->tr('Network')
                ],
                'ticks'      => [
                    'callback'    => 'mgBytesToSize',
                    'beginAtZero' => true,
                    'fontSize' => 10
                ],
            ]]);
        //xAxes
        $this->updateChartScale('xAxes', [
            [
                'display' => false,
            ]]);

        $this->updateChartOption('tooltips', [
            'callbacks' => [
                'label' => 'mgTooltipCallbackForNet'
            ]
        ]);
    }

    public function prepareAjaxData()
    {
        session_write_close();
        $labels   = [];
        $dataSets = [
            'netin'  => [],
            'netout' => [],
        ];
        foreach ($this->rrdDataQuery() as $rrd)
        {
            $labels[]             = $rrd->date;
            $dataSets['netin'][]  = isset($rrd->netin) ? $rrd->netin : 0;
            $dataSets['netout'][] = isset($rrd->netout) ? $rrd->netout : 0;
        }
        //Labels
        $this->setLabels($labels);
        //Net in
        $lang    = sl('lang');
        $dataSet = new DataSet();
        $dataSet->setTitle($lang->tr('Net In'))
            ->setData($dataSets['netin'])
            ->setConfigurationDataSet([
                "backgroundColor" => 'rgba(174, 198, 57, 0.79)',
                "borderColor"     => 'rgba(174, 198, 57, 1)',
            ]);
        $this->addDataSet($dataSet);
        //Net out
        $dataSet = new DataSet();
        $dataSet->setTitle($lang->tr('Net Out'))
            ->setData($dataSets['netout'])
            ->setConfigurationDataSet([
                "backgroundColor" => 'rgba(39, 133, 134, 0.91)',
                "borderColor"     => 'rgba(39, 133, 134, 1)',
            ]);
        $this->addDataSet($dataSet);
    }

}
