

package com.qiyou.mb.android.beans.basic;

import android.util.Log; 
import com.google.gson.Gson;
import com.qiyou.mb.android.utils.Utils;

 
/*
<trkpt lat="31.266748" lon="120.725372"><ele>48.6</ele><time>2013-11-15T18:05:18.000Z</time><speed>4.61397</speed></trkpt> 
{lat:31.266748,lon:120.725372,ele:48.6,time:331234123234,speed:4.1265}
*/

public class Waypoint  {
	public static String TAG ="com.qiyou.beans.Waypoint";
	public static String dTAG ="com.qiyou";
	private Waypoint_bean wb ;
	private transient String jsonWp;
	public Waypoint_bean getWb() {
		if(null==wb) wb = new Waypoint_bean();
		return wb;
	}
	public void setWb(Waypoint_bean wb){
		this.wb=wb;
	}
	public   Waypoint()
	{
		 wb = new Waypoint_bean();
	}
	 /*
	  * construct by JSON string
	  */
	public   Waypoint(String jsonWp)
	{
//		long bfdbTime = System.currentTimeMillis();
		Gson gson = new Gson();		
//		try{			
		 
			wb = gson.fromJson(jsonWp, Waypoint_bean.class);
			this.jsonWp = jsonWp;		
	 
		
	}
	
	
	public String toJsonWp()
	{
//		long bfdbTime = System.currentTimeMillis();	
		
 
	//	if(null != wb.getLatitude())		 
		{		 
			  
			Gson gson = new Gson();	
 			try{
			jsonWp = gson.toJson(wb);
			}catch(Exception e)
			{
				Log.e(TAG,jsonWp);
				Utils.logStackTrace(e, dTAG);
			}
			
		} 
 		return jsonWp;
	}
	
	/**
	 * Returns a String representation of this waypoint.
	 */
	@Override
	public String toString() { 
		return  toJsonWp();
	}
	
	
	public String toGpxString()
	{
		return null;
	}
	 
	/**
	 * Compares two {@link Waypoint} objects based on latitude and longitude only
	 */
	 @Override
	public boolean equals(Object obj) {
		 
		 //TODO: check more, it will filter out some same wp for calculation.
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Waypoint other = (Waypoint) obj;
		if (wb.getLatitude() == 0f) {
			if (other.getWb().getLatitude() != 0f)
				return false;
		} else if (wb.getLatitude()!=(other.getWb().getLatitude()))
			return false;
		if (wb.getLongitude() == 0f) {
			if (other.getWb().getLongitude() != 0f)
				return false;
		} else if (wb.getLongitude()!=(other.getWb().getLongitude()))
			return false;
		return false;
	}
	 
	 /**
		 * Compares two {@link Waypoint} objects based on time stamp only to see if the new one is a young brother.
		 */ 
 public boolean isNewer(Object obj) {
		 
		 //TODO: check more, it will filter out some same wp for calculation.
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Waypoint other = (Waypoint) obj;
		return other.getWb().getTime() > wb.getTime();		 
	}
	 
	public String getJsonWp() {
		return jsonWp;
	}
	public void setJsonWp(String jsonWp) {
		this.jsonWp = jsonWp;
	} 

	
}

