/* Copyright (c) 2008 -- CommonsWare, LLC

	 Licensed under the Apache License, Version 2.0 (the "License");
	 you may not use this file except in compliance with the License.
	 You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

	 Unless required by applicable law or agreed to in writing, software
	 distributed under the License is distributed on an "AS IS" BASIS,
	 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 See the License for the specific language governing permissions and
	 limitations under the License.
*/
	 
package com.commonsware.tourit;

import com.google.android.maps.Point;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.*;

class Waypoint {
	private JSONObject data;
	private Direction from;
	private Direction to;
	private double cumulativeDistance;
	private Point position=null;

	public Waypoint(JSONObject data, String path) throws Exception {
		this.data=data;
	}
	
	String getKey() throws org.json.JSONException {
		return(data.getString("key"));
	}
	
	String getTitle() {
		return(getString("title"));
	}
	
	String getDescription() {
		return(getString("desc"));
	}
	
	Double getLatitude() {
		return(getDouble("latitude"));
	}
	
	Double getLongitude() {
		return(getDouble("longitude"));
	}
	
	Point getPosition() {
		if (position==null) {
			Double lat=getLatitude();
			Double lon=getLongitude();
	
			if (lat!=null && lon!=null) {
				position=new Point((int) (lat*1E6), (int) (lon*1E6));
			}
		}
		
		return(position);
	}
	
	Double getElevation() {
		return(getDouble("elevation"));
	}
	
	void setLatitude(Double value) throws Exception {
		data.put("latitude", value);
		position=null;
	}
	
	void setLongitude(Double value) throws Exception {
		data.put("longitude", value);
		position=null;
	}
	
	void setElevation(Double value) throws Exception {
		data.put("elevation", value);
	}
	
	String getDataSource() {
		return(getString("source"));
	}
	
	void setFromDirection(Direction dir) {
		from=dir;
	}
	
	void setToDirection(Direction dir) {
		to=dir;
	}
	
	Direction getFromDirection() {
		return(from);
	}
	
	Direction getToDirection() {
		return(to);
	}
	
	public double getCumulativeDistance() {
		return(cumulativeDistance);
	}
	
	public void setCumulativeDistance(double value) {
		cumulativeDistance=value;
	}
	
	public String toString() {
		return(getTitle());
	}
	
	private String getString(String key) {
		return(getString(key, null));
	}
	
	private String getString(String key, String defaultValue) {
		String result=defaultValue;
		
		try {
			result=data.getString(key);
		}
		catch (org.json.JSONException e) {
//			android.util.Log.e("TourIt", "Exception parsing "+key, e);
		}
		
		return(result);
	}
	
	private Double getDouble(String key) {
		return(getDouble(key, null));
	}
	
	private Double getDouble(String key, Double defaultValue) {
		Double result=defaultValue;
		
		try {
			result=data.getDouble(key);
		}
		catch (org.json.JSONException e) {
			// android.util.Log.e("TourIt", "Exception parsing "+key, e);
		}
		
		return(result);
	}
}