 
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.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;

 
/**
 *X: distance
 *Y:time, speed
 */
 
public class DistSpeedChartOld extends AbstractChart {
	public static final String TAG ="com.qiyou.DistSpeedChart";
	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, "速度-时间 -距离", "距离（公里）", yTitle);
 }
  
 
  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 + "速度";
		   i++;
	   }
 
  
  }
  
  String yTitle ="时速--时间(分)";
  double yTscale=1;
  double ySscale=1;
 @Override
public double getMaxY(){
	  double maxYs =1;
	  double maxYt =1;
	  for(Track tr:trs){
		  if(tr ==null)continue;
//		  maxSportTime = getMaxTime();
		 
//		  maxY = tr.getTrackBean().getDistance()/1000.0>maxY? tr.getTrackBean().getDistance()/1000.0:maxY;
		  maxYs = tr.getTrackBean().getTopSpeed()>maxYs? tr.getTrackBean().getTopSpeed():maxYs;
		  maxYt = getMaxTime()>maxYt? getMaxTime():maxYt;	  		  
	  } 
	  if(maxYs>maxYt*2){
//		  ySscale=  Math.round(maxYs/maxYt);
		  ySscale=   maxYs/maxYt ;
		  maxY = maxYs/ySscale ;
		  yTitle ="时速（x"+  Utils.round(ySscale,1) + "公里。小时）--时间（分）";
	  }
	  else if(maxYt>maxYs*2){
//    	  yTscale = Math.round(maxYt/maxYs);
		  yTscale =    maxYt/maxYs ;
		  maxY = maxYt/yTscale;
		  yTitle ="时速--时间（x"+ Utils.round(yTscale,1 )+"分）";
	  }else		  
		  maxY = maxYs>maxYt?maxYs:maxYt;
      QLog.getLogger().d(dTAG,TAG+" maxYs "+ maxYs +" maxYt: " +maxYt +" maxY: " +maxY  ); 
 	  QLog.getLogger().d(dTAG,TAG+" YsScale "+ ySscale +" yTscale: " +yTscale  ); 
	  return maxY;
 }
 

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

	void getXYvalues (){  
		getMaxY();
	      xValues = new ArrayList<double[]>();		  
	
		  XstepValue =  getMaxDist()/Xcnt;
//		  double[]  xD ; 
		  ArrayList<Double>  xD ;
		  ArrayList<Double>   xS ; 
	   
		  yValues = new ArrayList<double[]>();    
//		  double[]  yD; 
		  ArrayList<Double>  yT;
		  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);
	         }
	          
	         yT =new ArrayList<Double>();
	         yT.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
	        
	        Waypoint_bean  wb;
	        int j=1;  
	        int k=0;
	        double asp =0.0;  //step average Speed
	        long trStartTime;
	        double lwb;
	        double xvalue=0;
	        double yt;
//	        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).getDistance():0;
	        	lwb = al.get(i).getDistance();
	        	xvalue = wb.getSumDist()/1000.0;
	            ttmS+=wb.getDistance()/1000;	 
	            yt=(wb.getTime()-trStartTime)/1000.0/60.0/yTscale;
	          asp +=wb.getSpeed()/ySscale;
//	          QLog.getLogger().d(dTAG,TAG+" yt "+ yt + ", wb.getSpeed()/ySscale "+wb.getSpeed()/ySscale+ "; YsScale "+ ySscale +" yTscale: " +yTscale  ); 
	          k++;
	          if(tr.getTrackBean().getTopSpeed() == wb.getSpeed()|| 0 == wb.getSpeed()){
	        	  xS.add(xvalue);
	        	  yS.add( wb.getSpeed()/ySscale);
	          } 
	          else if(ttmS >=XstepValue ||i==al.size()-1  ){ 
	        	  j++;
 
	        	  if(i==al.size()-1){
	        		  
	        		  QLog.getLogger().d(dTAG,TAG+"al.size() "+al.size() +",current wb "+ i);
	        		  QLog.getLogger().d(dTAG,TAG+" yt "+ yt + ", wb.getSpeed()/ySscale "+wb.getSpeed()/ySscale+ "; YsScale "+ ySscale +" yTscale: " +yTscale  );
	        		  
	        		  
	        	  }
	 	         
//	        	  if(i%9==1 || i==j-1)
	        	  if(j==10 || i==al.size()-1  ){  // filter x distance data to not show all.
	        		  
//	        		  QLog.getLogger().d(dTAG,"i: "+ i +" xvalue: " +xvalue  );
	        		  xD.add(xvalue);//	        		 
		        	  yT.add(yt);
		        	  j=0;
	        	  } 
                  xS.add(xvalue); 
        		  yS.add( asp/(k+0.00) );
        		  
        		  asp=0.0;
        		  k=0;
        		  ttmS =0; 
	        	  
//	        	  j++;
	          } 

	          
	        }  
	          
	       yValues.add( getDv(yT) );   
	       yValues.add(getDv(yS));
	       xValues.add(getDv(xD,true)); //distance
	       xValues.add(getDv(xS,true)); //speed
	      
		  
 	  	}  
	  }
}
	    
  


