package action;

import glycoMain.InputTreatment;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.ListModel;


@SuppressWarnings("serial")
public class SubmitAction extends AbstractAction {
/*
 * Button performing the digestion
 */
	JPanel jp;
	JTextArea input;
	JTextArea output;
	JList<String> selectedEnzyme;
	ArrayList<String> enzArray = new ArrayList<String>();
	JCheckBox unknownLink;
	JRadioButton iupac;
	String digestedGlycan;

	
	public SubmitAction (JPanel jp, JTextArea input, JTextArea output, JList<String> selectedEnzyme, JCheckBox unknownLink, JRadioButton iupac){
		this.jp = jp;
		this.input = input;
		this.output = output;
		this.selectedEnzyme = selectedEnzyme;
		this.unknownLink = unknownLink;
		this.iupac = iupac;
	}
	
	
	public void enzymeChoice(){
//		change list of selected enzymes to arrayList to the digestion
		enzArray.clear();
		ListModel<String> model = selectedEnzyme.getModel();
		if (model.getSize() != 0){
			String e1 = (String) model.getElementAt(model.getSize()-1);
			String e2[] = e1.split("\\+");
			for (int i =0; i<e2.length; i++)
				enzArray.add(e2[i]);
		}
	}
	

	public void actionPerformed(ActionEvent e) {
	
		String in = "";
		enzymeChoice();	
		System.out.println("testunk : " + unknownLink.isSelected());
		if (!input.getText().isEmpty()){
			if (!enzArray.isEmpty()){
				in = input.getText();
				
				// TODO try catch
				InputTreatment treatment = new InputTreatment(in, enzArray, unknownLink.isSelected(), iupac.isSelected());
				digestedGlycan = treatment.glycanTreatment();
//				if (treatment.isExceptFormat())
//					JOptionPane.showMessageDialog(jp, "input format incorrect\nmore information in the GlycoCT_manual pdf", "Error", JOptionPane.ERROR_MESSAGE);
//				if (treatment.isPbDigestion())
//					JOptionPane.showMessageDialog(jp, "Problem during the digestion, sorry", "Error", JOptionPane.ERROR_MESSAGE);
				this.output.setText(output.getText() + digestedGlycan);
			}
			else 
				JOptionPane.showMessageDialog(jp, "no enzyme selected\nPlease choose one enzyme at least", "Error", JOptionPane.ERROR_MESSAGE);
		}
		else 
			JOptionPane.showMessageDialog(jp, "No glycan in input \nPlease load a glycan sequence ", "Error", JOptionPane.ERROR_MESSAGE);
		}

}
