/*
 *  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.main.ui;

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

import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JDialog;


import com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.application.ChildFrame;
import com.approximatrix.draco.datamodel.DateObject;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.io.ImportExport;

import javax.swing.JTextField;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import java.util.ArrayList;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JToggleButton;
import javax.swing.JCheckBox;

import javax.swing.JFileChooser;
import javax.swing.filechooser.*;

public class NewTimeSeriesDialog extends ChildFrame {

	private static final String STD_DIALOG_NAME = "Create New Time Series";
	private static final String IMP_DIALOG_NAME = "Setup Imported Time Series";

	/** The string for requesting topical help */
	private static final String HELP_KEY = "newtimeseries";
	
	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JLabel jLabel = null;

	private JComboBox jTypeComboBox = null;

	private JPanel jPanel1 = null;

	private JPanel jPanel2 = null;

	private JLabel jStart1Label = null;

	private JLabel jStart2Label = null;

	private JLabel jStart3Label = null;

	private JTextField jStart1TextField = null;

	private JTextField jStart2TextField = null;

	private JTextField jStart3TextField = null;

	private JLabel jEnd1Label = null;

	private JLabel jEnd2Label = null;

	private JLabel jEnd3Label = null;

	private JTextField jEnd1TextField = null;

	private JTextField jEnd2TextField = null;

	private JTextField jEnd3TextField = null;

	private JPanel jPanel3 = null;

	private JCheckBox jCreateNewCheckBox = null;

	private JPanel jPanel4 = null;

	private JButton jCancelButton = null;

	private JButton jCreateButton = null;
	
	private boolean import_after_creation = false;

	/**
	 * This is the default constructor
	 */
	public NewTimeSeriesDialog() {
		//super(manager.getMainWindow());
		super(STD_DIALOG_NAME, 
				true, //resizable
				true, //closable
				true, //maximizable
				true);
		
		initialize();
	}

	public NewTimeSeriesDialog(boolean import_needed) {
		this();
		import_after_creation = import_needed;
		if(import_after_creation) this.setTitle(IMP_DIALOG_NAME);
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 239);
		this.setTitle(STD_DIALOG_NAME);
		this.setContentPane(getJContentPane());
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		//this.addWindowListener(new java.awt.event.WindowAdapter() {
		//	public void windowClosing(java.awt.event.WindowEvent e) {
		//		closeDialog();
		//	}
		//});
		
		updateFromSelection(0);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			BorderLayout borderLayout1 = new BorderLayout();
			borderLayout1.setHgap(3);
			borderLayout1.setVgap(3);
			jContentPane = new JPanel();
			jContentPane.setLayout(borderLayout1);
			jContentPane.add(getJPanel1(), java.awt.BorderLayout.CENTER);
			jContentPane.add(getJPanel4(), java.awt.BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.ipadx = 0;
			gridBagConstraints1.ipady = 0;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints.weightx = 0.0D;
			gridBagConstraints.weighty = 0.0D;
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Time Series Type:");
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(jLabel, gridBagConstraints);
			jPanel.add(getJTypeComboBox(), gridBagConstraints1);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTypeComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJTypeComboBox() {
		if (jTypeComboBox == null) {
			ArrayList type_strings = new ArrayList();
			int i = 1;
			String text = DateObject.getTypeString(i);
			while(text != null) {
				i++;
				type_strings.add(text);
				text = DateObject.getTypeString(i);
			}
			jTypeComboBox = new JComboBox(type_strings.toArray());
			type_strings = null;
			jTypeComboBox.setSelectedIndex(0);
			
			//updateFromSelection(0);
			
			jTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					updateFromSelection(getJTypeComboBox().getSelectedIndex());
				}
			});
		}
		return jTypeComboBox;
	}
	
	private void updateFromSelection(int index) {
		int type = index + 1;
		
		jStart1Label.setVisible(false);
		jStart2Label.setVisible(false);
		jStart3Label.setVisible(false);
		jEnd1Label.setVisible(false);
		jEnd2Label.setVisible(false);
		jEnd3Label.setVisible(false);
		
		getJStart1TextField().setVisible(false);
		getJStart2TextField().setVisible(false);
		getJStart3TextField().setVisible(false);
		getJEnd1TextField().setVisible(false);
		getJEnd2TextField().setVisible(false);
		getJEnd3TextField().setVisible(false);
		
		// Always have years present
		jStart1Label.setText("Start Year");
		jEnd1Label.setText("End Year");
		jStart1Label.setVisible(true);
		jEnd1Label.setVisible(true);
		getJStart1TextField().setVisible(true);
		getJEnd1TextField().setVisible(true);
		
		if(type != DateObject.QUARTERLY &&
		   type != DateObject.WEEKLY &&
		   type != DateObject.YEARLY) {
			jStart2Label.setText("Start Month");
			jEnd2Label.setText("End Month");
			jStart2Label.setVisible(true);
			jEnd2Label.setVisible(true);
			getJStart2TextField().setVisible(true);
			getJEnd2TextField().setVisible(true);
		}
		
		switch(type) {
			case DateObject.DAILY:
				jStart3Label.setText("Start Day");
				jEnd3Label.setText("End Day");
				jStart3Label.setVisible(true);
				jEnd3Label.setVisible(true);
				getJStart3TextField().setVisible(true);
				getJEnd3TextField().setVisible(true);
				break;
			case DateObject.BIMONTHLY:
				jStart3Label.setText("Start Half");
				jEnd3Label.setText("End Half");
				jStart3Label.setVisible(true);
				jEnd3Label.setVisible(true);
				getJStart3TextField().setVisible(true);
				getJEnd3TextField().setVisible(true);
				break;
			case DateObject.QUARTERLY:
				jStart2Label.setText("Start Quarter");
				jEnd2Label.setText("End Quarter");
				jStart2Label.setVisible(true);
				jEnd2Label.setVisible(true);
				getJStart2TextField().setVisible(true);
				getJEnd2TextField().setVisible(true);
				break;
			case DateObject.WEEKLY:
				jStart2Label.setText("Start Week");
				jEnd2Label.setText("End Week");
				jStart2Label.setVisible(true);
				jEnd2Label.setVisible(true);
				getJStart2TextField().setVisible(true);
				getJEnd2TextField().setVisible(true);
				break;	
		}
		
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setHgap(2);
			borderLayout.setVgap(2);
			jPanel1 = new JPanel();
			jPanel1.setLayout(borderLayout);
			jPanel1.add(getJPanel(), java.awt.BorderLayout.NORTH);
			jPanel1.add(getJPanel2(), java.awt.BorderLayout.CENTER);
			jPanel1.add(getJPanel3(), java.awt.BorderLayout.SOUTH);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.ipadx = 3;
			gridBagConstraints13.ipady = 3;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints12.gridy = 2;
			gridBagConstraints12.weightx = 1.0;
			gridBagConstraints12.ipadx = 3;
			gridBagConstraints12.ipady = 3;
			gridBagConstraints12.gridx = 3;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.gridy = 1;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.ipadx = 3;
			gridBagConstraints11.ipady = 3;
			gridBagConstraints11.gridx = 3;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints10.gridy = 0;
			gridBagConstraints10.weightx = 1.0;
			gridBagConstraints10.ipadx = 3;
			gridBagConstraints10.ipady = 3;
			gridBagConstraints10.gridx = 3;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 2;
			gridBagConstraints9.ipadx = 3;
			gridBagConstraints9.ipady = 3;
			gridBagConstraints9.gridy = 2;
			jEnd3Label = new JLabel();
			jEnd3Label.setText("JLabel");
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 2;
			gridBagConstraints8.ipadx = 3;
			gridBagConstraints8.ipady = 3;
			gridBagConstraints8.gridy = 1;
			jEnd2Label = new JLabel();
			jEnd2Label.setText("JLabel");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 2;
			gridBagConstraints7.ipadx = 3;
			gridBagConstraints7.ipady = 3;
			gridBagConstraints7.gridy = 0;
			jEnd1Label = new JLabel();
			jEnd1Label.setText("JLabel");
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridy = 2;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.ipadx = 3;
			gridBagConstraints6.ipady = 3;
			gridBagConstraints6.gridx = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.ipadx = 3;
			gridBagConstraints5.ipady = 3;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.ipadx = 3;
			gridBagConstraints4.ipady = 3;
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.ipadx = 3;
			gridBagConstraints3.ipady = 3;
			gridBagConstraints3.gridy = 2;
			jStart3Label = new JLabel();
			jStart3Label.setText("JLabel");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.ipadx = 3;
			gridBagConstraints2.ipady = 3;
			gridBagConstraints2.gridy = 1;
			jStart2Label = new JLabel();
			jStart2Label.setText("JLabel");
			jStart1Label = new JLabel();
			jStart1Label.setText("JLabel");
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(jStart1Label, gridBagConstraints13);
			jPanel2.add(jStart2Label, gridBagConstraints2);
			jPanel2.add(jStart3Label, gridBagConstraints3);
			jPanel2.add(getJStart1TextField(), gridBagConstraints4);
			jPanel2.add(getJStart2TextField(), gridBagConstraints5);
			jPanel2.add(getJStart3TextField(), gridBagConstraints6);
			jPanel2.add(jEnd1Label, gridBagConstraints7);
			jPanel2.add(jEnd2Label, gridBagConstraints8);
			jPanel2.add(jEnd3Label, gridBagConstraints9);
			jPanel2.add(getJEnd1TextField(), gridBagConstraints10);
			jPanel2.add(getJEnd2TextField(), gridBagConstraints11);
			jPanel2.add(getJEnd3TextField(), gridBagConstraints12);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jStart1TextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJStart1TextField() {
		if (jStart1TextField == null) {
			jStart1TextField = new JTextField();
			jStart1TextField.setPreferredSize(new java.awt.Dimension(60,20));
		}
		return jStart1TextField;
	}

	/**
	 * This method initializes jStart2TextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJStart2TextField() {
		if (jStart2TextField == null) {
			jStart2TextField = new JTextField();
		}
		return jStart2TextField;
	}

	/**
	 * This method initializes jStart3TextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJStart3TextField() {
		if (jStart3TextField == null) {
			jStart3TextField = new JTextField();
		}
		return jStart3TextField;
	}

	/**
	 * This method initializes jEnd1TextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJEnd1TextField() {
		if (jEnd1TextField == null) {
			jEnd1TextField = new JTextField();
			jEnd1TextField.setPreferredSize(new java.awt.Dimension(60,20));
		}
		return jEnd1TextField;
	}

	/**
	 * This method initializes jEnd2TextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJEnd2TextField() {
		if (jEnd2TextField == null) {
			jEnd2TextField = new JTextField();
		}
		return jEnd2TextField;
	}

	/**
	 * This method initializes jEnd3TextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJEnd3TextField() {
		if (jEnd3TextField == null) {
			jEnd3TextField = new JTextField();
		}
		return jEnd3TextField;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jPanel3 = new JPanel();
			jPanel3.add(getJCreateNewCheckBox(), null);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jCreateNewCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCreateNewCheckBox() {
		if (jCreateNewCheckBox == null) {
			jCreateNewCheckBox = new JCheckBox();
			jCreateNewCheckBox.setText("Create on New Worksheet");
		}
		return jCreateNewCheckBox;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
			jPanel4 = new JPanel();
			jPanel4.setLayout(flowLayout);
			jPanel4.add(getJCreateButton(), null);
			jPanel4.add(getJCancelButton(), null);
		}
		return jPanel4;
	}

	/**
	 * This method initializes jCancelButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJCancelButton() {
		if (jCancelButton == null) {
			jCancelButton = new JButton();
			jCancelButton.setText("Cancel");
			jCancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return jCancelButton;
	}

	/**
	 * This method initializes jCreateButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJCreateButton() {
		if (jCreateButton == null) {
			jCreateButton = new JButton();
			jCreateButton.setText("Create");
			jCreateButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					DateObject start = getDate(getJStart1TextField(),getJStart2TextField(),getJStart3TextField());
					DateObject end = getDate(getJEnd1TextField(),getJEnd2TextField(),getJEnd3TextField());
					
					StatTableModel newdata = ImportExport.CreateTimeSeriesModel(start,end);
					
					if(getJCreateNewCheckBox().isSelected()) {
						// Create a new worksheet
						MainWindow new_window = ApplicationManager.RequestAndReturnNewWindow(newdata);
						if(import_after_creation) new_window.forceImportWindow(true);
						new_window = null;
					} else {
						// Overwrite the current worksheet
						NewTimeSeriesDialog.this.getMainWindow().setWindowTableModel(newdata);
						if(import_after_creation) NewTimeSeriesDialog.this.getMainWindow().forceImportWindow(true);
					}
					
					dispose();
				}
			});
		}
		return jCreateButton;
	}
	
	/** Creates a new DateObject from the specified textfields
	 * 
	 * @param text1 JTextField #1
	 * @param text2 JTextField #2 
	 * @param text3 JTextField #3
	 * @return a new, valid DateObject
	 */
	private DateObject getDate(JTextField text1, JTextField text2, JTextField text3) {
		
		int[] value = new int[3];

		value[0] = getIntFromTextField(text1);
		value[1] = getIntFromTextField(text2);
		value[2] = getIntFromTextField(text3);
		
		int type = getJTypeComboBox().getSelectedIndex()+1;
		
		int year = value[0];
		int month = 0;
		int monthhalf = 0;
		int quarter = 0;
		int week = 0;
		int day = 0;
		
		switch(type) {
			case DateObject.DAILY:
				day = value[2];
			case DateObject.MONTHLY:
				month = value[1];
				break;
			case DateObject.BIMONTHLY:
				month = value[1];
				monthhalf = value[2];
				break;
			case DateObject.QUARTERLY:
				quarter = value[1];
				break;
			case DateObject.WEEKLY:
				week = value[1];
				break;
		}
		
		return new DateObject(type, year, quarter, month, monthhalf, week, day);
	}
	
	/** Returns an integer representation of the contents of a textfield,
	 * or zero if a NumberFormatException occurred
	 * @param textf the JTextFiled to process
	 * @return the int value or zero if unable to do so
	 */
	private int getIntFromTextField(JTextField textf) {
		int value;
		
		if(textf.getText() == null) return 0;
		
		try {
			value = new Integer(textf.getText()).intValue();
		} catch(NumberFormatException nfe) {
			value = 0;
		}
		return value;
	}

	/** This New Time Series Dialog does not have any menu options.
	 * 
	 * @return null because there are no menu items necessary
	 */
	public JMenu[] getMenuItems() {
		// TODO Auto-generated method stub
		return null;
	}

	/** This New Time Series Dialog does not have any toolbar buttons.
	 * 
	 * @return null because there are no buttons necessary
	 */
	public Component[] getToolbarButtons() {
		// TODO Auto-generated method stub
		return null;
	}

	/** This returns false since there is no reason to rename a "New Time Series Dialog"
	 * 
	 * @return false
	 */
	public boolean isRenameable() {
		return false;
	}
	
	/** Returns the help key for the New Time Series 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;
	}
	
}  //  @jve:decl-index=0:visual-constraint="21,12"
