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

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.util.ArrayList;

/*
 * Created on Apr 15, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

import com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.datamodel.ParseDate;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.datamodel.DateObject;
import com.approximatrix.draco.io.InterpolationSupport;
import com.approximatrix.draco.io.PastedData;
import com.approximatrix.draco.main.ui.MainWindow;

/**
 * @author jeff
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PasteWithInterpDialog extends JDialog {

	private javax.swing.JPanel jContentPane = null;

	private JPanel jPanel = null;
	private JButton paste_button = null;
	private JButton cancel_button = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JComboBox existing_combo = null;
	
	StatTableModel current_model = null;
	
	//double[][] new_data = null;
	private PastedData new_data = null;
	
	private JComboBox pasted_combo = null;
	
	private boolean dialog_is_ready = true;
	
	private boolean success = true;
	
	/**
	 * This is the default constructor
	 */
	public PasteWithInterpDialog(JFrame parent, String paste_text, StatTableModel incoming) {
		super(parent,true);
		initialize();
		
		this.setLocationRelativeTo(parent);
		
		// Set up the current column combobox
		current_model = incoming;
		for(int i=0;i<current_model.getColumnCount();i++) {
			existing_combo.addItem(current_model.getColumnName(i));
		}		
		
		// Parse through new data
		new_data = new PastedData(paste_text);
		
		// Fill in the new data combobox
		String[] names = new_data.getHeaders();
		for(int i=0; i<names.length;i++) {
			pasted_combo.addItem(names[i]);
		}
		
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Paste Data with Interpolation");
		this.setSize(424, 132);
		this.setContentPane(getJContentPane());
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new java.awt.BorderLayout());
			jContentPane.add(getJPanel(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getJPanel1(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanel() {
		if (jPanel == null) {
			FlowLayout flowLayout1 = new FlowLayout();
			jPanel = new JPanel();
			jPanel.setLayout(flowLayout1);
			flowLayout1.setAlignment(java.awt.FlowLayout.RIGHT);
			jPanel.add(getPaste_button(), null);
			jPanel.add(getCancel_button(), null);
		}
		return jPanel;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getPaste_button() {
		if (paste_button == null) {
			paste_button = new JButton();
			paste_button.setText("Paste");
			paste_button.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					paste();
				}
			});
		}
		return paste_button;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getCancel_button() {
		if (cancel_button == null) {
			cancel_button = new JButton();
			cancel_button.setText("Cancel");
			cancel_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {    
					close_window();
				}
			});
		}
		return cancel_button;
	}
	
	private void close_window() {
		//this.hide();
		this.setVisible(false);
		if(!success)
			JOptionPane.showMessageDialog(this.getParent(),"Error during data parsing",ApplicationManager.APP_NAME+" Error",JOptionPane.ERROR_MESSAGE);
		
		dispose();
	}
	
	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel1 = new JLabel();
			jLabel = new JLabel();
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 0;
			jLabel.setText("Pasted Data Interpolation Column:");
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 1;
			jLabel1.setText("Current Model Values for Interpolation: ");
			gridBagConstraints5.gridx = 1;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.gridy = 0;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.fill = java.awt.GridBagConstraints.HORIZONTAL;
			jPanel1.add(jLabel, gridBagConstraints2);
			jPanel1.add(jLabel1, gridBagConstraints4);
			jPanel1.add(getExisting_combo(), gridBagConstraints5);
			jPanel1.add(getPasted_combo(), gridBagConstraints6);
		}
		return jPanel1;
	}
	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */    
	private JComboBox getExisting_combo() {
		if (existing_combo == null) {
			existing_combo = new JComboBox();
		}
		return existing_combo;
	}
	/**
	 * This method initializes jComboBox1	
	 * 	
	 * @return javax.swing.JComboBox	
	 */    
	private JComboBox getPasted_combo() {
		if (pasted_combo == null) {
			pasted_combo = new JComboBox();
		}
		return pasted_combo;
	}
	
	public boolean isReady() {
		return dialog_is_ready;
	}

	private void paste() {
		// Get the independent points
		int interp_xdata = pasted_combo.getSelectedIndex();
		
		// From the existing model
		String column_for_interp = (String)existing_combo.getSelectedItem();
		int current_selected = current_model.getColumnNumber(column_for_interp);
		int[] allowed_rows = current_model.getNonNullRows(column_for_interp);
		double[] x_desired = current_model.getDataDouble(allowed_rows, current_selected);
		int date_column = current_model.getDateColumn();
		int date_type = current_model.getDateType();
		DateObject base = null;
		if(date_type >=0)
			base = current_model.getDateBase();
		
		// New data
		ArrayList text_data = new_data.getData();
		if(text_data.size() > 0) {
			double[][] passing = new double[text_data.size()][new_data.getHeaders().length];
			for(int i=0;i<text_data.size();i++) {
				String[] text_line = (String[])text_data.get(i);
				Number[] line = new Number[text_line.length];
				for(int j=0;j<text_line.length;j++) {
					if(j==interp_xdata && base != null) {
						line[j] = ParseDate.ParseTextToDate(text_line[j],date_type);
					} else if(j==interp_xdata) {
						try {
							line[j] = new Double(text_line[j]);
						} catch(NumberFormatException nfe) {
							success = false;
							line[j] = new Double(0.0);
						}
					} else {
						try {
							line[j] = new Double(text_line[j]);
						} catch(NumberFormatException nfe) {
							success = false;
							line[j] = new Double(0.0);
						}
					}
				}
				
				// Now convert to simple doubles from objects
				for(int j=0;j<line.length;j++) {
					passing[i][j] = line[j].doubleValue();
				}
				
			}
			
			// Passing array is built, now interpolate
			if(success) {
				double[][] desired = InterpolationSupport.Interpolate(x_desired,passing,interp_xdata,InterpolationSupport.LINEAR);
				success = (desired != null);
				
				for(int i=0;i<new_data.getHeaders().length && success;i++) {
					if(i == interp_xdata) continue;
					
					Object[] column_data = new Object[current_model.getRowCount()];
					for(int j=0;j<column_data.length;j++) 
						column_data[j] = null;
					for(int j=0;j<allowed_rows.length;j++) 
						column_data[allowed_rows[j]] = new Double(desired[j][i]);
					
					current_model.addColumn(column_data,new_data.getHeaders()[i]);
				}
				
			}
		}
		
		close_window();
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,22"
