package com.alwaysallthetime.adnlibutils.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.alwaysallthetime.adnlib.data.Annotation;

import java.util.HashMap;

public class DisplayLocation implements Parcelable {
    public enum LocationType {
        CHECKIN,
        OHAI,
        GEOLOCATION;

        public static LocationType fromInt(int ordinal) {
            switch(ordinal) {
                case 0:
                    return CHECKIN;
                case 1:
                    return OHAI;
                case 2:
                default:
                    return GEOLOCATION;
            }
        }
    }

    private LocationType mType;
    private String mName;
    private String mFactualId;
    private double mLatitude;
    private double mLongitude;

    public static DisplayLocation fromCheckinAnnotation(Annotation checkin) {
        HashMap<String, Object> value = checkin.getValue();
        String locationName = (String) value.get("name");
        String factualId = (String) value.get("factual_id");
        DisplayLocation loc = new DisplayLocation(locationName, factualId, (Double)value.get("latitude"), (Double)value.get("longitude"));
        loc.setType(LocationType.CHECKIN);
        return loc;
    }

    public static DisplayLocation fromOhaiLocation(Annotation ohaiLocation) {
        HashMap<String, Object> value = ohaiLocation.getValue();
        String name = (String) value.get("name");
        Double latitude = (Double) value.get("latitude");
        Double longitude = (Double) value.get("longitude");
        if(name == null) {
            if(latitude != null && longitude != null) {
                name = getLatLongString(latitude, longitude);
            }
        }
        DisplayLocation loc = new DisplayLocation(name, (Double)value.get("latitude"), (Double)value.get("longitude"));
        loc.setType(LocationType.OHAI);
        return loc;
    }

    public static DisplayLocation fromGeolocation(Geolocation geolocation) {
        DisplayLocation loc = new DisplayLocation(geolocation.getName(), geolocation.getLatitude(), geolocation.getLongitude());
        loc.setType(LocationType.GEOLOCATION);
        return loc;
    }

    public DisplayLocation(String name, double latitude, double longitude) {
        mLatitude = latitude;
        mLongitude = longitude;
        mName = name;
    }

    protected void setType(LocationType type) {
        mType = type;
    }

    protected DisplayLocation(String name, String factualId, double latitude, double longitude) {
        this(name, latitude, longitude);
        mFactualId = factualId;
    }

    public String getName() {
        return mName;
    }

    public double getLatitude() {
        return mLatitude;
    }

    public double getLongitude() {
        return mLongitude;
    }

    public String getFactualId() {
        return mFactualId;
    }

    private static String getLatLongString(double latitude, double longitude) {
        return String.format("%s, %s", String.valueOf(latitude), String.valueOf(longitude));
    }

    public LocationType getType() {
        return mType;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mName);
        dest.writeDouble(mLatitude);
        dest.writeDouble(mLongitude);
        dest.writeInt(mType.ordinal());
        if(mFactualId != null) {
            dest.writeInt(1);
            dest.writeString(mFactualId);
        } else {
            dest.writeInt(0);
        }
    }

    public final static Parcelable.Creator<DisplayLocation> CREATOR = new Parcelable.Creator<DisplayLocation>() {

        @Override
        public DisplayLocation createFromParcel(Parcel source) {
            String name = source.readString();
            double latitude= source.readDouble();
            double longitude = source.readDouble();
            int type = source.readInt();
            int hasFactualId = source.readInt();
            String factualId = null;
            if(hasFactualId == 1) {
                factualId = source.readString();
            }
            DisplayLocation loc = new DisplayLocation(name, factualId, latitude, longitude);
            loc.setType(LocationType.fromInt(type));
            return loc;
        }

        @Override
        public DisplayLocation[] newArray(int size) {
            return new DisplayLocation[size];
        }
    };
}

@@ -313,16 +313,33 @@
 LOCATION
+,%0A        UNKNOWN
 ;%0A%0A     
@@ -562,16 +562,56 @@
 case 2:%0A
+                    return GEOLOCATION;%0A
         
@@ -650,34 +650,30 @@
      return 
-GEOLOCATIO
+UNKNOW
 N;%0A         
@@ -2415,24 +2415,62 @@
 ame = name;%0A
+        mType = LocationType.UNKNOWN;%0A
     %7D%0A%0A    p

add an unknown location type