/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 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.analysis.ui;

import javax.swing.JPanel;

import java.awt.Component;
import java.awt.BorderLayout;

import com.approximatrix.draco.analysis.RunANOVA;
import com.approximatrix.draco.analysis.StandardAnalysis;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.datamodel.VariableSelectTableModel;
import com.approximatrix.draco.general.ui.InfoWindow;
import com.approximatrix.draco.io.XMLio.AnalysisLoadStore;
import com.approximatrix.draco.utility.ResultsUtilities;

import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.util.ArrayList;

public class ANOVADialog extends AnalysisChildDialog {

	/** Default dialog title */
	private static final String DIALOG_NAME = "One-Factor Analysis of Variance";
	
	/** The results dialog name */
	private static final String RESULTS_DIALOG = "ANOVA Results";
	
	/** The string for requesting topical help */
	private static final String HELP_KEY = "anova";
	
	/** The icon to be displayed for this child window */
	private static final String PATH_TO_ICON = "/com/approximatrix/draco/main/ui/images/icons/anova.png";
	
	
	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;
	private JMenuBar menubar = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JMenu perform_menu = null;
	private JMenuItem m_compute = null;
	private JMenu options_menu = null;

	private StatTableModel model = null;
	//private String results = null;
	private StandardAnalysis analysis = null;

	/**
	 * @param owner
	 */
	public ANOVADialog(StatTableModel information) {
		super(DIALOG_NAME, 
				true, //resizable
				true, //closable
				true, //maximizable
				true);
		
		initialize();
		
		model = information;
		getJTable().setModel(new VariableSelectTableModel(model.getColumnNames()));
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setTitle(DIALOG_NAME);
		this.setContentPane(getJContentPane());
		super.setIcon(PATH_TO_ICON);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
		}
		return jTable;
	}

	/**
	 * This method initializes perform_menu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getPerform_menu() {
		if (perform_menu == null) {
			perform_menu = new JMenu();
			perform_menu.setText("Analysis");
			perform_menu.add(getM_compute());
		}
		return perform_menu;
	}

	/**
	 * This method initializes m_compute	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getM_compute() {
		if (m_compute == null) {
			m_compute = new JMenuItem();
			m_compute.setText("Compute Analysis...");
			m_compute.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(analysis == null) {
						VariableSelectTableModel table_model = (VariableSelectTableModel)getJTable().getModel();
						
						if(table_model.getSelected() == null) {
							JOptionPane.showMessageDialog(jContentPane,"At least two variables must be selected",  "Data Error", JOptionPane.ERROR_MESSAGE);
							return;
						}
						
						if(table_model.getSelected().length < 2) {
							JOptionPane.showMessageDialog(jContentPane,"At least two variables must be selected",  "Data Error", JOptionPane.ERROR_MESSAGE);
							return;
						}
						
						analysis = new RunANOVA(model,table_model.getSelected());
						getJTable().setEnabled(false);
					}
					
					ANOVADialog.this.getMainWindow().openWindow(
							new InfoWindow(RESULTS_DIALOG,analysis.getResults(),false,ResultsUtilities.USE_HTML));
				}
			});
		}
		return m_compute;
	}

	/**
	 * This method initializes options_menu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getOptions_menu() {
		if (options_menu == null) {
			options_menu = new JMenu();
			options_menu.setEnabled(false);
			options_menu.setText("Options");
		}
		return options_menu;
	}
	
	/** Retuns the analysis as an StandardAnalysis object
	 * 
	 * @return a valid, complete StandardAnalysis object
	 */
	public StandardAnalysis getAnalysis() {
		return analysis;
	}
	
	/** Used for restoring an StandardAnalysis analysis to a dialog
	 * 
	 * @param values a valid, loaded AnalysisLoadStore dataset
	 */
	public void setAnalysis(AnalysisLoadStore data) {
		
		// Construct the analysis
		analysis = new RunANOVA();
		analysis.setSuccess(true);
		analysis.setResults(data.getResults());
		analysis.setVariables(data.getIndependentVariables());
		
		// Set the proper variables to selected
		VariableSelectTableModel tbl_model = (VariableSelectTableModel)this.getJTable().getModel();
		tbl_model.setSelected(data.getIndependentVariables());
		this.getJTable().setEnabled(false);
	}

	/** Returns the menu items needed for the ANOVA dialog
	 * 
	 * @return an array of JMenu objects
	 * @see com.approximatrix.draco.application.ChildFrame#getMenuItems()
	 */
	public JMenu[] getMenuItems() {
		if(menuItems==null) {
			ArrayList items = new ArrayList();
			
			items.add(getPerform_menu());
			items.add(getOptions_menu());
			
			menuItems = new JMenu[items.size()];
			items.toArray(menuItems);
		}
		return menuItems;
	}

	/** This dialog currently has no buttons
	 * 
	 * @return null since there are no toolbar buttons
	 * @see com.approximatrix.draco.application.ChildFrame#getToolbarButtons()
	 */
	public Component[] getToolbarButtons() {
		return null;
	}

	/** Returns the help key for the ANOVA Analysis window
	 * 
	 * @return the proper help key, defined by HELP_KEY
	 * @see #HELP_KEY
	 * @see com.approximatrix.draco.application.ChildFrame#getHelpKey()
	 */
	public String getHelpKey() {
		return HELP_KEY;
	}
}
