<?php

declare(strict_types=1);

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

use InvalidArgumentException;
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\Pracovisko\PocetKontrolPodlaEmisnehoSystemuVozidla
 */
final class PocetKontrolPodlaEmisnehoSystemuVozidlaEChartSourceItem implements EChartSourceItemContract
{
    /**
     * @var int
     */
    protected int $pocet = 0;

    /**
     * @var \Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EmisnySystemChartEnum
     */
    private EmisnySystemChartEnum $emisnySystem;

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\DruhPalivaEnum   $druhPaliva
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\EmisnySystemEnum $emisnySystem
     */
    public function __construct(DruhPalivaEnum $druhPaliva, EmisnySystemEnum $emisnySystem)
    {
        $this->emisnySystem = $this->emisnySystemChartEnum($druhPaliva, $emisnySystem);
    }

    /**
     * @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 \Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla $pocetKontrolPodlaEmisnehoSystemu
     *
     * @return $this
     */
    public function add(PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla $pocetKontrolPodlaEmisnehoSystemu): self
    {
        $kat = $this->emisnySystemChartEnum($pocetKontrolPodlaEmisnehoSystemu->druhPaliva(), $pocetKontrolPodlaEmisnehoSystemu->emisnySystem());

        if (!$this->emisnySystem->equal($kat)) {
            throw new InvalidArgumentException(sprintf(
                'Ocakava sa emisny system vozidla %s, vstupny atribut obsahuje emisny system vozidla %s',
                $this->emisnySystem->label(),
                $pocetKontrolPodlaEmisnehoSystemu->emisnySystem()->label()
            ));
        }

        $this->pocet += $pocetKontrolPodlaEmisnehoSystemu->pocet();

        return $this;
    }

    /**
     * @return \Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EmisnySystemChartEnum
     */
    public function emisnySystem(): EmisnySystemChartEnum
    {
        return $this->emisnySystem;
    }

    /**
     * @return int
     */
    public function pocet(): int
    {
        return $this->pocet;
    }

    /**
     * @param \Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EmisnySystemChartEnum $emisnySystemChartEnum
     * @param int                                                                                                    $pocet
     *
     * @return void
     */
    public function incrementData(EmisnySystemChartEnum $emisnySystemChartEnum, int $pocet)
    {
        if (!$this->emisnySystem->equal($emisnySystemChartEnum)) {
            throw new InvalidArgumentException(sprintf(
                'Ocakava sa emisny system vozidla %s, vstupny atribut obsahuje emisny system vozidla %s',
                $this->emisnySystem->label(),
                $emisnySystemChartEnum->label()
            ));
        }

        $this->pocet += $pocet;
    }

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

    /**
     * @return array
     */
    public function toArray(): array
    {
        return [
            'label' => $this->emisnySystem->label(),
            'emisny_system' => $this->emisnySystem->value,
            'pocet' => $this->pocet,
        ];
    }

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