/* 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 android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.*;

class Tour {
	public static final String[] PROJECTION = new String[] {
			Provider.Tours.ID, Provider.Tours.TITLE,
			Provider.Tours.DESCRIPTION,
			Provider.Tours.CREATED_DATE,
			Provider.Tours.MODIFIED_DATE,
			Provider.Tours.ROUTE};
	
	private Uri url;
	private JSONObject routeJSON;
	private Map<String, Waypoint> waypoints=new HashMap<String, Waypoint>();
	private List<Waypoint> route=new ArrayList<Waypoint>();
	private Cursor c;

	public Tour(Uri url, Cursor c) throws Exception {
		this.url=url;
		this.c=c;
		updateRouteFromJSON(c.getString(5));
	}
	
	List<Waypoint> getRoute() {
		return(route);
	}
	
	String getTitle() {
		return(c.getString(1));
	}
	
	void setTitle(String title) {
		c.updateString(1, title);
	}
	
	String getRouteAsJSON() {
		return(routeJSON.toString());
	}
	
	void updateRouteFromJSON(String path) throws Exception {
		BufferedReader in=new BufferedReader(new FileReader(path));
		StringBuffer buf=new StringBuffer();
		String str;
		
		while ((str = in.readLine()) != null) {
			buf.append(str);
		}
		
		in.close();

		routeJSON=new JSONObject(buf.toString());
		route.clear();
		waypoints.clear();
		
		JSONArray waypointsJSON=routeJSON.getJSONArray("waypoints");
		
		for (int i=0;i<waypointsJSON.length();i++) {
			Waypoint pt=new Waypoint(waypointsJSON.getJSONObject(i), path);
			
			waypoints.put(pt.getKey(), pt);
			route.add(pt);
		}
		
		JSONArray directionsJSON=routeJSON.getJSONArray("directions");
		
		for (int i=0;i<directionsJSON.length();i++) {
			Direction dir=new Direction(directionsJSON.getJSONObject(i));
			
			Waypoint from=waypoints.get(dir.getFromWaypointKey());
			
			if (from!=null) {
				dir.setFromWaypoint(from);
				from.setToDirection(dir);
			}
			
			Waypoint to=waypoints.get(dir.getToWaypointKey());
			
			if (to!=null) {
				dir.setToWaypoint(to);
				to.setFromDirection(dir);
			}
		}
		
		double cumulativeDistance=0.0;
		
		for (Waypoint pt : route) {
			if (pt.getFromDirection()!=null) {
				cumulativeDistance+=pt.getFromDirection().getDistance();
			}
			
			pt.setCumulativeDistance(cumulativeDistance);
		}
	}
	
	void prepForSave() throws Exception {
		BufferedWriter out=new BufferedWriter(new FileWriter(c.getString(5)));
		out.write(getRouteAsJSON());
		out.close();
		
		c.updateLong(4, Calendar.getInstance().getTimeInMillis()/1000);
	}
	
	Uri getUrl() {
		return(url);
	}
}