<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\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\Technik\PocetKontrolPodlaStavu
 */
final class PocetKontrolPodlaStavuEChartSourceItem implements EChartSourceItemContract, Arrayable, Jsonable, JsonSerializable
{
    /**
     * @var array
     */
    private array $poctyKontrolPodlaStavu = [];

    /**
     * @var string
     */
    private string $technik;

    /**
     * @var int
     */
    private int $spolu = 0;

    /**
     * @param string $technik
     */
    public function __construct(string $technik)
    {
        $this->technik = $technik;

        foreach (StavKontrolyChartEnum::toValues() as $k) {
            $this->poctyKontrolPodlaStavu[$k] = 0;
        }
    }

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaStavu $pocetKontrolPodlaStavu
     *
     * @return $this
     */
    public function add(PocetKontrolPodlaStavu $pocetKontrolPodlaStavu): self
    {
        $this->poctyKontrolPodlaStavu[$this->kontrolaChartEnum($pocetKontrolPodlaStavu->stav())->value] += $pocetKontrolPodlaStavu->pocet();
        $this->spolu += $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 int $options
     *
     * @return string
     */
    public function toJson($options = 0): string
    {
        return json_encode($this->toArray(), $options);
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return array_merge([
            'label' => $this->technik,
            'spolu' => $this->spolu,
        ], $this->poctyKontrolPodlaStavu);
    }

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