package com.cocoahero.android.geojson;

import java.util.Arrays;

import org.json.JSONArray;
import org.json.JSONException;

import android.location.Location;

public class Position {

    // ------------------------------------------------------------------------
    // Private Constants
    // ------------------------------------------------------------------------

    private static final int LON_IDX = 0;

    private static final int LAT_IDX = 1;

    private static final int ALT_IDX = 2;

    // ------------------------------------------------------------------------
    // Instance Variables
    // ------------------------------------------------------------------------

    private final double[] mStorage = new double[3];

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    public Position(JSONArray array) {
        this.mStorage[LON_IDX] = array.optDouble(LON_IDX, 0);
        this.mStorage[LAT_IDX] = array.optDouble(LAT_IDX, 0);
        this.mStorage[ALT_IDX] = array.optDouble(ALT_IDX, 0);
    }

    public Position(double[] array) {
        if (array.length == 2) {
            this.mStorage[LON_IDX] = array[LON_IDX];
            this.mStorage[LAT_IDX] = array[LAT_IDX];
        }
        else if (array.length == 3) {
            this.mStorage[LON_IDX] = array[LON_IDX];
            this.mStorage[LAT_IDX] = array[LAT_IDX];
            this.mStorage[ALT_IDX] = array[ALT_IDX];
        }
    }

    public Position(double latitude, double longitude) {
        this.mStorage[LAT_IDX] = latitude;
        this.mStorage[LON_IDX] = longitude;
    }

    public Position(double latitude, double longitude, double altitude) {
        this.mStorage[LAT_IDX] = latitude;
        this.mStorage[LON_IDX] = longitude;
        this.mStorage[ALT_IDX] = altitude;
    }

    public Position(Location location) {
        this.mStorage[LAT_IDX] = location.getLatitude();
        this.mStorage[LON_IDX] = location.getLongitude();
        this.mStorage[ALT_IDX] = location.getAltitude();
    }

    // ------------------------------------------------------------------------
    // Public Methods
    // ------------------------------------------------------------------------

    public double getLatitude() {
        return this.mStorage[LAT_IDX];
    }

    public void setLatitude(double latitude) {
        this.mStorage[LAT_IDX] = latitude;
    }

    public double getLongitude() {
        return this.mStorage[LON_IDX];
    }

    public void setLongitude(double longitude) {
        this.mStorage[LON_IDX] = longitude;
    }

    public double getAltitude() {
        return this.mStorage[ALT_IDX];
    }

    public void setAltitude(double altitude) {
        this.mStorage[ALT_IDX] = altitude;
    }

    public JSONArray toJSON() throws JSONException {
        JSONArray coordinates = new JSONArray();

        coordinates.put(LAT_IDX, this.getLatitude());
        coordinates.put(LON_IDX, this.getLongitude());
        coordinates.put(ALT_IDX, this.getAltitude());

        return coordinates;
    }

    public Location toLocation() {
        Location location = new Location("GeoJSON");

        location.setLatitude(this.getLatitude());
        location.setLongitude(this.getLongitude());
        location.setAltitude(this.getAltitude());

        return location;
    }

    public double[] toArray() {
        return this.mStorage;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }

        if (!(object instanceof Position)) {
            return false;
        }

        Position aPosition = (Position) object;

        return Arrays.equals(this.mStorage, aPosition.mStorage);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(this.mStorage);
    }

    @Override
    public String toString() {
        return Arrays.toString(this.mStorage);
    }

}

@@ -156,30 +156,108 @@
 on;%0A
-%0Apublic class Position
+import android.os.Parcel;%0Aimport android.os.Parcelable;%0A%0Apublic class Position implements Parcelable
  %7B%0A%0A
@@ -2204,24 +2204,840 @@
 e();%0A    %7D%0A%0A
+    private Position(Parcel parcel) %7B%0A        this(parcel.createDoubleArray());%0A    %7D%0A%0A    // ------------------------------------------------------------------------%0A    // Parcelable Interface%0A    // ------------------------------------------------------------------------%0A%0A    public static final Parcelable.Creator%3CPosition%3E CREATOR = new Parcelable.Creator%3CPosition%3E() %7B%0A        @Override%0A        public Position createFromParcel(Parcel in) %7B%0A            return new Position(in);%0A        %7D%0A%0A        @Override%0A        public Position%5B%5D newArray(int size) %7B%0A            return new Position%5Bsize%5D;%0A        %7D%0A    %7D;%0A%0A    @Override%0A    public int describeContents() %7B%0A        return 0;%0A    %7D%0A%0A    @Override%0A    public void writeToParcel(Parcel dest, int flags) %7B%0A        dest.writeDoubleArray(this.mStorage);%0A    %7D%0A%0A
     // -----

Add Parcelable support to Position.