<?php

namespace App\Entity;

use App\Enums\County;
use App\Enums\ResourceType;
use App\Enums\State;
use App\Libs\Libs;
use App\Repository\CommunityResourceRepository;
use DateTime;
use DateTimeZone;
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: CommunityResourceRepository::class)]
class CommunityResource
{
    #[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 $name = null;

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

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

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

    #[ORM\Column(enumType: State::class)]
    private ?State $state = null;

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

    #[ORM\Column(enumType: County::class)]
    private ?County $county = null;

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

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

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

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $monOpen = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $monClose = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $tueOpen = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $tueClose = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $wedOpen = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $wedClose = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $thuOpen = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $thuClose = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $friOpen = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $friClose = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $satOpen = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $satClose = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $sunOpen = null;

    #[ORM\Column(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $sunClose = null;

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

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

    #[ORM\Column(type: Types::JSON, enumType: ResourceType::class)]
    private array $type = [];

    #[ORM\Column(nullable: true)]
    private ?float $lat = null;

    #[ORM\Column(nullable: true)]
    private ?float $lon = null;

    public function __construct(
        private DateTime $today = new DateTime('now', new DateTimeZone('America/Indiana/Indianapolis'))
    ) {
    }

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): static
    {
        $this->name = $name;

        return $this;
    }

    public function getAddress(): ?string
    {
        return $this->address;
    }

    public function setAddress(string $address): static
    {
        $this->address = $address;

        return $this;
    }

    public function getAddress2(): ?string
    {
        return $this->address2;
    }

    public function setAddress2(?string $address2): static
    {
        $this->address2 = $address2;

        return $this;
    }

    public function getCity(): ?string
    {
        return $this->city;
    }

    public function setCity(string $city): static
    {
        $this->city = $city;

        return $this;
    }

    public function getState(): ?State
    {
        return $this->state;
    }

    public function setState(State $state): static
    {
        $this->state = $state;

        return $this;
    }

    public function getZip(): ?int
    {
        return $this->zip;
    }

    public function setZip(int $zip): static
    {
        $this->zip = $zip;

        return $this;
    }

    public function getCounty(): ?County
    {
        return $this->county;
    }

    public function setCounty(County $county): static
    {
        $this->county = $county;

        return $this;
    }

    public function getFormattedAddress(): ?string
    {
        return $this->address .
            ($this->address2 ? ' ' . $this->address2 : '') . '<br/>' .
            $this->city . ', ' . $this->state->value . ' ' . $this->zip;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(?string $phone): static
    {
        $this->phone = $phone;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(?string $email): static
    {
        $this->email = $email;

        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(?string $url): static
    {
        $this->url = $url;

        return $this;
    }

    public function urlString(): ?string
    {
        if (preg_match("/facebook/i", $this->url)) {
            return "<a href='$this->url' target='_blank'><i class='fa-brands fa-facebook'></i></a>";
        } else {
            return "<a href='$this->url' target='_blank'><i class='fa-solid fa-globe'></i></a>";
        }
        return null;
    }

    public function getContactCard(): ?string
    {
        $formattedPhone = ($this->phone ? Libs::formatPhone($this->phone) : '');
        return ($this->email ? "<a href='mailto:$this->email'>$this->email</a><br/>" : '') .
            ($this->phone ? "<a href='tel:$this->phone'>$formattedPhone</a>" : '');
    }

    public function generateVCard(): string
    {
        return 'BEGIN:VCARD' .
            "\nVERSION:3.0" .
            "\nN:$this->name" .
            "\nFN:$this->name" .
            "\nORG:$this->name" .
            "\nADR;TYPE=WORK:;;$this->address;$this->city;{$this->state->value};$this->zip" .
            ($this->phone ? "\nTEL;TYPE=WORK,VOICE:$this->phone" : null) .
            ($this->email ? "\nEMAIL;TYPE=WORK,INTERNET:$this->email" : null) .
            ($this->url ? "\nURL:$this->url" : null) .
            "\nNOTE:$this->notes" .
            "\nREV:" . date('c') .
            "\nEND:VCARD";
    }

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

    public function setMonOpen(?\DateTimeInterface $monOpen): static
    {
        $this->monOpen = $monOpen;

        return $this;
    }

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

    public function setMonClose(?\DateTimeInterface $monClose): static
    {
        $this->monClose = $monClose;

        return $this;
    }

    public function mon(): ?string
    {
        if (!$this->monOpen || !$this->monClose) {
            return 'C';
        }

        $closeAt = new DateTime($this->today->format('Y-m-d') . ' ' . $this->monClose->format('H:i:s'), new DateTimeZone($_ENV['COMPANY_TIMEZONE']));
        if ($closeAt <= new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']))) {
            return 'C';
        }

        return $this->monOpen->format('g:i A') . '-' . $this->monClose->format('g:i A');
    }

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

    public function setTueOpen(?\DateTimeInterface $tueOpen): static
    {
        $this->tueOpen = $tueOpen;

        return $this;
    }

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

    public function setTueClose(?\DateTimeInterface $tueClose): static
    {
        $this->tueClose = $tueClose;

        return $this;
    }

    public function tue(): ?string
    {
        if (!$this->tueOpen || !$this->tueClose) {
            return 'C';
        }

        $closeAt = new DateTime($this->today->format('Y-m-d') . ' ' . $this->tueClose->format('H:i:s'), new DateTimeZone($_ENV['COMPANY_TIMEZONE']));
        if ($closeAt <= new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']))) {
            return 'C';
        }

        return $this->tueOpen->format('g:i A') . '-' . $this->tueClose->format('g:i A');
    }

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

    public function setWedOpen(?\DateTimeInterface $wedOpen): static
    {
        $this->wedOpen = $wedOpen;

        return $this;
    }

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

    public function setWedClose(?\DateTimeInterface $wedClose): static
    {
        $this->wedClose = $wedClose;

        return $this;
    }

    public function wed(): ?string
    {
        if (!$this->wedOpen || !$this->wedClose) {
            return 'C';
        }

        $closeAt = new DateTime($this->today->format('Y-m-d') . ' ' . $this->wedClose->format('H:i:s'), new DateTimeZone($_ENV['COMPANY_TIMEZONE']));
        if ($closeAt <= new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']))) {
            return 'C';
        }

        return $this->wedOpen->format('g:i A') . '-' . $this->wedClose->format('g:i A');
    }

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

    public function setThuOpen(?\DateTimeInterface $thuOpen): static
    {
        $this->thuOpen = $thuOpen;

        return $this;
    }

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

    public function setThuClose(?\DateTimeInterface $thuClose): static
    {
        $this->thuClose = $thuClose;

        return $this;
    }

    public function thu(): ?string
    {
        if (!$this->thuOpen || !$this->thuClose) {
            return 'C';
        }

        $closeAt = new DateTime($this->today->format('Y-m-d') . ' ' . $this->thuClose->format('H:i:s'), new DateTimeZone($_ENV['COMPANY_TIMEZONE']));
        if ($closeAt <= new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']))) {
            return 'C';
        }

        return $this->thuOpen->format('g:i A') . '-' . $this->thuClose->format('g:i A');
    }

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

    public function setFriOpen(?\DateTimeInterface $friOpen): static
    {
        $this->friOpen = $friOpen;

        return $this;
    }

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

    public function setFriClose(?\DateTimeInterface $friClose): static
    {
        $this->friClose = $friClose;

        return $this;
    }

    public function fri(): ?string
    {
        if (!$this->friOpen || !$this->friClose) {
            return 'C';
        }

        $closeAt = new DateTime($this->today->format('Y-m-d') . ' ' . $this->friClose->format('H:i:s'), new DateTimeZone($_ENV['COMPANY_TIMEZONE']));
        if ($closeAt <= new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']))) {
            return 'C';
        }

        return $this->friOpen->format('g:i A') . '-' . $this->friClose->format('g:i A');
    }

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

    public function setSatOpen(?\DateTimeInterface $satOpen): static
    {
        $this->satOpen = $satOpen;

        return $this;
    }

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

    public function setSatClose(?\DateTimeInterface $satClose): static
    {
        $this->satClose = $satClose;

        return $this;
    }

    public function sat(): ?string
    {
        if (!$this->satOpen || !$this->satClose) {
            return 'C';
        }

        $closeAt = new DateTime($this->today->format('Y-m-d') . ' ' . $this->satClose->format('H:i:s'), new DateTimeZone($_ENV['COMPANY_TIMEZONE']));
        if ($closeAt <= new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']))) {
            return 'C';
        }

        return $this->satOpen->format('g:i A') . '-' . $this->satClose->format('g:i A');
    }

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

    public function setSunOpen(?\DateTimeInterface $sunOpen): static
    {
        $this->sunOpen = $sunOpen;

        return $this;
    }

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

    public function setSunClose(?\DateTimeInterface $sunClose): static
    {
        $this->sunClose = $sunClose;

        return $this;
    }

    public function sun(): ?string
    {
        if (!$this->sunOpen || !$this->sunClose) {
            return 'C';
        }

        $closeAt = new DateTime($this->today->format('Y-m-d') . ' ' . $this->sunClose->format('H:i:s'), new DateTimeZone($_ENV['COMPANY_TIMEZONE']));
        if ($closeAt <= new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']))) {
            return 'C';
        }

        return $this->sunOpen->format('g:i A') . '-' . $this->sunClose->format('g:i A');
    }

    public function getHours(): ?string
    {
        $this->today = new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']));
        switch ($this->today->format('w')) {
            case 0:
                return $this->sun();
            case 1:
                return $this->mon();
            case 2:
                return $this->tue();
            case 3:
                return $this->wed();
            case 4:
                return $this->thu();
            case 5:
                return $this->fri();
            case 6:
                return $this->sat();
        }
    }

    public function getFormattedHours(): ?string
    {
        $mon = 'CLOSED';
        $tue = 'CLOSED';
        $wed = 'CLOSED';
        $thu = 'CLOSED';
        $fri = 'CLOSED';
        $sat = 'CLOSED';
        $sun = 'CLOSED';

        if ($this->monOpen && $this->monClose) {
            $mon = $this->monOpen->format('g:i A') . '-' . $this->monClose->format('g:i A');
        }
        if ($this->tueOpen && $this->tueClose) {
            $tue = $this->tueOpen->format('g:i A') . '-' . $this->tueClose->format('g:i A');
        }
        if ($this->wedOpen && $this->wedClose) {
            $wed = $this->wedOpen->format('g:i A') . '-' . $this->wedClose->format('g:i A');
        }
        if ($this->thuOpen && $this->thuClose) {
            $thu = $this->thuOpen->format('g:i A') . '-' . $this->thuClose->format('g:i A');
        }
        if ($this->friOpen && $this->friClose) {
            $fri = $this->friOpen->format('g:i A') . '-' . $this->friClose->format('g:i A');
        }
        if ($this->satOpen && $this->satClose) {
            $sat = $this->satOpen->format('g:i A') . '-' . $this->satClose->format('g:i A');
        }
        if ($this->sunOpen && $this->sunClose) {
            $sun = $this->sunOpen->format('g:i A') . '-' . $this->sunClose->format('g:i A');
        }

        return <<<HTML
            <p>Sun: {$sun}</p>
            <p>Mon: {$mon}</p>
            <p>Tue: {$tue}</p>
            <p>Wed: {$wed}</p>
            <p>Thu: {$thu}</p>
            <p>Fri: {$fri}</p>
            <p>Sat: {$sat}</p>
        HTML;
    }

    public function getNotes(): ?string
    {
        return $this->notes;
    }

    public function setNotes(?string $notes): static
    {
        $this->notes = $notes;

        return $this;
    }

    public function getServicesAvailable(): ?string
    {
        return $this->servicesAvailable;
    }

    public function setServicesAvailable(?string $servicesAvailable): static
    {
        $this->servicesAvailable = $servicesAvailable;

        return $this;
    }

    /**
     * @return ResourceType[]
     */
    public function getType(): array
    {
        return $this->type;
    }

    public function setType(array $type): static
    {
        $this->type = $type;

        return $this;
    }

    public function getLat(): ?float
    {
        return $this->lat;
    }

    public function setLat(?float $lat): static
    {
        $this->lat = $lat;

        return $this;
    }

    public function getLon(): ?float
    {
        return $this->lon;
    }

    public function setLon(?float $lon): static
    {
        $this->lon = $lon;

        return $this;
    }

    public function _toInfoWindow(): string
    {
        return <<<EOL
        {$this->name}<br/>
        <a href='http://maps.google.com/?q={$this->lat},{$this->lon}'>{$this->address}<br/>
        {$this->city}, {$this->state->value} {$this->zip}</a><br/>
        {$this->servicesAvailable}<br/>
        {$this->getContactCard()}
        EOL;
    }
}
