<?php

class TrackingInfo {

    private $code = null;
    private $carrier = null;
    private $service = null;
    private $history = [];
    private $from = null;
    private $to = null;
    private $current_status = null;
    private $carrierImageUrl = "";
    private $attributionText = "";

    public function __construct($code = null, $carrier = null, $to = null, $from = null, $current_status = null, $history = null) {
        $this->code = $code;
        $this->carrier = $carrier;
        $this->to = $to;
        $this->from = $from;
        $this->current_status = $current_status;
        $this->history = $history;
    }

    public function setCarrierLogo(string $url) {
        $this->carrierImageUrl = $url;
    }

    public function setCarrierAttributionText(string $text) {
        $this->attributionText = $text;
    }

    public function setCode(string $code) {
        $this->code = $code;
    }

    public function getCode(): string {
        return $this->code;
    }

    public function setCarrier(string $carrierid) {
        $this->carrier = $carrierid;
    }

    public function getCarrier() {
        return $this->carrier;
    }

    public function setService(Service $service) {
        $this->service = $service;
    }

    public function getService() {
        return $this->service;
    }

    public function setTo(Location $to) {
        $this->to = $to;
    }

    public function getTo() {
        return $this->to;
    }

    public function setFrom(Location $from) {
        $this->from = $from;
    }

    public function getFrom() {
        return $this->from;
    }

    public function setCurrentStatus(TrackingEntry $status) {
        $this->current_status = $status;
    }

    public function getCurrentStatus() {
        return $this->current_status;
    }

    public function getHistory() {
        return $this->history;
    }

    public function appendHistoryEntry(TrackingEntry $history) {
        $this->history[] = $history;
    }

    public function toObject() {
        if (is_null($this->service)) {
            $this->service = new Service("", "");
        }
        $output = [
            "status" => "OK",
            "code" => $this->getCode(),
            "carrier" => [
                "code" => Carriers::getCarrierCode($this->getCarrier()),
                "name" => Carriers::getCarrierName($this->getCarrier()),
                "trackingurl" => Carriers::getCarrierTrackingUrl($this->getCarrier(), $this->getCode()),
                "attribution" => $this->attributionText,
                "logo" => $this->carrierImageUrl
            ],
            "service" => [
                "id" => $this->service->id,
                "name" => $this->service->name
            ],
            "addresses" => [
                "from" => [
                    "street" => "",
                    "city" => "Unknown",
                    "state" => "",
                    "zip" => "",
                    "country" => ""
                ],
                "to" => [
                    "street" => "",
                    "city" => "Unknown",
                    "state" => "",
                    "zip" => "",
                    "country" => ""
                ]
            ],
            "current" => [
                "status" => "UNKNOWN",
                "details" => "No tracking information found.",
                "datetime" => date("Y-m-d\TH:i:s", time()),
                "nicetime" => date("F j g:i a", time()),
                "containerscan" => false,
                "location" => [
                    "street" => "",
                    "city" => "",
                    "state" => "",
                    "zip" => "",
                    "country" => ""
                ]
            ],
            "history" => [
            ]
        ];

        if (!is_null($this->getCurrentStatus())) {
            $output["current"] = [
                "status" => TrackingStatus::statusToString($this->getCurrentStatus()->getStatus()),
                "details" => $this->getCurrentStatus()->toString(),
                "datetime" => $this->getCurrentStatus()->getDateTime(),
                "nicetime" => $this->getCurrentStatus()->getDateTime("F j g:i a"),
                "timestamp" => $this->getCurrentStatus()->getTimestamp(),
                "containerscan" => $this->getCurrentStatus()->isContainerScan(),
                "location" => [
                    "street" => "",
                    "city" => "Unknown",
                    "state" => "",
                    "zip" => "",
                    "country" => ""
                ]
            ];

            if (!empty($this->getCurrentStatus()->getLocation())) {
                $output["current"]["location"] = [
                    "street" => $this->getCurrentStatus()->getLocation()->street,
                    "city" => $this->getCurrentStatus()->getLocation()->city,
                    "state" => $this->getCurrentStatus()->getLocation()->state,
                    "zip" => $this->getCurrentStatus()->getLocation()->zip,
                    "country" => $this->getCurrentStatus()->getLocation()->country
                ];
            }
        }

        if (!is_null($this->getFrom())) {
            $output["addresses"]["from"] = [
                "street" => $this->getFrom()->street,
                "city" => $this->getFrom()->city,
                "state" => $this->getFrom()->state,
                "zip" => $this->getFrom()->zip,
                "country" => $this->getFrom()->country
            ];
        }

        if (!is_null($this->getTo())) {
            $output["addresses"]["to"] = [
                "street" => $this->getTo()->street,
                "city" => $this->getTo()->city,
                "state" => $this->getTo()->state,
                "zip" => $this->getTo()->zip,
                "country" => $this->getTo()->country
            ];
        }

        if (!is_null($this->getHistory())) {
            foreach ($this->getHistory() as $history) {
                $output["history"][] = [
                    "status" => TrackingStatus::statusToString($history->getStatus()),
                    "details" => $history->toString(),
                    "datetime" => $history->getDateTime(),
                    "nicetime" => $history->getDateTime("F j g:i a"),
                    "timestamp" => $history->getTimestamp(),
                    "containerscan" => $history->isContainerScan(),
                    "location" => [
                        "street" => $history->getLocation()->street,
                        "city" => $history->getLocation()->city,
                        "state" => $history->getLocation()->state,
                        "zip" => $history->getLocation()->zip,
                        "country" => $history->getLocation()->country
                    ],
                ];
            }
        }

        return $output;
    }

}
