 
package com.qiyou.mb.android.ui.charts;

import java.util.ArrayList;
import org.achartengine.ChartFactory;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.qiyou.mb.android.beans.Track;
import com.qiyou.mb.android.beans.basic.Waypoint_bean;
import com.qiyou.mb.android.utils.Utils;
import com.qiyou.mb.android.utils.log.QLog;

/**
 * Average temperature demo chart.
 */
public class DistanceChart extends AbstractChart {
	public static final String TAG ="com.qiyou.DistanceChart";
	public static final String dTAG ="com.qiyou";
	 
   	
  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  @Override
public Intent execute(Context context) {
	  
    buildChartData(); 
    Intent intent = null;
    try{
      intent = ChartFactory.getLineChartIntent(context, dataset, renderer,
        "距离-时间");
   
    }catch(Exception er)
    {
    	Utils.logStackTrace(er, TAG);
    }
     return intent;
  }
  
 /*
  *  (non-Javadoc)
  * @see com.qiyou.mb.android.ui.charts.AbstractChart#getChartView(android.content.Context)
  */
  @Override
 public View getChartView(Context context)
  {
	  View v = null;
	  try{
		  buildChartData(); 
		  v= ChartFactory.getLineChartView(context, dataset, renderer);
	   
	    }catch(Exception er)
	    {
	    	Utils.logStackTrace(er, TAG);
	    }
	  
	  return v;
	  
	 
  }
 
 @Override
 public void buildChartData(){
	    getTitles();
	    getXYvalues();
	    dataset = buildDataset(titles, xValues, yValues);
	    renderer = getRender(2, "距离-时速-时间 ", "时间(分钟)", "");
 }
  
 
  void getTitles(){ 
	  if(trs.size()==0)return;
	  titles = new String[trs.size()*2];
	   int i=0;
	   String name="ID";
	   for(Track tr:trs){
		   if(tr ==null)continue;
		   name =name+tr.getTrackBean().getTrackId();
		   name = String.valueOf((TextUtils.isEmpty(name)?tr.getTrackBean().getStartTime():name));
//		   titles[i] =name + "距离";
//		   i++;
//		   titles[i] =name + "时速";
		   titles[i] =  "距离(公里)";
		   i++;
		   titles[i] =  "时速(公里/小时)";
		   i++;
	   }
 
  
  }
  

  
  
 @Override
public double getMaxY(){
	  double maxY =0;
	  for(Track tr:trs){
		  if(tr ==null)continue;
//		  maxSportTime = tr.getTrackBean().getSportTime()>maxSportTime? tr.getTrackBean().getSportTime():maxSportTime;
		 
		  maxY = tr.getTrackBean().getDistance()/1000.0>maxY? tr.getTrackBean().getDistance()/1000.0:maxY;
		  maxY = tr.getTrackBean().getTopSpeed()>maxY? tr.getTrackBean().getTopSpeed():maxY;
	  } 
	  
	  return maxY;
 }
 

	public ArrayList<Track> getTrs() { 
		return trs;
	}

	void getXYvalues (){  
		   
	      xValues = new ArrayList<double[]>();
		  
		  double maxSportTime = getMaxTime(); 
		  XstepValue = maxSportTime/Xcnt;
//		  double[]  xD ; 
		  ArrayList<Double>  xD ;
		  ArrayList<Double>   xS ; 
	   
		  yValues = new ArrayList<double[]>();    
//		  double[]  yD; 
		  ArrayList<Double>  yD;
		  ArrayList<Double>   yS;  
		  
	  for(Track tr:trs){
		  if(tr==null)continue;
		  ArrayList<Waypoint_bean> al = new ArrayList<Waypoint_bean>();
		  
	         for(Waypoint_bean b:tr.getTrackBean().getTrkseg())
	         {
	         	if (null !=b)
	         		al.add(b);
	         }
	          
	       yD =new ArrayList<Double>();
	       yD.add(0,  0D);
	       yS =new ArrayList<Double>();
	       yS.add(0,  0D);
	       xD = new ArrayList<Double>();//[Xcnt+1];	       
		   xD.add(0,  0D);;
		   xS =new ArrayList<Double>();
		   xS.add(0,  0D);
	       double ttmS =0 ;  //step value for Speed  
//	       double ttmD =0 ;  //step value for distance
	       long tm=0;
	        Waypoint_bean  wb;
	        int j=1;  
	        int k=0;
	        double asp =0.0;  //step average Speed
	        long trStartTime;
	        long lwb;
	        double xvalue=0;
//	        for(Waypoint_bean wb:al){
	        for(int i=0;i<al.size();i++){
	        	if(i>0 &&al.get(i).getTime()< al.get(i-1).getTime()){
	        		QLog.getLogger().d(dTAG,TAG +" Duplicated WB, dropped.");
	        		continue;
	        	}
	        	wb = al.get(i); 
	        	trStartTime =tr.getTrackBean().getStartTime();
	        	lwb = i>0?al.get(i-1).getTime():trStartTime;
	        	xvalue = (wb.getTime() -trStartTime)/1000.0/60.0;
	           tm =	wb.getTime() -lwb;//how much time till the wp
	          ttmS+=tm/1000.0/60.0;	       
	          asp +=wb.getSpeed();
	          k++;
	          if(!xS.contains(xvalue) && (tr.getTrackBean().getTopSpeed() == wb.getSpeed()|| 0 == wb.getSpeed())){
	        	  xS.add(xvalue);
	        	  yS.add( (double) wb.getSpeed());
	          } 
	          if(ttmS >=XstepValue  ||i==al.size()-1 ){ 
	        	  j++;
//	        	  ttmD+=ttmS;
	        	  if(xS.size()>0 && wb.getOfftime() + wb.getPauseTime()>0)
		          {
		        	  xS.add( xS.get(xS.size()-1));
		        	  yS.add(0D); 
		        	  if(wb.getOfftime() + wb.getPauseTime()>ttmS ){
//			        	  xD.add(xvalue);//
		        		  xD.add( xS.get(xS.size()-1));
			        	  yD.add(wb.getSumDist()/1000.0);
//			        	  QLog.getLogger().d(dTAG,"j: "+ j +" xvalue: " +xvalue +", yvalue" +(wb.getSumDist()/1000.0));
		        	  }
//		        	  j++;
		        	  
		        	  xS.add(xvalue);
		        	  yS.add( 0D);
		        	  
		        	  if(wb.getOfftime() + wb.getPauseTime()>ttmS  && xvalue > xS.get(xS.size()-1)){
			        	  xD.add(xvalue);//	        		 
			        	  yD.add(wb.getSumDist()/1000.0);
//			        	  QLog.getLogger().d(dTAG,"j: "+ j +" xvalue: " +xvalue +", yvalue" +(wb.getSumDist()/1000.0));
		        	  }
//		        	  j++;
		          }
	        	  
//	        	  if(i%9==1 || i==j-1)
	        	  if(!xD.contains(xvalue) && (j==20  ||i==al.size()-1)){  // filter x distance data to not show all.
	        		  
//	        		  QLog.getLogger().d(dTAG,"i: "+ i +" xvalue: " +xvalue  );
	        		  
	        		  xD.add(xvalue);//	        		 
		        	  yD.add(wb.getSumDist()/1000.0);
		        	  j=0;
	        	  } 
                  if(!xS.contains(xvalue)){
		        	  xS.add(xvalue); 
	        		  yS.add( asp/(k+0.00) );
                  }
        		  asp=0.0;
        		  k=0;
        		  ttmS =0; 
	        	  
//	        	  j++;
	          }  
	          
	        }  
	        /*xD.add(xvalue+1);//	        		 
	        yD.add(0d);    	  
	      	xS.add(xvalue+1); 
	      	yS.add(0d );  */
	      	  
	       yValues.add( getDv(yD) );   
	       yValues.add(getDv(yS));
	       xValues.add(getDv(xD,true)); //distance
	       xValues.add(getDv(xS,true)); //speed
	      
		  
 	  	}  
	  }
}
	    
  


