<?php

namespace App\Entity;

use JsonSerializable;

use App\Repository\BibleRepository;
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: BibleRepository::class)]
class Bible 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 $book = null;

    #[ORM\Column]
    private ?int $chapter = null;

    #[ORM\Column]
    private ?int $verse = null;

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

    #[ORM\Column]
    private ?int $book_index = null;

    #[ORM\Column(length: 20, nullable: true)]
    private ?string $label = 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 getBook(): ?string
    {
        return $this->book;
    }

    public function setBook(string $book): static
    {
        $this->book = $book;

        return $this;
    }

    public function getChapter(): ?int
    {
        return $this->chapter;
    }

    public function setChapter(int $chapter): static
    {
        $this->chapter = $chapter;

        return $this;
    }

    public function getVerse(): ?int
    {
        return $this->verse;
    }

    public function setVerse(int $verse): static
    {
        $this->verse = $verse;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(?string $content): static
    {
        $this->content = $content;

        return $this;
    }

    public function getBookIndex(): ?int
    {
        return $this->book_index;
    }

    public function setBookIndex(int $book_index): static
    {
        $this->book_index = $book_index;

        return $this;
    }

    public function getLabel(): ?string
    {
        return $this->label;
    }

    public function setLabel(?string $label): static
    {
        $this->label = $label;

        return $this;
    }

    public static function findBook(string $reference): string
    {
        $book = '';
        $tmp = explode(' ', $reference);
        $book = "{$tmp[0]}";

        if(count($tmp) > 2) {
            $book = "{$tmp[0]} {$tmp[1]}";
        }

        return $book;
    }

    public static function findChapter(string $reference): string
    {
        $chapter = '';
        $tmp = explode(' ', $reference);

        if(count($tmp) > 2) {
            $passage = $tmp[2];
        } else {
            $passage = $tmp[1];
        }

        $tmp = explode(':', $passage);
        $chapter = $tmp[0];

        return $chapter;
    }

    public static function findVerse(string $reference): int|array|bool
    {
        $verses = null;
        $semicolon = strpos($reference, ':');
        if ($semicolon === false) {
            return false;
        }

        $tmp = substr($reference, $semicolon + 1);

        if(strpos($tmp, '-') !== false) {
            $verses = explode('-', $tmp);
        } else {
            $verses = (int) $tmp;
        }

        return $verses;
    }

    public function jsonSerialize(): array
    {
        return [
            'id' => $this->getId(),
            'book' => $this->getBook(),
            'chapter' => $this->getChapter(),
            'verse' => $this->getVerse(),
            'content' => $this->getContent(),
            'book_index' => $this->getBookIndex(),
            'label' => $this->getLabel(),
        ];
    }
}
