package cn.jingzhuan.lib.chart.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;

import java.util.List;

import cn.jingzhuan.lib.chart.BaseChart;
import cn.jingzhuan.lib.chart.data.BarDataSet;
import cn.jingzhuan.lib.chart.data.CombineData;
import cn.jingzhuan.lib.chart.data.LineDataSet;
import cn.jingzhuan.lib.chart.renderer.CombineChartRenderer;

/**
 * Created by Donglua on 17/8/2.
 */

public class CombineChart extends BaseChart {

    public CombineChart(Context context) {
        super(context);
    }

    public CombineChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CombineChart(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CombineChart(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void initChart() {
        super.initChart();

        mRenderer = new CombineChartRenderer(this);
    }

    public void addDataSet(BarDataSet barDataSet) {
        barDataSet.setChart(this);
        getRenderer().addDataSet(barDataSet);
    }

    public void addDataSet(LineDataSet lineDataSet) {
        lineDataSet.setChart(this);
        getRenderer().addDataSet(lineDataSet);
    }

    public void setDataSet(BarDataSet barDataSet) {
        getBarDataSet().clear();
        addDataSet(barDataSet);
    }

    public void setDataSet(LineDataSet lineDataSet) {
        getLineDataSet().clear();
        addDataSet(lineDataSet);
    }

    public void setLineData(List<LineDataSet> data) {
        getLineDataSet().clear();
        for (LineDataSet datum : data) {
            addDataSet(datum);
        }
    }

    public void setCombineData(CombineData combineData) {
        List<LineDataSet> lineDataSets = combineData.getLineData();
        if (lineDataSets != null) {
            for (LineDataSet lineDataSet : lineDataSets) {
                addDataSet(lineDataSet);
            }
        }

        List<BarDataSet> barDataSets = combineData.getBarData();
        if (barDataSets != null) {
            for (BarDataSet barDataSet : barDataSets) {
                addDataSet(barDataSet);
            }
        }
    }

    public List<LineDataSet> getLineDataSet() {
        return getRenderer().getDataSet();
    }

    public List<BarDataSet> getBarDataSet() {
        return getRenderer().getBarDataSet();
    }

    public CombineChartRenderer getRenderer() {
        return (CombineChartRenderer) mRenderer;
    }

}

@@ -1604,22 +1604,24 @@
         
-getBar
+cleanAll
 DataSet(
@@ -1617,34 +1617,27 @@
 AllDataSet()
-.clear()
 ;%0A
+%0A
         addD
@@ -1717,39 +1717,40 @@
 aSet) %7B%0A        
-getLine
+cleanAll
 DataSet().clear(
@@ -1738,34 +1738,27 @@
 AllDataSet()
-.clear()
 ;%0A
+%0A
         addD
@@ -1847,23 +1847,24 @@
         
-getLine
+cleanAll
 DataSet(
@@ -1868,18 +1868,11 @@
 et()
-.clear()
 ;%0A
+%0A
     
@@ -1948,32 +1948,32 @@
        %7D%0A    %7D%0A%0A
-
     public void 
@@ -2010,24 +2010,52 @@
 bineData) %7B%0A
+        cleanAllDataSet();%0A%0A
         List
@@ -2799,10 +2799,261 @@
 %0A    %7D%0A%0A
+    public void cleanLineDataSet() %7B%0A        getLineDataSet().clear();%0A    %7D%0A%0A    public void cleanBarDataSet() %7B%0A        getBarDataSet().clear();%0A    %7D%0A%0A    public void cleanAllDataSet() %7B%0A        cleanLineDataSet();%0A        cleanBarDataSet();%0A    %7D%0A
 %7D%0A

clean dataSet before dataSet added