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

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

import android.text.TextUtils;

import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.utils.Utils;
import com.qiyou.mb.android.utils.enums.TrackAvStatus;
import com.qiyou.mb.android.utils.enums.TrackStatus;
import com.qiyou.mb.android.utils.enums.TrackType;
import com.qiyou.mb.android.utils.log.QLog;

public class Track_bean {
	public Track_bean() {
		// TODO Auto-generated constructor stub
		
	} 
	private long creatTime;
	private String creator;
	private String name="";
	private String cmt;
	private String desc;
	private float distance =0.0f;
	private float eUpDist =0.0f;   //累积上升
	private float eDownDist =0.0f; //累积下降
	private float topSpeed =0.0f;
	private int userid;
    private Waypoint startWp;
	private Waypoint lastWp;
	private long startTime;
	private long endTime;
	private int trackId;
	private long pause=0  ;
	private String src;
	private int svrNumber; //编号,服务器端的记录编号
//	private int type; //0,1,2...(bike,walk,car)
	private TrackType type;
	private long sportTime;
	private long lastdbTime;
	private int isCheckedIn; //0,1 ( false,true)
//	private CopyOnWriteArrayList<Waypoint_bean> trkseg;
	private  ArrayList<Waypoint_bean> trkseg;
	private TrackStatus status =TrackStatus.PENING; //0,1,2,3,...(pending,recording,paused,finished,...)
	private long offTime; //no gps time
	private int gpsSpan =2000;
//	private CopyOnWriteArrayList<Waypoint_bean> roadWps = new CopyOnWriteArrayList<Waypoint_bean>();
	private ArrayList<Waypoint_bean> roadWps = new ArrayList<Waypoint_bean>();	
	private Waypoint maxDistWp; 
	private float maxR;//max radius 
	private float burn;//卡路里
//	private CopyOnWriteArrayList<Waypoint_bean> mdWps = new CopyOnWriteArrayList<Waypoint_bean>();
	private ArrayList<Waypoint_bean> mdWps = new ArrayList<Waypoint_bean>();	
	private long totalTime=0;
	private TrackAvStatus avStatus =TrackAvStatus.ACTIVE;
	private String summary;
	private String userName;
	private int userScore;
	private String staticMapUrl;
//	private ArrayList<String> thumbUrls;
	/**
	 * Returns the name of this track.
	 * @return A String representing the name of this track.
	 */
	public String getName() {
		
		if(TextUtils.isEmpty(name) && trackId>0)
			name ="No."+ trackId;
		return name;
	}
	
	/**
	 * Setter for track name property. This maps to &lt;name&gt; tag value.
	 * @param name A String representing the name of this track.
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * Returns the comment of this track.
	 * @return A String representing the comment of this track.
	 */
	public String getComment() {
		return cmt;
	}
	
	/**
	 * Setter for track comment property. This maps to &lt;comment&gt; tag value.
	 * @param comment A String representing the comment of this track.
	 */
	public void setComment(String comment) {
		cmt = comment;
	}
	
	/**
	 * Returns the description of this track.
	 * @return A String representing the description of this track.
	 */
	public String getDescription() {
		return desc;
	}
	
	/**
	 * Setter for track description property. This maps to &lt;description&gt; tag value.
	 * @param description A String representing the description of this track.
	 */
	public void setDescription(String description) {
		 desc = description;
	}
	
	/**
	 * Returns the src of this track.
	 * @return A String representing the src of this track.
	 */
	public String getSrc() {
		return src;
	}
	
	/**
	 * Setter for src type property. This maps to &lt;src&gt; tag value.
	 * @param src A String representing the src of this track.
	 */
	public void setSrc(String src) {
		this.src = src;
	}
	
/*	*//**
	 * Returns the number of this track.
	 * @return A String representing the number of this track.
	 *//*
	public int getNumber() {
		return svrNumber;
	}
	
	*//**
	 * Setter for track number property. This maps to &lt;number&gt; tag value.
	 * @param number An int representing the number of this track.
	 *//*
	public void setNumber(int number) {
		this.svrNumber = number;
	}
	*/
	/**
	 * Returns the type of this track.
	 * @return A String representing the type of this track.
	 */
	public TrackType getType() {
		if(type==null) type=TrackType.BIKE;
		return type;
	}
	
	/**
	 * Setter for track type property. This maps to &lt;type&gt; tag value.
	 * @param type A String representing the type of this track.
	 */
	public void setType(TrackType type) {
		this.type = type;
	}
	
	/**
	 * Getter for the list of waypoints of a track.
	 * @return an ArrayList of {@link Waypoint} representing the points of the track.
	 */
	
/*	public CopyOnWriteArrayList<Waypoint_bean> getTrkseg() {
		if(trkseg == null) trkseg= new CopyOnWriteArrayList<Waypoint_bean>();
		return trkseg;
	}*/
	public  ArrayList<Waypoint_bean> getTrkseg() {
		if(trkseg == null) trkseg= new  ArrayList<Waypoint_bean>();
		return trkseg;
	}
	
	/**
	 * Setter for the list of waypoints of a track.
	 * @param trackPoints an ArrayList of {@link Waypoint} representing the points of the track.
	 */
	/*public void setTrksegs(CopyOnWriteArrayList<Waypoint_bean> trkseg) {
		this.trkseg = trkseg;
	}*/
	
	public void setTrksegs( ArrayList<Waypoint_bean> trkseg) {
		this.trkseg = trkseg;
	}
	
/*	public String toGsonString()
	{
		String strwb ="";
		if(null != number)		 
		{		 
			Gson gson = new Gson();	
			try{
			strwb = gson.toJson(this);
			}catch(Exception e)
			{
				Log.e( e.getStackTrace().toString());
			}
			
		}
		return strwb;
	}*/

	/**
	 * @return the dist
	 */
	public float getDistance() {
//		dist=getAvs()>0?dist:0;
//		return distance;
//		return getSportTime() >0?dist:0; 
//		distance =this.getLastWp().getWb().getSumDist();
		return distance;
	}

	/**
	 * @param dist the dist to set
	 */
	public void setDistance(float dist) {
//		this.distance = dist>distance?dist:distance;
		this.distance = dist;
	}

	/**
	 * @return the pause
	 */
	public long getPauseTime() {
		
//		pause= dist>0?pause: endTime  - getStartTime() -  offTime;	
		
//		pause =endTime-startTime-getSportTime()-offTime;
//		pause =getTotalTime()-getSportTime();
//		pause =getTotalTime()-getSportTime()-offTime;
		return pause >0?pause:0;		
	}
	public long getDeltaPause(){
		return getPauseTime()-pause;
	}

	/**
	 * @param pause the pause to set
	 */
	public void setPauseTime(long pause) {
		if(pause<=0)return;
		
		long old= this.pause;
		this.pause = pause;
		
		if(this.getAvs()>this.topSpeed){
			this.pause= old;
		}
		
		
	}

	/**
	 * @return the userid
	 */
	public int getUserid() {
		return userid;
	}

	/**
	 * @param userid the userid to set
	 */
	public void setUserid(int userid) {
		this.userid = userid;
	}

	/**
	 * @return the startTime
	 */
	public long getStartTime() {
//		if(startTime ==0){
//			if(startWp !=null)
//				startTime =startWp.getWb().getTime();
//			else
//				startTime = System.currentTimeMillis();
//				
//			
//		}
		startTime=startTime>0?startTime:(startWp!=null?startWp.getWb().getTime():0);
//		if(type==TrackType.GPX){
//			startTime=startWp!=null?startWp.getWb().getTime():1;
//		}
		return startTime;
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(long stTime) {
		 startTime =startTime==0?stTime:startTime;
//						startTime>stTime?stTime:startTime;//取小
	}

	/**
	 * @return the endTime
	 */
	public long getEndTime() {
		
		return endTime>=this.getLastWp().getWb().getTime()?endTime:this.getLastWp().getWb().getTime();
	}

	/**
	 * @return
	 */
	public long getTotalTime() {
		totalTime =getEndTime() -getStartTime();
		return totalTime;
// 		return endTime -getStartTime();// +this.getOffTime();
/*//		totalTime =totalTime>this.sportTime+this.pause+this.offTime?totalTime: this.sportTime+this.pause+this.offTime;
		totalTime =sportTime+pause+offTime;
		totalTime=totalTime>(endTime -getStartTime()) ||type==TrackType.GPX ?endTime -getStartTime():totalTime;
//		if(type==TrackType.GPX){
//			totalTime=endTime -getStartTime();
//		}
		return totalTime;//this.sportTime+this.pause+this.offTime;
*/	}
	
 
	
//	public void setTotalTime(long totalTime) {
//		this.totalTime = totalTime;
//	}

	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime(long endTime) {
		this.endTime = endTime>this.endTime?endTime:this.endTime;
	}

	/**
	 * @return the isCheckedIn
	 */
	public int getIsCheckedIn() {
		return isCheckedIn;
	}

	/**
	 * @param isCheckedIn the isCheckedIn to set
	 */
	public void setIsCheckedIn(int isCheckedIn) {
		this.isCheckedIn = isCheckedIn;
	}

	/**
	 * @return the trackid
	 */
	public int getTrackId() {
		return trackId;
	}

	/**
	 * @param trackid the trackid to set
	 */
	public void setTrackId(int trackId) {
		this.trackId = trackId;
	}

	 public Waypoint getLastWp() {
		if(lastWp ==null )lastWp = new Waypoint();
		 return lastWp;
	}

	public void setLastWp(Waypoint lastWp) {
		this.lastWp = lastWp;
	}

	public Waypoint getStartWp() {
		return startWp;
	}

	public void setStartWp(Waypoint startWp) {
		if(startWp==null)return;
		
		this.startWp = startWp;
//		long stTime=System.currentTimeMillis();
//		stTime = stTime>=startWp.getWb().getTime()?stTime:startWp.getWb().getTime();
//	 	setStartTime(stTime);
//	 	setStartTime(System.currentTimeMillis());
		setStartTime(startWp.getWb().getTime());
//	 	startTime =startTime==0?System.currentTimeMillis():startTime;
	 	
	}

	public long getSportTime() {
// 		sportTime =  getLastWp().getWb().getTime()  - getStartTime() -  pause -offTime; 
		
//		sportTime =sportTime==0?getLastWp().getWb().getSportTime():sportTime;
		
//		return getLastWp().getWb().getSportTime()>0?getLastWp().getWb().getSportTime():sportTime;//getLastWp().getWb().getSportTime();
//		return  getLastWp().getWb().getSportTime()>60*1000?getLastWp().getWb().getSportTime():0;  
		sportTime =sportTime<getLastWp().getWb().getSportTime()?getLastWp().getWb().getSportTime():sportTime;//getLastWp().getWb().getSportTime();
		
		sportTime = sportTime>0? sportTime:getLastWp().getWb().getSportTime();//getLastWp().getWb().getSportTime();
		sportTime=sportTime >getTotalTime()?getTotalTime():sportTime;
		return sportTime;
	}

	public long getLastdbTime() {
		return lastdbTime;
	}

	public void setLastdbTime(long lastdbTime) {
		this.lastdbTime = lastdbTime;
	}

	public float geteUpDist() {
		return eUpDist;
	}

	public float getEsumUp() {
		return eUpDist ;//+ eDownDist;
	}

	
	public void seteUpDist(float eUpDist) {
		this.eUpDist = eUpDist;
	}

	public float geteDownDist() {
		
		return  eDownDist ;
		
	}

	public void seteDownDist(float eDownDist) {
		this.eDownDist = eDownDist;
	}

	public float getTopSpeed() {
		return topSpeed;
	}

	public void setTopSpeed(float topSpeed) {
		if(topSpeed>this.topSpeed)
			this.topSpeed = topSpeed;
	}

	public TrackStatus getStatus() {
		return status;
	}

	public void setStatus(TrackStatus status) {
		this.status = status;
	}

	public long getOffTime() {
		return offTime;
	}

	public void setOffTime(long offTime) {
		this.offTime = offTime;
	}

	public int getGpsSpan() {
		return gpsSpan;
	}

	public void setGpsSpan(int gpsSpan) {
		this.gpsSpan = gpsSpan;
	}

	/*
	 * km/h
	 */
	public float getAvs() {  
		if(getCreator()!=null && getCreator().startsWith("爱骑驴路书"))return getTavs();
//		float avs =TrackType.COPY==this.getType()?0:getSportTime()>1000? 3600f*dist/getSportTime():0;
		float avs = getSportTime()>10000? 3600f*distance/getSportTime():0;
//		QLog.getLogger().dLog("com.qiyoutop.qnaviSvc","com.qiyou.qnaviSvc", "  getSportTime:   "+getSportTime() );
//		QLog.getLogger().dLog("com.qiyoutop.qnaviSvc","com.qiyou.qnaviSvc", "  dist:   "+dist );
		if(type==TrackType.GPX &&avs>topSpeed){ 
			topSpeed=avs;
			return avs;
		}
		if(avs>topSpeed) avs =3600f*distance/getTotalTime();// topSpeed;
//		QLog.getLogger().dLog("com.qiyoutop.qnaviSvc","com.qiyou.qnaviSvc", "  app.getCurrentTrack().getTrackBean().getAvs():   "+avs );
//		if(avs>topSpeed) return topSpeed;
		return avs;
	}
	
	public float getCAvs() {
 
		float avs = getSportTime()>10000? 3600f*distance/getSportTime():0;
//    	if(avs>topSpeed) return 0;
//		QLog.getLogger().dLog("com.qiyoutop.qnaviSvc","com.qiyou.qnaviSvc", "  app.getCurrentTrack().getTrackBean().getAvs():   "+avs );
		return avs;
 
	}

	public float getTavs() {
//		float tavs =getSportTime()>1000? 3600f*dist/(endTime-getStartTime()):0;
		float tavs =getTotalTime()>10000 || getType()==TrackType.GPX? 3600f*distance/getTotalTime():0; 
		
		if(type==TrackType.GPX &&tavs>topSpeed){ 
//			topSpeed=tavs;
			return tavs;
		}
		
//		if(type!=TrackType.GPX &&(tavs>topSpeed ||(type!=TrackType.GPX &&  tavs> getAvs())||tavs<0)) tavs=0;
		
		return tavs;
//		return getAvs();
	}

	
	/*public CopyOnWriteArrayList<Waypoint_bean> getRoadWps() { 
		 if(roadWps!=null && roadWps.size()>1000)
			 roadWps=Utils.getCleanWbs(roadWps);
		return roadWps;
	}

	public void setRoadWps(CopyOnWriteArrayList<Waypoint_bean> roadWps) {
		try{
			this.roadWps = roadWps;
		}catch(Exception e){
			Utils.logStackTrace(e, "trackbean");
			
		}
	}*/

	public  ArrayList<Waypoint_bean> getRoadWps() { 
		 if(roadWps!=null && roadWps.size()>1000)
			 roadWps=Utils.getCleanWbs(roadWps);
		return roadWps;
	}

	public void setRoadWps( ArrayList<Waypoint_bean> roadWps) {
		try{
			this.roadWps = roadWps;
		}catch(Exception e){
			Utils.logStackTrace(e, "trackbean");
			
		}
	}
	public Waypoint getMaxDistWp() {
		if(maxDistWp==null) maxDistWp =new Waypoint();
		return  maxDistWp;
	}

	public void setMaxDistWp(Waypoint  maxDistWp) {
		 this.maxDistWp =  maxDistWp;
	}

	public float getMaxR() {
		return maxR;
	}

	public void setMaxR(float maxRdist) {
		 maxR = maxRdist;
	}

	/*public CopyOnWriteArrayList<Waypoint_bean> getMdWps() {
		return mdWps;
	}

	public void setMdWps(CopyOnWriteArrayList<Waypoint_bean> mdWps) {
		this.mdWps = mdWps;
	} */
	
	public ArrayList<Waypoint_bean> getMdWps() {
		return mdWps;
	}

	public void setMdWps( ArrayList<Waypoint_bean> mdWps) {
		this.mdWps = mdWps;
	} 

	public void setSportTime(long sportTime) {
		this.sportTime = sportTime;
	}

	public float getBurn() {
		return burn;
	}

	public void setBurn(float burn) {
		this.burn = burn;
	}

	public long getNonSportTime(){
		return  getTotalTime() - getSportTime();
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		if(!TextUtils.isEmpty(creator) && !creator.equalsIgnoreCase("null"))
			this.creator = creator;
	}

	public TrackAvStatus getAvStatus() {		
		return avStatus;
	}

	public void setAvStatus(TrackAvStatus avStatus) {
		this.avStatus = avStatus;
	}

	public long getCreatTime() {
		return creatTime;
	}

	public void setCreatTime(long creatTime) {
		this.creatTime = creatTime;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public int getSvrNumber() {
		return svrNumber;
	}

	public void setSvrNumber(int svrNumber) {
		this.svrNumber = svrNumber;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getUserScore() {
		
		return userScore;
	}

	public void setUserScore(int userScore) {
		this.userScore = userScore;
	}
	public String getStaticMapUrl() {
		return staticMapUrl;
	}

	public void setStaticMapUrl(String staticMapUrl) {
		this.staticMapUrl = staticMapUrl;
	}
	private String[] thumbUrls;
	public String[] getThumbUrls() {
//		if(thumbUrls==null)thumbUrls=new String[1];
		return thumbUrls;
	}

	public void setThumbUrls(String[] thumbUrls) {
		this.thumbUrls = thumbUrls;
	}
/*
	public ArrayList<String> getThumbUrls() {
		if(thumbUrls==null)thumbUrls=new ArrayList<String>();
		return thumbUrls;
	}

	public void setThumbUrls(ArrayList<String> thumbUrls) {
		this.thumbUrls = thumbUrls;
	}
*/
	
	public float getUpDist() { 
		return this.getLastWp().getWb().getUpDist();
	} 
	 
	public float getDownDist() {
//		return downDist<-1*distance*0.07f? downDist - -1*distance*0.07f:0f;
		return this.getLastWp().getWb().getDownDist();
	}

 
	public float getMaxEle() {
		return  this.getLastWp().getWb().getMaxEle();
	}
 
	public float getMinEle() {
		float minEle=this.getLastWp().getWb().getMinEle();
//		minEle=minEle>getMaxEle()?getMaxEle():minEle;
//		return this.getLastWp().getWb().getMinEle();
		float max=getMaxEle();
		minEle=(max>QiYouConstant.MAX_ELE*-1 &&minEle>max)?max:minEle;
		return minEle;
//		return minEle>getMaxEle()?getMaxEle():minEle;
	}
 
 public long getKmTime(){
	 
//	 long v=(long)(this.getTotalTime()/this.getDistance()/1000);
//	 return Utils.getTimeCnt(v);
//	 return  (long)(this.getTotalTime()/this.getDistance()*1000);
	 if(getDistance()<100)
		 return 0;
	 return  (long)(this.getSportTime()/this.getDistance()*1000);
 }
}

