<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\PocetKontrolPodlaKategorieVozidla;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Str;
use JsonSerializable;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaKategorieVozidla;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceItemContract;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\KategoriaVozidlaChartEnum;

/**
 * Class PocetKontrolPodlaKategorieVozidlaEChartSourceItem
 *
 * @package Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\PocetKontrolPodlaKategorieVozidla
 */
final class PocetKontrolPodlaKategorieVozidlaEChartSourceItem implements EChartSourceItemContract, Arrayable, Jsonable, JsonSerializable
{
    /**
     * @var int
     */
    protected int $pocet = 0;
    /**
     * @var string
     */
    private string $technik;
    /**
     * @var array
     */
    private array $poctyPreKategorie = [];

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

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

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaKategorieVozidla $pocetKontrolPodlaKategorieVozidla
     *
     * @return $this
     */
    public function add(PocetKontrolPodlaKategorieVozidla $pocetKontrolPodlaKategorieVozidla): self
    {
        $this->poctyPreKategorie[$this->kategoriaVozidlaChartEnum($pocetKontrolPodlaKategorieVozidla->kategoria())->value] += $pocetKontrolPodlaKategorieVozidla->pocet();
        $this->pocet += $pocetKontrolPodlaKategorieVozidla->pocet();

        return $this;
    }

    /**
     * @param string $kategoriaVozidla
     *
     * @return \Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\KategoriaVozidlaChartEnum
     */
    private function kategoriaVozidlaChartEnum(string $kategoriaVozidla): KategoriaVozidlaChartEnum
    {
        switch ($kategoriaVozidla) {
            case Str::startsWith($kategoriaVozidla, ['M1', 'N1']):
                $kat = KategoriaVozidlaChartEnum::M1_N1();
                break;
            case Str::startsWith($kategoriaVozidla, ['M2', 'M3']):
                $kat = KategoriaVozidlaChartEnum::M2_M3();
                break;
            case Str::startsWith($kategoriaVozidla, ['N2', 'N3']):
                $kat = KategoriaVozidlaChartEnum::N2_N3();
                break;
            default:
                $kat = KategoriaVozidlaChartEnum::T();
                break;
        }

        return $kat;
    }

    /**
     * @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,
            'pocet' => $this->pocet,
        ], $this->poctyPreKategorie);
    }

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