/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.qiyou.mb.android.ui.charts;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import com.qiyou.mb.android.beans.basic.CItem;
import com.qiyou.mb.android.utils.Utils;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;

/**
 * Budget demo pie chart.
 */
public class TrackPieChart extends AbstractChart {
  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
//	HashMap<String,Double> values=new HashMap<String,Double>();
	 DefaultRenderer renderer ;
/*  public TrackPieChart(SparseArray<StatisticsTrack> statisticsTracks){
	  if(statisticsTracks==null)return;
	  StatisticsTrack st;
	  double value=0;
	  for(int i=0;i<=statisticsTracks.size();i++){
		   st=statisticsTracks.get(i);
		   if(st==null)continue;
		   value =st.getStaticsTrackBeans().size();
		   if(value>0){
			   values.put(TrackType.valueOf(i).toCHString(),value);
		   }
	  }
	  buildChartData();
	  
	  
  }
  
  public TrackPieChart(HashMap<String,Double> trackStatMap){
	  values.putAll(trackStatMap);
	  buildChartData();
	  
	  
  }*/
  List<CItem> Citems;
  
  public TrackPieChart(List<CItem> citems){
	 /* if(citems==null)return;
	   for(CItem citem:citems){		   
		   if(citem==null)continue;
		   
		   if(citem.getCnt()>0){
			   values.put(TrackType.valueOf(Integer.valueOf(citem.GetID())).toCHString(),(double) citem.getCnt());
		   }
	  } 
	   values.clear();
	  CItem citem;
	  for(int i=citems.size()-1;i>=0;i--){		
		   citem=citems.get(i);
		   if(citem==null)continue; 
		   if(citem.getCnt()>0){
			   values.put(TrackType.valueOf(Integer.valueOf(citem.GetID())).toCHString(),(double) citem.getCnt());
		   }
	  } 
	  if(values.size()>0)*/
	  Citems =citems;
	  buildChartData(); 
  }
  
  
  public String getName() {
    return "Track Statistics";
  }

  /**
   * Returns the chart description.
   * 
   * @return the chart description
   */
  public String getDesc() {
    return "Track Statistics";
  }

  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  @Override
public Intent execute(Context context) {
//    double[] values = new double[] { 12, 14, 11, 10, 19 };
    
   
//    Intent intent = ChartFactory.getPieChartIntent(context,
//        buildCategoryDataset("Track Statistics", values), renderer, "Budget");
//    return intent;
    Intent intent = null;
    try{
    	intent = ChartFactory.getPieChartIntent(context,
    	        buildCategoryDataset("Track Statistics", Citems), renderer, "Budget");
     
      }catch(Exception er)
      {
      	Utils.logStackTrace(er, TAG);
      }
       return intent;
       
  }

@Override
public double getMaxY() {
	// TODO Auto-generated method stub
	return 0;
}

@Override
public View getChartView(Context context)
 {
	  View v = null;
	  try{
		  setFontSize(context);
		  v= ChartFactory.getPieChartView(context,
				  							buildCategoryDataset("Track Statistics", Citems), 
				  							renderer)  ;//.getLineChartView(context, dataset, renderer); 
	   
	    }catch(Exception er)
	    {
	    	Utils.logStackTrace(er, TAG);
	    }
	  
	  return v;
	  
	 
 }

@Override
public void buildChartData() {
	// TODO Auto-generated method stub
	  if(Citems.size()==0)return;
//	  int slateGrey = Integer.parseInt("708090",16);
//	 int[] allColors = new int[] {Color.GREEN,Color.CYAN,  Color.RED, Color.GRAY, Color.DKGRAY,Color.MAGENTA, Color.YELLOW,Color.LTGRAY ,R.color.lightslategray};
	  int[] allColors = {  Color.parseColor("#56ADDB"),Color.parseColor("#FF8497"),Color.parseColor("#95AC5F"), CYRAN,Color.parseColor("#95AC5F"),
	            Color.parseColor("#F59331"), 
	            Color.parseColor("#948D8B"),  Color.parseColor("#73C83C"),
	            Color.parseColor("#FFD34E"), Color.parseColor("#E54273"),
	            Color.parseColor("#2A86D6"), Color.parseColor("#2A86D6"),Color.parseColor("#00BFC0")
	             };  
	    int[] colors = new int[Citems.size()]  ;
	    
	    for(int i=0;i<Citems.size();i++){ 
	    	 colors[i]=  allColors[i];
			 
	    }  
	
	    renderer = buildCategoryRenderer(colors);
	    renderer.setZoomButtonsVisible(false);
	    renderer.setZoomEnabled(false);
//	    renderer.setChartTitleTextSize(20);
//	    renderer.setChartTitle("运动次数明细");
	    renderer.setDisplayValues(true);
	    renderer.setShowLabels(false); 
	    renderer.setLabelsTextSize(mFontSize); 
//	    renderer.setLegendTextSize(18);
	    renderer.setLabelsColor(Color.DKGRAY); //周边标签字体颜色
	    renderer.setPanEnabled(false);
	    
	    SimpleSeriesRenderer r = renderer.getSeriesRendererAt(0);
	    r.setGradientEnabled(false);
	    r.setGradientStart(0, Color.BLUE);
	    r.setGradientStop(0, Color.GREEN);
	    r.setHighlighted(true);
	    r.setDisplayChartValues(true);
//	    r.setChartValuesTextSize(55);
}


}
