<?php

namespace App\Service\DataGrid;

use Doctrine\Common\Collections\ArrayCollection;

class DataGrid implements DataGridInterface
{
    private $data;
    private $headers;
    private $navigation;
    private $filters;
    private $sorters;
    private $aggregates;
    private $name;

    public function __construct()
    {
        $this->data = new ArrayCollection();
        $this->headers = new ArrayCollection();
        $this->navigation = new ArrayCollection();
        $this->filters = new ArrayCollection();
        $this->sorters = new ArrayCollection();
        $this->aggregates = new ArrayCollection();
//        $this->addAggregate(new DataGridAggregate('total', 'SUM', '1'));
    }

    public function getHeaders()
    {
        return $this->headers;
    }

    public function getNavigation()
    {
        return $this->navigation;
    }

    public function getFilters()
    {
        return $this->filters;
    }

    public function getSorters()
    {
        return $this->sorters;
    }

    public function getData()
    {
        return $this->data;
    }

    public function setNavigation(DataGridNavigationInterface $navigation)
    {
        $this->navigation = $navigation;

        return $this;
    }

    public function addHeader(DatagridHeaderInterface $header)
    {
        if (!$this->headers->contains($header)) {
            $this->headers->add($header);
        }

        return $this;
    }

    public function removeHeader(DataGridHeaderInterface $header)
    {
        if ($this->headers->contains($header)) {
            $this->headers->remove($header);
        }

        return $this;
    }

    public function addSorter(DataGridSortInterface $sort)
    {
        if (!$this->sorters->contains($sort)) {
            $this->sorters->add($sort);
        }

        return $this;
    }

    public function removeSorter(DataGridSortInterface $sort)
    {
        if ($this->sorters->contains($sort)) {
            $this->sorters->remove($sort);
        }

        return $this;
    }

    public function addFilter(DataGridFilterInterface $filter)
    {
        if (!$this->filters->contains($filter)) {
            $this->filters->set($filter->getKey(), $filter);
        }

        return $this;
    }

    public function setData(array $data)
    {
        $this->data = $data;

        return $this;
    }

    public function setTotal(int $total): self
    {
        $this->navigation->setTotal($total);

        return $this;
    }

    public function setAggregates($param): self
    {
        $this->aggregates = $param;

        return $this;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @return mixed
     */
    public function getAggregates()
    {
        return $this->aggregates;
    }

    /**
     * @param DataGridAggregate $aggregate
     *
     * @return DataGrid
     */
    public function addAggregate(DataGridAggregate $aggregate): self
    {
        if (!$this->aggregates->contains($aggregate)) {
            $this->aggregates[] = $aggregate;
        }

        return $this;
    }
}
