package com.approximatrix.draco.io.builders;

import java.beans.PropertyVetoException;

import com.approximatrix.draco.analysis.RunANOVA;
import com.approximatrix.draco.analysis.ui.ANOVADialog;
import com.approximatrix.draco.analysis.ui.AnalysisChildDialog;
import com.approximatrix.draco.application.ChildFrameRegistrar;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.fit.RunLS;
import com.approximatrix.draco.fit.RunLSAutocorrelation;
import com.approximatrix.draco.fit.RunNLLS;
import com.approximatrix.draco.fit.RunTSLS;
import com.approximatrix.draco.fit.SimpleMLEFit;
import com.approximatrix.draco.fit.ui.FitChildDialog;
import com.approximatrix.draco.fit.ui.LeastSquaresFit;
import com.approximatrix.draco.fit.ui.NonLinearLSFit;
import com.approximatrix.draco.fit.ui.SimpleIterativeFit;
import com.approximatrix.draco.fit.ui.TSLeastSquaresFit;
import com.approximatrix.draco.io.XMLio.AnalysisLoadStore;

public class AnalysisBuilder {

	public static void BuildAnalysis(AnalysisLoadStore analysis, ChildFrameRegistrar parent, StatTableModel model) {
		
		//StatTableModel model = parent.getWindowTableModel();
		
		// Get the type
		Class type = analysis.getAnalysisClass();
		FitChildDialog fit_temp = null;
		AnalysisChildDialog simple_temp = null; 
		
		// Need to select the type and open the appropriate window
		if(type == RunLS.class || type ==  RunLSAutocorrelation.class ) { // Least Squares
			fit_temp = (FitChildDialog)parent.openWindow(new LeastSquaresFit(model));
			
		} else if(type == RunTSLS.class) { // Two Stage Least Squares
			fit_temp = (FitChildDialog)parent.openWindow(new TSLeastSquaresFit(model));
			
		} else if(type == RunNLLS.class) { // Non-Linear Least Squares
			fit_temp = (FitChildDialog)parent.openWindow(new NonLinearLSFit(model));
			
		} else if(type == SimpleMLEFit.class) { // MLE (Logit/Probit)
			fit_temp = (FitChildDialog)parent.openWindow(new SimpleIterativeFit(model,0));
		
		} else if(type == RunANOVA.class) { // ANOVA
			simple_temp = (AnalysisChildDialog)parent.openWindow(new ANOVADialog(model));
		}
		
		if(fit_temp != null) {
			fit_temp.setAnalysis(analysis);
			try {
				fit_temp.setIcon(true);
			} catch(PropertyVetoException pve) {
				System.err.println("Can't iconify: "+fit_temp.getTitle());
				pve.printStackTrace();
			}
		} else if(simple_temp != null) {
			simple_temp.setAnalysis(analysis);
			try {
				simple_temp.setIcon(true);
			} catch(PropertyVetoException pve) {
				System.err.println("Can't iconify: "+simple_temp.getTitle());
				pve.printStackTrace();
			}
		}	
	}
	
}
