/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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.application.ChildFrameRegistrar;
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.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, ChildFrameRegistrar 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
		}
	}
	
}
