<?php

class TrackingEntry {

    private $status = TrackingStatus::TRACKING_STATUS_UNKNOWN;
    private $details = "";
    private $datetime = "";
    private $location = null;
    private $iscontainerscan = false;

    public function __construct(int $status, string $details, string $datetime, $location = null, bool $iscontainerscan = false) {
        $this->status = $status;
        $this->details = $details;
        $this->datetime = date("Y-m-d\TH:i:s", strtotime($datetime));
        if (is_null($location)) {
            $this->location = new Location();
        } else {
            $this->location = $location;
        }
        $this->iscontainerscan = $iscontainerscan;
        $this->formatDetails();
    }

    /**
     * Do some find/replace on the event details to clean up some carrier formatting nonsense.
     */
    private function formatDetails() {
        $findreplace = [
            "<SUP>&reg;</SUP>" => "®"
        ];

        foreach ($findreplace as $f => $r) {
            $this->details = str_replace($f, $r, $this->details);
        }
    }

    public function toString(): string {
        return $this->details;
    }

    public function getTimestamp(): int {
        return strtotime($this->datetime);
    }

    public function getDateTime(string $format = "Y-m-d\TH:i:s"): string {
        return date($format, strtotime($this->datetime));
    }

    public function getLocation() {
        return $this->location;
    }

    public function setLocation(Location $location) {
        $this->location = $location;
    }

    public function getStatus() {
        return $this->status;
    }

    public function isContainerScan() {
        return $this->iscontainerscan;
    }

}