<?php

namespace App\Entity;

use JsonSerializable;
use App\Repository\NoteRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: NoteRepository::class)]
class Note implements JsonSerializable
{
    #[ORM\Id]
    #[ORM\Column(type: UuidType::NAME, unique: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
    private ?Uuid $id = null;

    #[ORM\Column(length: 255)]
    private ?string $title = null;

    #[ORM\Column(type: Types::DATE_MUTABLE)]
    private ?\DateTimeInterface $date = null;

    #[ORM\Column(length: 255)]
    private ?string $passage = null;

    #[ORM\Column(type: Types::JSON, nullable: true)]
    private ?array $refs = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $text = null;

    #[ORM\ManyToOne(inversedBy: 'notes')]
    private ?Speaker $speaker = null;

    #[ORM\ManyToOne(inversedBy: 'notes')]
    private ?Series $series = null;

    #[ORM\ManyToOne(inversedBy: 'notes')]
    private ?User $user = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $recording = null;

    public function getId(): ?Uuid
    {
        return $this->id;
    }

    public function setId(?Uuid $id): static
    {
        $this->id = $id;

        return $this;
    }

    public static function idFromString(string $id): Uuid
    {
        return Uuid::fromString($id);
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): static
    {
        $this->title = $title;

        return $this;
    }

    public function getDate(): ?\DateTimeInterface
    {
        return $this->date;
    }

    public function setDate(\DateTimeInterface $date): static
    {
        $this->date = $date;

        return $this;
    }

    public function getPassage(): ?string
    {
        return $this->passage;
    }

    public function setPassage(string $passage): static
    {
        $this->passage = $passage;

        return $this;
    }

    public function getRefs(): ?array
    {
        return $this->refs;
    }

    public function setRefs(?array $refs): static
    {
        $this->refs = $refs;

        return $this;
    }

    public function addRef(Reference $ref): static
    {
        $this->refs[] = $ref;

        return $this;
    }

    public function getText(): ?string
    {
        return $this->text;
    }

    public function setText(?string $text): static
    {
        $this->text = $text;

        return $this;
    }

    public function getSpeaker(): ?Speaker
    {
        return $this->speaker;
    }

    public function setSpeaker(?Speaker $speaker): static
    {
        $this->speaker = $speaker;

        return $this;
    }

    public function getSeries(): ?Series
    {
        return $this->series;
    }

    public function setSeries(?Series $series): static
    {
        $this->series = $series;

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): static
    {
        $this->user = $user;

        return $this;
    }

    public function toLink(): string
    {
        return "<a href='#' onclick=\"note.retrieveNote('{$this->id}')\">".
            $this->title.
            "</a>&nbsp;".
            "<a href='#' onclick=\"note.deleteNote('{$this->id}', this)\">".
                "<i class='fas fa-trash-alt'></i>".
            "</a>".
            (
                $this->recording ? 
                "<a href='{$this->recording}' target='_blank' class='recording-link'>".
                    "<i class='fa fa-play-circle'></i>".
                "</a>" : null
            );
    }

    public function toTableRow(): string
    {
        return "<tr data-id='row-{$this->id->toHex()}'>".
            "<td>{$this->toLink()}</td>".
            "<td>{$this->speaker->getName()}</td>".
            "<td>{$this->passage}</td>".
            "<td>{$this->date->format('M j, Y')}</td>".
        "</tr>";
    }

    public function jsonSerialize(): array
    {
        return [
            'id' => $this->getId(),
            'recording' => $this->getRecording(),
            'link' => $this->toLink(),
            'title' => $this->getTitle(),
            'date' => $this->getDate(),
            'passage' => $this->getPassage(),
            'refs' => $this->getRefs(),
            'text' => $this->getText(),
            'speaker' => $this->getSpeaker(),
            'series' => $this->getSeries(),
            'user' => $this->getUser(),
        ];
    }

    public function getRecording(): ?string
    {
        return $this->recording;
    }

    public function setRecording(?string $recording): static
    {
        $this->recording = $recording;

        return $this;
    }
}
