<?php

declare(strict_types=1);

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

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\DruhPalivaEnum;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\EmisnySystemEnum;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceItemContract;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EmisnySystemChartEnum;

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

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

        foreach (EmisnySystemChartEnum::cases() as $case) {
            $this->poctyEmisnychSystemov[$case->value] = 0;
        }
    }

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla $pocetKontrolPodlaEmisnehoSystemu
     *
     * @return $this
     */
    public function add(PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla $pocetKontrolPodlaEmisnehoSystemu): self
    {
        $this->poctyEmisnychSystemov[$this->emisnySystemChartEnum(
            $pocetKontrolPodlaEmisnehoSystemu->druhPaliva(),
            $pocetKontrolPodlaEmisnehoSystemu->emisnySystem()
        )->value] += $pocetKontrolPodlaEmisnehoSystemu->pocet();
        $this->pocet += $pocetKontrolPodlaEmisnehoSystemu->pocet();

        return $this;
    }

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\DruhPalivaEnum   $druhPaliva
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\EmisnySystemEnum $emisnySystem
     *
     * @return \Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EmisnySystemChartEnum
     */
    private function emisnySystemChartEnum(DruhPalivaEnum $druhPaliva, EmisnySystemEnum $emisnySystem): EmisnySystemChartEnum
    {
        switch ($emisnySystem) {
            case DruhPalivaEnum::D->equal($druhPaliva) && (EmisnySystemEnum::BKAT->equal($emisnySystem) || EmisnySystemEnum::NKAT->equal($emisnySystem)):
                $emSys = EmisnySystemChartEnum::D_BKAT_NKAT;
                break;
            case DruhPalivaEnum::D->equal($druhPaliva) && EmisnySystemEnum::NKAT_OBD->equal($emisnySystem):
                $emSys = EmisnySystemChartEnum::D_NKAT_OBD;
                break;
            case EmisnySystemEnum::RKAT->equal($emisnySystem):
                $emSys = EmisnySystemChartEnum::RKAT;
                break;
            case EmisnySystemEnum::RKAT_OBD->equal($emisnySystem):
                $emSys = EmisnySystemChartEnum::RKAT_OBD;
                break;
            case !DruhPalivaEnum::D->equal($druhPaliva) && (EmisnySystemEnum::BKAT->equal($emisnySystem) || EmisnySystemEnum::NKAT->equal($emisnySystem)):
                $emSys = EmisnySystemChartEnum::BKAT_NKAT;
                break;
            default:
                $emSys = EmisnySystemChartEnum::NEDEFINOVANY;
                break;
        }

        return $emSys;
    }

    /**
     * @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->poctyEmisnychSystemov);
    }

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