 
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 CopyOfDistEleChart extends AbstractChart {
	public static final String TAG ="com.qiyou.DistEleChart";
	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;
  }
  
  @Override
 public View getChartView(Context context)
  {
	  View v = null;
	  try{
		  setFontSize(context);
		  buildChartData(); 
		  v= ChartFactory.getLineChartView(context, dataset, renderer);
	   
	    }catch(Exception er)
	    {
	    	Utils.logStackTrace(er, TAG);
	    }
	  
	  return v;
  }
 @Override
 public void buildChartData(){
	    getTitles();
	    getMinY();
	    getXYvalues();
	   
	    dataset = buildDataset(titles, xValues, yValues);
	    renderer = getRender(3," ", "距离(公里)", "高度(米)");
 }
  
 

  
  
  void getTitles(){ 
	  if(trs.size()==0)return;
	  titles = new String[trs.size()*3];
	   int i=0;
	   String name="";
	   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;
	  maxY=  maxY*1.1;  
	  
	  if(maxY<500 ){
	    	maxY=maxY*2; 
	     
	    	  
	     }
	  return maxY ;
	  
 }
 
 
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;
	  
	  if( Math.abs(minY)<500){
	     
	    	minY=minY*2; //扩大图形范围，使不精确的小的海拔变化波动在图上不明显
	    	  
	     }
}

 
	void getXYvalues (){  
		   
	      xValues = new ArrayList<double[]>(); 
		  XstepValue =  getMaxDist()/Xcnt*2;
 		  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 &&!(b.getEled()==0 &&b.getElevation()==0))
	         		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;  
	        double k=0;
	        double asp =0.0;  //step average Speed
	        long trStartTime;
	        double 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);
	        	j++;
//	        	if(j==1){
//	        		minX=wb.getSumDist()/1000;
//	        	}
	        	
	        	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;	
	            
		          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 ){
		        	  if(i==al.size()-1)
		        	  {
		        		  QLog.getLogger().d(dTAG," it is here ");
		        	  }
	
	                  xE.add(xvalue); 
	        		  yE.add( asp/(k+0.00) );
	//        		  yE.add((double) wb.getEled());
	        		  maxY =maxY< asp/(k) ?asp/(k):maxY;
	     	          minY =minY< asp/(k) ?minY:asp/(k);
	        		  asp=0.0;
	        		  k=0;
	        		  ttmS =0; 
	        		  
		        	  xD.add(xvalue);
		        	  yD.add(ed);
		        	  yU.add(eu); 
//				        	  xE.add(xvalue);
//				        	  yE.add((double) wb.getElevation());
	//	        	  j++;
		          } 
	        } 
	     
	        
//	     if(maxY-minY<1000){
//	    	 maxY=maxY*2; //扩大图形范围，使不精确的小的海拔变化波动在图上不明显
//	    	 minY=minY*2;
//	     } 
	       yValues.add(getDv(yE)); //e y
	       yValues.add( getDv(yU) );//e up y   
	       yValues.add( getDv(yD) );//e down y
	       xValues.add(getDv(xE,true)); //e x
	       xValues.add(getDv(xD,true)); //e up  x
	       xValues.add(getDv(xD)); //e down x
	  
	      
		  } 
	        

	   }
	  }
 
	
	
 