<?php

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Graph\Pages;

use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\Core\Models\ModuleSettings\Model;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

trait GraphData
{
    use ProductService;
    use ApiService;

    protected $timeframe = "week";

    public function chartData()
    {
        $request = [
            "timeframe" => $this->timeframe,
            "cf"        => "MAX",
        ];
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        return $vm->rrdData($request);
    }

    protected function chartOptions()
    {
        $registrationDate = new \DateTime($this->getWhmcsParamByKey('model')->registrationDate->format("Y-m-d"));
        $options          = ['hour' => sl("lang")->tr("Hour")];
        $dnow             = new \DateTime();
        $dDiff            = $registrationDate->diff($dnow);
        if ($dDiff->days >= 1)
        {
            $options['day'] = sl("lang")->tr("Day");
        }
        if ($dDiff->days >= 7)
        {
            $options['week'] = sl("lang")->tr("Week");
        }
        if ($dDiff->days >= 30)
        {
            $options['month'] = sl("lang")->tr("Month");
        }
        if ($dDiff->y >= 1)
        {
            $options['year'] = sl("lang")->tr("year");
        }
        return $options;
    }

    protected function loadSettings()
    {
        $this->configChartsSettings = json_decode(Model::where('setting', $this->graphSettingsKey)->first()->value);

        if ($this->configChartsSettings)
        {
            $this->setGraphFilterInfo(null, $this->configChartsSettings->start, $this->configChartsSettings->end);
        }

        return $this;
    }

    protected function convertData($data, $format = null)
    {
        return array_slice($data, 0, 24);

        $format    = [];
        //Format data
        foreach($this->convertData($rrdata) as $record)
        {
            $time           = date('Y-m-d H', $record['time']);
            $format[$time][]   = $record;
        }

        //calculate average value
        foreach($format as $time => $records)
        {
            $sum = [];
            $count = count($records);

            foreach($records as $record)
            {
                foreach($records as $type => $value)
                {
                }

                $sum['time']    = strtotime($time);
            }
        }


    }


}
