package uk.co.traintrackapp.traintrack.api;

import org.json.JSONException;
import org.json.JSONObject;

import uk.co.traintrackapp.traintrack.R;
import uk.co.traintrackapp.traintrack.model.Station;
import uk.co.traintrackapp.traintrack.utils.Utils;

public class CallingPoint {

    public static final int START = R.drawable.start;
    public static final int STOP = R.drawable.stop;
    public static final int END = R.drawable.end;

    private Station station;
    private String scheduledTimeArrival;
    private String estimatedTimeArrival;
    private String actualTimeArrival;
    private String scheduledTimeDeparture;
    private String estimatedTimeDeparture;
    private String actualTimeDeparture;
    private int icon;

    public CallingPoint() {
        this.station = new Station();
        this.scheduledTimeArrival = "";
        this.estimatedTimeArrival = "";
        this.actualTimeArrival = "";
        this.scheduledTimeDeparture = "";
        this.estimatedTimeDeparture = "";
        this.actualTimeDeparture = "";
    }

    public CallingPoint(JSONObject json) {
        this();
        try {

            if (json.has("station")) {
                this.station = new Station(json.getJSONObject("station"));
            }

            if (json.has("sta")) {
                this.scheduledTimeArrival = json.getString("sta");
            }

            if (json.has("eta")) {
                this.estimatedTimeArrival = json.getString("eta");
            }

            if (json.has("ata")) {
                this.actualTimeArrival = json.getString("ata");
            }

            if (json.has("std")) {
                this.scheduledTimeDeparture = json.getString("std");
            }

            if (json.has("etd")) {
                this.estimatedTimeDeparture = json.getString("etd");
            }

            if (json.has("atd")) {
                this.actualTimeDeparture = json.getString("atd");
            }

        } catch (JSONException e) {
            Utils.log(e.getMessage());
        }
    }

    public void setStation(Station station) {
        this.station = station;
    }

    public Station getStation() {
        return station;
    }

    public String getScheduledTimeArrival() {
        return scheduledTimeArrival;
    }

    public void setScheduledTimeArrival(String scheduledTimeArrival) {
        this.scheduledTimeArrival = scheduledTimeArrival;
    }

    public String getEstimatedTimeArrival() {
        return estimatedTimeArrival;
    }

    public void setEstimatedTimeArrival(String estimatedTimeArrival) {
        this.estimatedTimeArrival = estimatedTimeArrival;
    }

    public String getActualTimeArrival() {
        return actualTimeArrival;
    }

    public void setActualTimeArrival(String actualTimeArrival) {
        this.actualTimeArrival = actualTimeArrival;
    }

    public String getScheduledTimeDeparture() {
        return scheduledTimeDeparture;
    }

    public void setScheduledTimeDeparture(String scheduledTimeDeparture) {
        this.scheduledTimeDeparture = scheduledTimeDeparture;
    }

    public String getEstimatedTimeDeparture() {
        return estimatedTimeDeparture;
    }

    public void setEstimatedTimeDeparture(String estimatedTimeDeparture) {
        this.estimatedTimeDeparture = estimatedTimeDeparture;
    }

    public String getActualTimeDeparture() {
        return actualTimeDeparture;
    }

    public void setActualTimeDeparture(String actualTimeDeparture) {
        this.actualTimeDeparture = actualTimeDeparture;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public int getIcon() {
        return icon;
    }

    public String toString() {
        return getScheduledTimeDeparture() + " " + getStation() + " " + getTime() + "\n";
    }

    public boolean hasArrived() {
        return !getActualTimeArrival().isEmpty();
    }

    public boolean isOnTime() {
        return getEstimatedTimeArrival().equalsIgnoreCase(getScheduledTimeArrival());
    }

    public String getScheduledTime() {
        String std = getScheduledTimeDeparture();
        if ((std != null) && (!std.isEmpty())) {
            return std;
        } else {
            return getScheduledTimeArrival();
        }
    }

    public String getTime() {
        if (hasArrived()) {
            return getActualTimeArrival();
        } else if (isOnTime()) {
            return getEstimatedTimeArrival();
        } else {
            return getScheduledTimeArrival();
        }
    }

}
@@ -1127,32 +1127,36 @@
         if (
+!
 json.
-has
+isNull
 (%22station%22))
@@ -1260,32 +1260,36 @@
         if (
+!
 json.
-has
+isNull
 (%22sta%22)) %7B%0A 
@@ -1381,32 +1381,36 @@
         if (
+!
 json.
-has
+isNull
 (%22eta%22)) %7B%0A 
@@ -1502,32 +1502,36 @@
         if (
+!
 json.
-has
+isNull
 (%22ata%22)) %7B%0A 
@@ -1624,24 +1624,28 @@
     if (
+!
 json.
-has
+isNull
 (%22std%22))
@@ -1747,24 +1747,28 @@
     if (
+!
 json.
-has
+isNull
 (%22etd%22))
@@ -1874,16 +1874,20 @@
 if (
+!
 json.
-has
+isNull
 (%22at
@@ -2006,24 +2006,24 @@
 eption e) %7B%0A
-
             
@@ -2032,16 +2032,36 @@
 ils.log(
+%22Calling Point: %22 + 
 e.getMes

Change to check for not nulls