<?php
namespace Ceetrox\Sidekick\Views\Limitless\Linechart\DataManager;

Class ChartDataManager {

    private $categories;
    private $series;

    public function __construct()
    {
        $this->categories = [];
        $this->series = new Series();
    }

    public static function getInstance(): ChartDataManager
    {
        return new self;
    }

    public static function getSeriesDataInstance(): SeriesData
    {
        return new SeriesData();
    }

    public function getSeries() : Series
    {
        return $this->series;
    }

    private function getCategories() : array
    {
        return $this->categories;
    }

    public function addCategories($category) : ChartDataManager
    {
        if(is_array($category)) {
            $this->categories = array_merge($this->categories, $category);
        } else {
            $this->categories[] = $category;
        }

        return $this;
    }

    public function addSeries(SeriesData $series) : ChartDataManager
    {
        $this->getSeries()->push($series);
        return $this;
    }

    public function toJson(): string
    {
        return json_encode($this->toArray());
    }

    public function toArray(): array
    {
        $data['series'] = $this->getSeries()->toArray();
        $data['categories'] = $this->getCategories();

        return $data;
    }

}