<?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) : ChartDataManager
    {
        if(is_array($color)) {
            $this->colors = array_merge($this->colors, $color);
        } else {
            $this->colors[] = $color;
        }

        return $this;
    }

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

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

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

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

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

    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->series->push($series);
        $this->numberOfCharts = $this->series->numberOfCharts();
        return $this;
    }

    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;
    }
}