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

Class ChartDataManager {

    private $categories;
    private $series;
    private $numberOfCharts;
    private $titles;
    private $colors;
    private $hasBoundaryGap;
    private $isInverted;
    private $hasDataZoom;
    private $animationDuration;
    private $yAxisLabelValueFormat;
    private $rawData;

    // these attributes should also be available on seriesData object.
    private $showPointValues;
    private $isStacked;
    private $isArea;
    private $markLine;

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

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

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

    private function getNumberOfCharts() : int
    {
        return $this->series->numberOfCharts();
    }

    private function getTitles()
    {
        return $this->titles;
    }

    private function getColors() : array
    {
        return $this->colors;
    }

    private function getHasBoundaryGap() : ?bool
    {
        return $this->hasBoundaryGap;
    }

    private function getIsInverted() : ?bool
    {
        return $this->isInverted;
    }

    private function getHasDataZoom() : ?bool
    {
        return $this->hasDataZoom;
    }

    private function getAnimationDuration() : ?int
    {
        return $this->animationDuration;
    }

    private function getYAxisLabelValueFormat() : ?string
    {
        return $this->yAxisLabelValueFormat;
    }

    private function getRawData() : ?string
    {
        return $this->rawData;
    }

    private function getShowPointValues() : ?bool
    {
        return $this->showPointValues;
    }

    private function getIsStacked() : ?bool
    {
        return $this->isStacked;
    }

    private function getIsArea() : ?bool
    {
        return $this->isArea;
    }

    private function getMarkLine() : ?string
    {
        return $this->markLine;
    }

    public function setTitle($title) : ChartDataManager
    {
        if(is_array($title)) {
            $this->titles = array_merge($this->titles, $title);
        } else {
            $this->titles[] = $title;
        }

        return $this;
    }

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

        return $this;
    }

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

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

    public function setHasDataZoom(bool $value) : ChartDataManager
    {
        $this->hasDataZoom = $value;
        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 setShowPointValues(bool $value) : ChartDataManager
    {
        $this->showPointValues = $value;
        return $this;
    }

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

    public function setIsArea(bool $value) : ChartDataManager
    {
        $this->isArea = $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->getSeries()->push($series);
        $this->numberOfCharts = $this->getSeries()->numberOfCharts();
        return $this;
    }

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

    public function toArray(): 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.

        if(!is_null($this->numberOfCharts))             $data['charts'] = $this->numberOfCharts;

        if(count($this->series) > 0)                    $data['series'] = $this->getSeries()->toArray();
        if(count($this->categories) > 0)                $data['categories'] = $this->getCategories();
        if(!is_null($this->titles))                     $data['titles'] = $this->getTitles();
        if(count($this->colors) > 0)                    $data['colors'] = $this->getColors();

        if(!is_null($this->hasBoundaryGap))             $data['boundaryGap'] = $this->getHasBoundaryGap();
        if(!is_null($this->isInverted))                 $data['inverted'] = $this->getIsInverted();
        if(!is_null($this->hasDataZoom))                $data['dataZoom'] = $this->getHasDataZoom();
        if(!is_null($this->animationDuration))          $data['animationDuration'] = $this->getAnimationDuration();
        if(!is_null($this->yAxisLabelValueFormat))      $data['yAxisLabelValueFormat'] = $this->getYAxisLabelValueFormat();
        if(!is_null($this->rawData))                    $data['raw'] = json_encode(json_decode($this->getRawData()));

        if(!is_null($this->showPointValues))            $data['pointValues'] = $this->getShowPointValues();
        if(!is_null($this->isStacked))                  $data['stacked'] = $this->getIsStacked();
        if(!is_null($this->isArea))                     $data['isArea'] = $this->getIsArea();
        if(!is_null($this->markLine))                   $data['markLine'] = $this->getMarkLine();

        return $data;
    }

}