<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla;

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\DataTables\DataTableRowContract;

/**
 * Class PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidlaDataTableRow
 *
 * @package Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla
 */
final class PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidlaDataTableRow implements DataTableRowContract, Arrayable, Jsonable, JsonSerializable
{
    /**
     * @var array
     */
    private array $emisnySystemPocet = [];

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

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

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

//        foreach (DruhPalivaEnum::toValues() as $druhPalivaKey) {
//            foreach (EmisnySystemEnum::toValues() as $emisnySystemKey) {
//                $this->emisnySystemPocet[
//                    $this->makeEmisnySystemKey(
//                        DruhPalivaEnum::from($druhPalivaKey),
//                        EmisnySystemEnum::from($emisnySystemKey)
//                    )] = 0;
//            }
//        }
    }

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

        return $this;
    }

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\DruhPalivaEnum   $druhPaliva
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\EmisnySystemEnum $emisnySystem
     *
     * @return string
     */
    private function makeEmisnySystemKey(DruhPalivaEnum $druhPaliva, EmisnySystemEnum $emisnySystem): string
    {
        return sprintf('%s_%s', $druhPaliva->value, $emisnySystem->value);
    }

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

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

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

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