/* 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 java.util.Date;
import org.json.*;

class Direction {
	private JSONObject data;
	private Waypoint from;
	private Waypoint to;

	public Direction(JSONObject data) {
		this.data=data;
	}
	
	void setToWaypoint(Waypoint pt) {
		to=pt;
	}
	
	void setFromWaypoint(Waypoint pt) {
		from=pt;
	}
	
	String getFromWaypointKey() {
		return(getString("from", null));
	}
	
	String getToWaypointKey() {
		return(getString("to", null));
	}
	
	Waypoint getFromWaypoint() {
		return(from);
	}
	
	Waypoint getToWaypoint() {
		return(to);
	}
	
	Double getDistance() {
		return(getDouble("distance", null));
	}
	
	void setDistance(Double value) throws Exception {
		data.put("distance", value);
	}
	
	String getTurn() {
		return(getString("turn", null));
	}
	
	String getMarker() {
		return(getString("marker", null));
	}
	
	String getHint() {
		return(getString("hint", null));
	}
	
	private String getString(String key) {
		return(getString(key, "***MISSING***"));
	}
	
	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, 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);
	}
}