package bicat.gui.window;

import bicat.gui.BicatGui;
import bicat.run_machine.*;
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Properties;
/**
 * <p>
 * Title: BicAT Tool
 * </p>
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * 
 * @author Amela Prelic
 * @version 1.0
 * 
 */

public class RunDialog_ConstantBi implements ActionListener {

	BicatGui owner;

	JDialog dialog;

	static String DEFAULT_PARAMETERS = "set_defaults";

	static String RUN_CONSTANTBI = "run_constantbi";

	static String RUN_CONSTANTBI_DIALOG_CANCEL = "cancel";

	static String DEFAULT_ALPHA_VALUE = "0.4";

	static String DEFAULT_GAMMA_VALUE = "1.2";

	static String DEFAULT_BETA_VALUE = "0.5";

	static String DEFAULT_RFERENCE_GENE_TYPE_VALUE = "single";
        
        static String DEFAULT_GENE_NO_VALUE="1";
        static String DEFAULT_NO_OF_GENES_SELECTED_RANDOM_VALUE="100";
        static String DEFAULT_GENE_LIST_FILE_PATH="gene_list.txt";
        
        public static final String LOAD_DATA_WINDOW_BROWSE_DATA_MATRIX_FILE = "ld_browse_file_main";

	static JTextField alpha_f;

	static JTextField beta_f;

	static JTextField gamma_f;

	static JTextField gene_No_f;
        
        static JTextField no_Of_Genes_Selected_f;
        static JTextField f_field;
	int radioIndex=0;
        JRadioButton rb_single;
        JRadioButton rb_list;
        JRadioButton rb_random;
                
        
        static ButtonGroup radioGroup;
        private File file;
        final File propFile = new File(System.getProperty("user.home"),
			".bicat.lastfile");

	final String lastFilePropertyName = "last-file";

	Arguments_ConstantBi constantBia;

	// ===========================================================================
	public RunDialog_ConstantBi() {
	}

	public RunDialog_ConstantBi(BicatGui o, Arguments_ConstantBi args) {
		owner = o;
		constantBia = args;
	}

	// ===========================================================================
	public void makeWindow() {

		// user-defined parameters: seed, delta, alpha, n
		dialog = new JDialog(owner, "Run Consant Bicluster");

		JPanel parameters = new JPanel(new BorderLayout());
                

		// ....
		JPanel p0 = new JPanel(new FlowLayout());

		JPanel parameter_values = new JPanel(new GridLayout(0, 2));
//                parameter_values.setBorder(BorderFactory.createTitledBorder(BorderFactory
//				.createLineBorder(Color.black), "ConstantBi parameters"));

		JLabel alhpa_l = new JLabel("Set alpha : ");
		alhpa_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		parameter_values.add(alhpa_l);

		alpha_f = new JTextField();
		alpha_f.setText(DEFAULT_ALPHA_VALUE);
		alpha_f.addActionListener(this);
		parameter_values.add(alpha_f);

		// ....

		JLabel beta_l = new JLabel("Set Beta : ");
		beta_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		parameter_values.add(beta_l);

		beta_f = new JTextField();
		beta_f.setText(DEFAULT_BETA_VALUE);
		beta_f.addActionListener(this);
		parameter_values.add(beta_f);

		JLabel gamma_l = new JLabel("Set gamma : ");
		gamma_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		parameter_values.add(gamma_l);

		gamma_f = new JTextField();
		gamma_f.setText(DEFAULT_GAMMA_VALUE);
		gamma_f.addActionListener(this);
		parameter_values.add(gamma_f);

		// .... Refernce Gene Type
                
                JLabel ref_l = new JLabel("Select Reference Gene Type : ");
		ref_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		parameter_values.add(ref_l);
                
                JLabel ref_l2 = new JLabel("  ");
		ref_l2.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		parameter_values.add(ref_l2);
		
                
                radioGroup = new ButtonGroup(); 
                
                
               // Single radio Button/////////////////////
                 rb_single=new JRadioButton("Single   set the Ref Gene No:",true);
                //rb_single.setText(DEFAULT_BETA_VALUE);
		rb_single.addActionListener(this);
		parameter_values.add(rb_single);
                
                gene_No_f = new JTextField();
		gene_No_f.setText(DEFAULT_GENE_NO_VALUE);
		gene_No_f.addActionListener(this);
		parameter_values.add(gene_No_f);
                
                // Random radio Button ///////////////////////////////
                 rb_random=new JRadioButton("Random    set the No Of Genes :");
                //rb_single.setText(DEFAULT_BETA_VALUE);
		rb_random.addActionListener(this);
		parameter_values.add(rb_random);
                no_Of_Genes_Selected_f = new JTextField();
		no_Of_Genes_Selected_f.setText(DEFAULT_NO_OF_GENES_SELECTED_RANDOM_VALUE);
		no_Of_Genes_Selected_f.addActionListener(this);
		parameter_values.add(no_Of_Genes_Selected_f);        
                
                
                
                
                
                // List Radio Button
                
                 rb_list=new JRadioButton("List");
                //rb_single.setText(DEFAULT_BETA_VALUE);
		rb_list.addActionListener(this);
		parameter_values.add(rb_list);
                JLabel lb = new JLabel();
		lb.setText("    ");
		parameter_values.add(lb); 
                
                
                
                JPanel panel_2 = new JPanel(new FlowLayout());
		panel_2.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(Color.black), "Select Reference Genes List File.."));

		//JLabel fileOne = new JLabel("  ");
		//fileOne.setOpaque(true);
		//panel_2.add(rb_list);
               // panel_2.add(fileOne);
		// Show default file ""
		f_field = new JTextField("C:/TEMP/100_20.txt",23);
		panel_2.add(f_field);
		JButton browse = new JButton("Browse...");
		browse.setActionCommand(LOAD_DATA_WINDOW_BROWSE_DATA_MATRIX_FILE);
		browse.addActionListener(this);
		panel_2.add(browse);
                panel_2.setSize(new Dimension(90,30));
                //Dimension d=new Dimension(20,30);
                //  panel_2.setMaximumSize(d);
                        
		
                
                RadioButtonHandler handler = new RadioButtonHandler();
			rb_single.addItemListener(handler);
			rb_random.addItemListener(handler);
			rb_list.addItemListener(handler);
                // //////////////////////////////////////////////////////////////////////////
                // parameter_values.add(panel_2);
                radioGroup.add(rb_single);
                radioGroup.add(rb_random);
                radioGroup.add(rb_list);
                               

		p0.add(parameter_values);

		// ........................................................................

		JPanel p1 = new JPanel(new FlowLayout());

		JButton okButton = new JButton("Run ConstantBi");
		okButton.setActionCommand(RUN_CONSTANTBI);
		okButton.addActionListener(this);
		p1.add(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand(RUN_CONSTANTBI_DIALOG_CANCEL);
		cancelButton.addActionListener(this);
		p1.add(cancelButton);

		// ........................................................................

		parameters.add(p0,BorderLayout.PAGE_START);
                parameters.add(panel_2,BorderLayout.CENTER);
		parameters.add(p1,BorderLayout.PAGE_END);
		parameters.setOpaque(true);
                
                dialog.setContentPane(parameters);

		// ........................................................................
		dialog.pack();
		dialog.setLocationRelativeTo(owner);
		dialog.setVisible(true);
	}

	// ===========================================================================
	/**
	 * For <code>ActionListener</code> interface, reacts to user selections
	 * and button clicks in this search window.
	 * 
	 */
	public void actionPerformed(ActionEvent e) {

		if (RUN_CONSTANTBI.equals(e.getActionCommand())) {
			try {
				double alpha = (new Double(alpha_f.getText()).doubleValue());
				double beta = (new Double(beta_f.getText()).doubleValue());
				double gamma = (new Double(gamma_f.getText()).doubleValue());
				
                                
                                System.out.println(gene_No_f.getText().toString());
                                
                                int gene_No = (new Integer(gene_No_f.getText().toString()).intValue());
                                String gene_List_File_Path=f_field.getText();
                                int no_Of_Genes_Selected = (new Integer(no_Of_Genes_Selected_f.getText()).intValue());

                                constantBia.setAlpha(alpha);
                                constantBia.setBeta(beta);
                                constantBia.setGamma(gamma);
                                constantBia.setGene_Ref_No(gene_No);
                                constantBia.setNo_Of_Genes_Random_Selected(no_Of_Genes_Selected);
                                constantBia.setRef_gene_list_file_Path(gene_List_File_Path);
                                // get the Reference Type (radio Index)
                                
                                if(radioIndex==0)
                                    constantBia.setGene_refernce_Type(constantBia.GENE_REFERENCE_SINGLE);
                                else if(radioIndex==1)
                                    constantBia.setGene_refernce_Type(constantBia.GENE_REFERENCE_RANDOM);
                                else if(radioIndex==2)
                                    constantBia.setGene_refernce_Type(constantBia.GENE_REFERENCE_LIST);
                                
                                
                                
                                
				
				dialog.setVisible(false);
				dialog.dispose();
				
				JOptionPane.showMessageDialog(null,
								"ConstantBi algorithm is running... \nThe calculations may take some time");

				for (int i = 0; i < 500000000; i++) {}
				owner.runMachine_constantBi.runBiclustering(constantBia);

				

			} catch (NumberFormatException nfe) {
				nfe.printStackTrace();
			}
		}

		else if (RUN_CONSTANTBI_DIALOG_CANCEL.equals(e.getActionCommand())) {
			dialog.setVisible(false);
			dialog.dispose();
		}
               else if (LOAD_DATA_WINDOW_BROWSE_DATA_MATRIX_FILE.equals(e.getActionCommand())) {

			if (BicatGui.debug)
				System.out.println("Current working directory is: "
						+ BicatGui.currentDirectoryPath);

			JFileChooser jfc = new JFileChooser(BicatGui.currentDirectoryPath);
			jfc.setMultiSelectionEnabled(true);
			jfc.setDragEnabled(true);
			jfc.setDialogTitle("Select main data file:");
			try {
				Properties prop = new Properties();
				prop.load(new FileInputStream(propFile));
				if (prop.containsKey(lastFilePropertyName)) {
					File selFile = new File(prop.getProperty(lastFilePropertyName));
					jfc.setSelectedFile(selFile);
				}
			} catch (IOException ex) {
				// could not fetch last file from properties file
				// ignore exception
				ex.printStackTrace();
			}

			if (jfc.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
				file = jfc.getSelectedFile();
				Properties prop = new Properties();
				prop.setProperty(lastFilePropertyName, file.getAbsolutePath());
				try {
					prop.store(new FileOutputStream(propFile),lastFilePropertyName);
				} catch (IOException ex) {
					// ignore
					ex.printStackTrace();
				}
				f_field.setText(file.getPath());
			} else
				return; 
		}
                
                
	}

        
        
        
        
        
        //Creates class to handle buttons and assigns if/else tasks
public class RadioButtonHandler implements ItemListener
 
	{
 			public void itemStateChanged(ItemEvent event)
 				{
 
					if (event.getSource() == rb_single )
					radioIndex = 0;
 
					else if(event.getSource() == rb_random)
					radioIndex = 1;
 
					else if(event.getSource() == rb_list)
					radioIndex = 2;
 		
                        System.out.println("radio<<"+radioIndex);
                        }
		}
        
        
        
        
        
}