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

use ModulesGarden\ProxmoxAddon\Core\Helper\DatabaseCache;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Graphs\Line;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Graphs\Models\DataSet;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;


class NetworkGraph extends Line implements ClientArea, AdminArea
{
    use GraphData;
    protected $id = 'networkGraph';
    protected $name = 'networkGraph';
    protected $graphSettingsEnabled = true;
    protected $graphSettingsKey = 'networkGraphSettings';

    public function initContent()
    {
        $selectScope = new Select('timeframe');
        $selectScope->setAvailableValues($this->chartOptions());
        $selectScope->setDefaultValue('week');
        $this->addSettingField($selectScope);
        $this->updateChartScale('yAxes', [
            [
                'scaleLabel' => [
                    'display'     => true,
                    'labelString' => sl('lang')->tr('Bytes/s')
                ],
                'ticks'      => [
                    'callback' => 'mgBytesToSize'
                ],
            ]]);
        $this->updateChartOption('tooltips', [
            'callbacks' => [
                'label' => 'mgTooltipCallbackForNet'
            ]
        ]);

    }

    public function prepareAjaxData()
    {
        if ($this->configChartsSettings->timeframe)
        {
            $this->timeframe = $this->configChartsSettings->timeframe;
        }
        $rrdata     = DatabaseCache::loadData(
            $this->graphSettingsKey.$this->timeframe  .$this->getWhmcsParamByKey('serviceid'). '_cacheData', [$this, 'chartData'], 30, true);
        $labels     = [];
        $dataSets   = [
            'netin'     => [],
            'netout'    => [],
        ];
        $dateFormat = in_array($this->timeframe, ['hour', 'day']) ? "H:i:s" : "Y-m-d";

        foreach ($rrdata as $rrd)
        {
            $labels[]               = date($dateFormat, $rrd['time']);
            $dataSets['netin'][]    = isset($rrd['netin']) ? (float)$rrd['netin'] : 0;
            $dataSets['netout'][]   = isset($rrd['netout']) ? (float)$rrd['netout'] : 0;
        }


        //Labels
        $this->setLabels($labels);
        //Memory
        $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);
        //Total
        $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);
    }
}
