<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Pracovisko\PocetKontrolPodlaStavu;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\StavEmisnejKontrolyEnum;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaStavu;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceItemContract;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\StavKontrolyChartEnum;

/**
 * Class PocetKontrolPodlaStavuEChartSourceItem
 *
 * @package Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Pracovisko\PocetKontrolPodlaStavu
 */
final class PocetKontrolPodlaStavuEChartSourceItem implements EChartSourceItemContract, Arrayable, Jsonable, JsonSerializable
{
    /**
     * @var int
     */
    protected int $pocet = 0;
    /**
     * @var array
     */
    private array $stavyKontrol = [];

    /**
     *
     */
    public function __construct()
    {
        foreach (StavKontrolyChartEnum::cases() as $case) {
            $this->stavyKontrol[$case->value] = 0;
        }
    }

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaStavu $pocetKontrolPodlaStavu
     *
     * @return $this
     */
    public function add(PocetKontrolPodlaStavu $pocetKontrolPodlaStavu): self
    {
        $this->stavyKontrol[$this->kontrolaChartEnum($pocetKontrolPodlaStavu->stav())->value] += $pocetKontrolPodlaStavu->pocet();
        $this->pocet += $pocetKontrolPodlaStavu->pocet();

        return $this;
    }

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\StavEmisnejKontrolyEnum $stavEmisnejKontroly
     *
     * @return \Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\StavKontrolyChartEnum
     */
    private function kontrolaChartEnum(StavEmisnejKontrolyEnum $stavEmisnejKontroly): StavKontrolyChartEnum
    {
        switch ($stavEmisnejKontroly) {
            case StavEmisnejKontrolyEnum::PRIJEM:
            case StavEmisnejKontrolyEnum::PREVZATIE:
            case StavEmisnejKontrolyEnum::MERANIE:
            case StavEmisnejKontrolyEnum::UZAVRETIE:
                $stavEK = StavKontrolyChartEnum::ROZPRACOVANE;
                break;
            case StavEmisnejKontrolyEnum::STORNO:
                $stavEK = StavKontrolyChartEnum::STORNO;
                break;
            default:
                $stavEK = StavKontrolyChartEnum::ARCHIV;
                break;
        }

        return $stavEK;
    }

    /**
     * @param array $stavyEmisnychKontrol
     *
     * @return void
     */
    public function incrementData(array $stavyEmisnychKontrol)
    {
        foreach ($this->stavyKontrol as $k => $v) {
            if (array_key_exists($k, $stavyEmisnychKontrol)) {
                $this->stavyKontrol[$k] += $stavyEmisnychKontrol[$k];
                $this->pocet += $stavyEmisnychKontrol[$k];
            }
        }
    }

    /**
     * @return array
     */
    public function stavyEmisnychKontrol(): array
    {
        return $this->stavyKontrol;
    }

    /**
     * @param int $options
     *
     * @return string
     */
    public function toJson($options = 0): string
    {
        return json_encode($this->toArray(), $options);
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return $this->stavyKontrol;
    }

    /**
     * @return array
     */
    public function jsonSerialize(): array
    {
        return $this->toArray();
    }
}
