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

import java.util.ArrayList;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart;
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;

/**
 * ele distance bar chart
 * X: time
 * Y: eled
 */
public class EledBarChart extends AbstractChart {
	public static final String TAG ="com.qiyou.EledBarChart";
	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{
		  buildChartData(); 
		 
//		  v= ChartFactory.getLineChartView(context, dataset, renderer);
//		  v= ChartFactory.getBarChartView(context, dataset, renderer,  BarChart.Type.DEFAULT );// (context, dataset, renderer);	   
		  
		  String[] types  = new String[trs.size()*2];
		  
		  for(int i=0;i < trs.size()*2;i++) {
			  types[i] =BarChart.TYPE;
		  } 
		  v=  ChartFactory.getCombinedXYChartView(context,  dataset,  renderer,types);
	     
	    }catch(Exception er)
	    {
	    	Utils.logStackTrace(er, TAG);
	    }
	  
	  return v;
  }
 @Override
 public void buildChartData(){
	    getTitles();
	    getXYvalues();
	    
	    QLog.getLogger().d(TAG,"minY, getMaxY()*1.1: " +minY +"," + getMaxY()*1.1);
	    
	    getMinY();
	    
	    
 	    dataset = buildDataset(titles, xValues, yValues);
//	    dataset = buildBarDataset(titles,yValues);
	    renderer = getRender(2,"高度 ", "时间(分钟)", "高度(米)",true);
 }
  
  
//  private ArrayList<Track> trs = new ArrayList<Track>();  
  
  

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

 
	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>   yEu;  
		  ArrayList<Double>   yEd;  
		  
	  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);
	       yEu =new ArrayList<Double>();
	       yEd =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 aeu =0.0;  //step average ed up
	        double aed =0.0;  //step average ed down
	        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;	       
		        
	//	          maxY =maxY<Math.abs(wb.getEled())?Math.abs(wb.getEled()):maxY;
	//	          minY =minY< wb.getEled() ?minY:wb.getEled();
		          k++;
	//	          QLog.getLogger().d(dTAG,"i: "+ i +", ttm is: "+ ttmS +",XstepValue is: "+XstepValue +", j: " );
		          
		          if(wb.getEled()>0)
		        	  aeu+=wb.getEled() ;
		          if(wb.getEled() < 0)
		 	         aed+=wb.getEled() ; 
		          
		          if(ttmS >=XstepValue  ||i==al.size()-1){ 
		        	  j++;
//		        	  if(xE.size()>0 && wb.getOfftime() + wb.getPauseTime()>0)
//			          {
//			        	   
////			        	  xE.add(xvalue);
////			        	  yEd.add( yEd.get(yEd.size()-1));
////			        	  yEu.add( yEu.get(yEu.size()-1));
//			        	  
//	 
//			          } 
	                  xE.add(xvalue); 
	        		  yEu.add( aeu/(k+0.00) );
	        		  yEd.add( aed/(k+0.00) );
	//        		  yE.add((double) wb.getEled());
	
	                  maxY =maxY<aeu/(k+0.00)?aeu/(k+0.00):maxY;
	     	          minY =minY< aed/(k+0.00) ?minY:aed/(k+0.00);
	        		  
	        		  
	        		  aed=0.0;
	        		  aeu=0.0;
	        		  k=0;
	        		  ttmS =0; 
		        	  
	//	        	  j++;
		          }
	 
		          
	        } 

	       
	       yValues.add(getDv(yEu)); //ed up y
	       yValues.add(getDv(yEd)); //ed down y	       
	   
	       xValues.add(getDv(xE,true)); //ed up x
	       xValues.add(getDv(xE)); //ed  donwn  x
	      
		  } 
	        

	   }
	  }
 
	
	
 