<?php

namespace App\Entity;

use App\Repository\CaseItineraryRepository;
use DateInterval;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;
use Symfony\UX\Map\Point;

#[ORM\Entity(repositoryClass: CaseItineraryRepository::class)]
class CaseItinerary
{
    #[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(type: Types::TIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $departure = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false)]
    private ?Location $originLocation = null;

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

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false)]
    private ?Location $destLocation = null;

    #[ORM\Column]
    private ?bool $caseMileage = null;

    #[ORM\Column]
    private ?DateInterval $duration = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false)]
    private ?MemberCase $memberCase = null;

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

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

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

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

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

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

        return $this;
    }

    public function getOriginLocation(): ?Location
    {
        return $this->originLocation;
    }

    public function setOriginLocation(?Location $originLocation): static
    {
        $this->originLocation = $originLocation;

        return $this;
    }

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

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

        return $this;
    }

    public function getDestLocation(): ?Location
    {
        return $this->destLocation;
    }

    public function setDestLocation(?Location $destLocation): static
    {
        $this->destLocation = $destLocation;

        return $this;
    }

    public function isCaseMileage(): ?bool
    {
        return $this->caseMileage;
    }

    public function setCaseMileage(bool $caseMileage): static
    {
        $this->caseMileage = $caseMileage;

        return $this;
    }

    public function getDuration(): ?DateInterval
    {
        return $this->duration;
    }

    public function setDuration(?DateInterval $duration): static
    {
        //$this->calcDuration();
        $this->duration = $duration;

        return $this;
    }

    public function calcDuration()
    {
        $this->duration = $this->departure - $this->arrival;
    }

    public function getMemberCase(): ?MemberCase
    {
        return $this->memberCase;
    }

    public function setMemberCase(?MemberCase $memberCase): static
    {
        $this->memberCase = $memberCase;

        return $this;
    }

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

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

        return $this;
    }

    public function getDistance(): ?float
    {
        return $this->distance;
    }

    public function setDistance(?float $distance): static
    {
        $this->distance = $distance;

        return $this;
    }

    public function getGpsRoute(): ?array
    {
        return $this->gpsRoute;
    }

    public function setGpsRoute(?array $gpsRoute): static
    {
        $this->gpsRoute = $gpsRoute;

        return $this;
    }

    public function getGPSPolyLines(): array
    {
        $points = [];
        foreach ($this->gpsRoute as $route) {
            $points[] = new Point($route['lat'], $route['lon']);
        }
        return $points;
    }

    public function originInfoWindow(): string
    {
        return <<<EOL
        {$this->originLocation->getName()}<br/>
        <a href='http://maps.google.com/?q={$this->originLocation->getLat()},{$this->originLocation->getLon()}'>{$this->originLocation->getFormattedAddress()}</a><br/>
        {$this->departure->format("g:i a")}
        EOL;
    }

    public function destinationInfoWindow(): string
    {
        return <<<EOL
        {$this->destLocation->getName()}<br/>
        <a href='http://maps.google.com/?q={$this->destLocation->getLat()},{$this->destLocation->getLon()}'>{$this->destLocation->getFormattedAddress()}</a><br/>
        {$this->arrival->format("g:i a")}
        EOL;
    }
}
