 
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: ele, sum ele
 */
public class EleChart extends AbstractChart {
	public static final String TAG ="com.qiyou.EleChart";
	public static final String dTAG ="com.qiyou";

    
   	
  /**
   *  
   * 
   * @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;
  }
  
  @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();
	    getMinY();
	    dataset = buildDataset(titles, xValues, yValues);
	    renderer = getRender(3,"高度 ", "时间(分钟)", "高度(米)");
 }
  
//  private int Xcnt =100;
//  private double XstepValue;
//  private ArrayList<Track> trs = new ArrayList<Track>();  
  
  

  
  
  void getTitles(){ 
	  if(trs.size()==0)return;
	  titles = new String[trs.size()*3];
	   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++;
		   titles[i] =name + "高度";
		   i++;
	   }
 
  
  }
  
  /*
   * return max time of minutes
   */
  
  
@Override
public double getMaxY(){
	  double tmp =0;
	  for(Track tr:trs){
		  if(tr ==null)continue;
		 
		  tmp = tr.getTrackBean().geteUpDist()>tmp? tr.getTrackBean().geteUpDist():tmp;
		
	  } 
	  maxY =maxY<tmp?tmp:maxY; 
	  return maxY*1.1;
 }
 
 
void getMinY(){	  
	
	double tmp =0;
	  for(Track tr:trs){
		  if(tr ==null)continue;
		 
		  tmp = tr.getTrackBean().geteDownDist()<tmp? tr.getTrackBean().geteDownDist():tmp;
		
	  } 
	  minY =minY>tmp?tmp:minY; 
	  minY= minY*1.1;
}

 
	void getXYvalues (){  
		   
	      xValues = new ArrayList<double[]>();
		  
		  double maxSportTime = getMaxTime(); 
		  XstepValue = maxSportTime/Xcnt;
//		  double[]  xD ; 
		  ArrayList<Double>  xD ;
		  ArrayList<Double>   xE ; 
	   
		  yValues = new ArrayList<double[]>();    
//		  double[]  yD; 
		  ArrayList<Double>  yD; // y e down
		  ArrayList<Double>  yU; // y e up
		  ArrayList<Double>   yE;  
		  
	  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);
	       yU =new ArrayList<Double>();
	       yU.add(0,  0D);
	       yE =new ArrayList<Double>();
	       xD = new ArrayList<Double>();  
		   xD.add(0,  0D);
		   xE =new ArrayList<Double>();
	       double ttmS =0 ;  //step value for Speed  
	       long tm=0;
	        Waypoint_bean  wb;
	        int j=0;  
	        int k=0;
	        double asp =0.0;  //step average Speed
	        long trStartTime;
	        long lwb;
	        double xvalue=0;
	        double eu=0;
	        double ed=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.getElevation();
//	          asp +=wb.getEled();
	          k++;
//	          QLog.getLogger().d(dTAG,"i: "+ i +", ttm is: "+ ttmS +",XstepValue is: "+XstepValue +", j: " );
	          
	          if(wb.getEled()>0)
	        	  eu+=wb.getEled();
	          if(wb.getEled() < 0)
	 	         ed+=wb.getEled();
	          
	          if(ttmS >=XstepValue  || i==al.size()-1){ 
	        	  j++;
	        	  if(xE.size()>0 && wb.getOfftime() + wb.getPauseTime()>0)
		          {
		        	   
		        	  xE.add(xvalue);
		        	  yE.add( yE.get(yE.size()-1));
//		        	  
		        	  if(wb.getOfftime() + wb.getPauseTime()>ttmS ){
//			        	 
		        		  xD.add(xvalue);
			        	  yD.add(yD.get(yD.size()-1));
			        	  yU.add(yU.get(yU.size()-1));
			        	  
			        	  xD.add(xvalue);
			        	  yD.add(ed);
			        	  yU.add(eu);
			        	  
 		        	  }
		          }
 
	        	  if(!xD.contains(xvalue) &&(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);//	        		 
		        	  yD.add(ed);
		        	  yU.add(eu);
		        	  j=0;
	        	  } 
	        	  if(!xE.contains(xvalue)){
	                  xE.add(xvalue); 
	        		  yE.add( asp/(k+0.00) );
	        	  }
//        		  yE.add((double) wb.getEled());
        		  maxY =maxY<asp/(k+0.00)?asp/(k+0.00):maxY;
     	          minY =minY< asp/(k+0.00) ?minY:asp/(k+0.00);
        		  asp=0.0;
        		  k=0;
        		  ttmS =0; 
	        	  
//	        	  j++;
	          }
// 
//	          else if(i==al.size()-1 ){
//	        	  
//			        	  xD.add(xvalue);
//			        	  yD.add(ed);
//			        	  yU.add(eu); 
//			        	  xE.add(xvalue);
//			        	  yE.add((double) wb.getElevation());
////			        	  yE.add((double) wb.getEled());
//			        	  
////	        	
//	          }   
 
	          
	        } 
	     
	       yValues.add( getDv(yU) );//e up y   
	       yValues.add( getDv(yD) );//e down y
	       yValues.add(getDv(yE)); //e y
	       xValues.add(getDv(xD,true)); //e up  x
	       xValues.add(getDv(xD)); //e down x
	       xValues.add(getDv(xE,true)); //e x
	      
		  } 
	        

	   }
	  }
 
	
	
 