<?php
namespace Ceetrox\Managers\ChartManager;

Class ChartDataManager {

    private array    $categories;
    private Series   $series;
    private ?int     $numberOfCharts;
    private array    $colors;
    private ?int     $animationDuration;
    private ?string  $yAxisLabelValueFormat;
    private ?string  $rawData;

    private ?bool    $isStacked;
    private ?bool    $markLine;

    public function __construct()
    {
        $this->categories = [];
        $this->series = new Series();
        $this->numberOfCharts = null;
        $this->colors = [];
        $this->animationDuration = null;
        $this->yAxisLabelValueFormat = null;
        $this->rawData = null;

        $this->isStacked = null;
        $this->markLine = null;
    }

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

    public function setColors($color)
    {
        if(is_array($color)) {
            $this->colors = array_merge($this->colors, $color);
        } else {
            $this->colors[] = $color;
        }
    }

    public function setAnimationDuration(int $value)
    {
        $this->animationDuration = $value;
    }

    public function setYAxisLabelValueFormat(string $value)
    {
        $this->yAxisLabelValueFormat = $value;
    }

    public function setRawData(string $value)
    {
        $this->rawData = $value;
    }

    public function setIsStacked(bool $value)
    {
        $this->isStacked = $value;
    }

    public function setMarkLine(string $value)
    {
        $this->markLine = $value;
    }

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

    public function addSeries(SeriesData $series)
    {
        $this->series->push($series);
        $this->numberOfCharts = $this->series->numberOfCharts();
    }

    public function getParentData(): array
    {
        // set if conditions so we can only pass attributes that are set.
        // if attributes are not set then we can check and use the manual parameters of the charts.

        $data = [];

        if(!is_null($this->numberOfCharts))             $data['charts'] = $this->numberOfCharts;
        if(count($this->series) > 0)                    $data['series'] = $this->series->toArray();
        if(count($this->categories) > 0)                $data['categories'] = $this->categories;
        if(count($this->colors) > 0)                    $data['colors'] = $this->colors;
        if(!is_null($this->animationDuration))          $data['animationDuration'] = $this->animationDuration;
        if(!is_null($this->yAxisLabelValueFormat))      $data['yAxisLabelValueFormat'] = $this->yAxisLabelValueFormat;
        if(!is_null($this->rawData))                    $data['raw'] = json_encode(json_decode($this->rawData));

        if(!is_null($this->isStacked))                  $data['stacked'] = $this->isStacked;
        if(!is_null($this->markLine))                   $data['markLine'] = $this->markLine;

        return $data;
    }
}