package com.approximatrix.draco.io.builders;

import java.awt.Color;
import java.beans.PropertyVetoException;
import java.util.List;

import com.approximatrix.charting.CoordSystem;
import com.approximatrix.charting.model.ChartDataModel;
import com.approximatrix.charting.model.MultiScatterDataModel;
import com.approximatrix.charting.model.ObjectChartDataModel;
import com.approximatrix.charting.render.AbstractChartRenderer;
import com.approximatrix.charting.render.BarChartRenderer;
import com.approximatrix.charting.render.MultiScatterChartRenderer;
import com.approximatrix.charting.render.RowColorModel;
import com.approximatrix.draco.application.ChildFrame;
import com.approximatrix.draco.io.builders.plot.AxisStore;
import com.approximatrix.draco.io.builders.plot.SeriesStore;
import com.approximatrix.draco.io.sql.DracoSQLTables;
import com.approximatrix.draco.main.ui.MainWindow;
import com.approximatrix.draco.plot.ui.DataPlot;
import com.approximatrix.draco.plot.ui.GenericPlotInterface;
import com.approximatrix.draco.plot.ui.HistogramPlot;
import com.approximatrix.draco.plot.ui.PlotDialog;

public class PlotBuilder {
	
	public static void BuildPlot(String name, String title, String type, List<AxisStore> axes, List<SeriesStore> series, MainWindow parent) {

		GenericPlotInterface gpi = null;
		
		if(type.equals(com.approximatrix.charting.render.BarChartRenderer.class.getName())) {
			for(SeriesStore ss : series) {
				gpi = new HistogramPlot(ss.xdata, ss.getYDataPrimitiveInteger(), title);
				break;
			}
			
		} else if(type.equals(com.approximatrix.charting.render.MultiScatterChartRenderer.class.getName())) {
			MultiScatterDataModel md = new MultiScatterDataModel();
			
			for(SeriesStore ss : series) {
				md.addData(ss.getXDataPrimitiveDouble(), ss.getYDataPrimitiveDouble(), ss.name);
				md.setSeriesLine(ss.name, ss.line);
				md.setSeriesMarker(ss.name, ss.marker);
			}
			gpi = new DataPlot(md,title);
		}
	
	    RowColorModel rcm = new RowColorModel(gpi.getPlotPanel().getChartDataModel());
	    for(int i=0;i<gpi.getPlotPanel().getChartDataModel().getDataSetNumber();i++) {
	        try {
    	        SeriesStore ss = series.get(i);
    	        rcm.setColor(i,new Color(ss.color));
    	    } catch(ArrayIndexOutOfBoundsException ae) {
    	        // Color setting failed, continue
    	    }
    	}
    	
        for(AbstractChartRenderer renderer : gpi.getPlotPanel().getChartRenderer().values())
            renderer.setRowColorModel(rcm);
	
		int ycounter = 0;
		for(AxisStore axis : axes) {
			gpi.getPlotPanel().getCoordSystem().setPaintGrid(axis.grid);
			gpi.getPlotPanel().getChartDataModel().setAutoScale(axis.scaleam == DracoSQLTables.TYPE_AXIS_SCALE_AUTO);
			gpi.getPlotPanel().getChartDataModel().setManualScale(axis.scaleam == DracoSQLTables.TYPE_AXIS_SCALE_MANUAL);
			if(axis.axis == DracoSQLTables.TYPE_AXIS_X)
				gpi.getPlotPanel().getCoordSystem().setXAxisUnit(axis.label);
			else if(axis.axis == DracoSQLTables.TYPE_AXIS_Y && ycounter == 0) {
				gpi.getPlotPanel().getCoordSystem().setYAxisUnit(axis.label);
				ycounter++;
			} 
		}
		
		try {
			ChildFrame cf = parent.openWindow(new PlotDialog(gpi));
			cf.setName(name);
			cf.setIcon(true);
		} catch(PropertyVetoException pve) {
			//nothing
		}
	}
	
}
