<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Terminy\Entities\Udalost\ValueObjects;

use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;

/**
 * Class MoznostUdalostiValueObject
 *
 * @package Seka\Portal\Modul\Terminy\Entities\Udalost\ValueObjects
 */
final class MoznostUdalostiValueObject implements Arrayable, JsonSerializable
{
    /**
     * @var string
     */
    private $id;

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

    /**
     * @param string      $popis
     * @param string|null $id
     */
    public function __construct(string $popis, ?string $id = null)
    {
        $this->id = $id ?? md5(sprintf('%s%s', $this->popis, rand(0, 1024)));
        $this->popis = $popis;
    }

    /**
     * @param $moznost
     *
     * @return self
     */
    public static function fromJSON($moznost): self
    {
        $data = json_decode($moznost, true);

        return new self($data['popis'] ?? '-', $data['id']);
    }

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

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

    /**
     * @param \Seka\Portal\Modul\Terminy\Entities\Udalost\ValueObjects\MoznostUdalostiValueObject $moznostUdalostiValueObject
     *
     * @return bool
     */
    public function isEqual(MoznostUdalostiValueObject $moznostUdalostiValueObject): bool
    {
        return $this->id === $moznostUdalostiValueObject->getId();
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'popis' => $this->popis,
        ];
    }

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