package action;


import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class CheckDisplayInAction extends AbstractAction{
/*
 * When CheckBox "Display Glycan Structures" for input sequences is checked, combobox to choose glycans to display appears
 */
//	test
	JTextArea area;
	JComboBox<String> combo;
	JButton ok;
	JRadioButton iupac;
	JCheckBox check;
	ArrayList<String> glycanInBox = new ArrayList<String>(); //array of glycan of input area
	
	public CheckDisplayInAction(JTextArea area, JComboBox<String> combo, JButton ok, JRadioButton iupac,JCheckBox check){
		this.area = area;
		this.combo = combo;
		this.ok = ok;
		this.iupac = iupac;
		this.check = check;
	}

	public void fillCombobox(){
//		fill the combobox, depending on the number of glycan in input and fill the glycanInBox array
		
		String[] sugar = area.getText().trim().split("\n\n");
		
		if (sugar.length == 1){
			if (iupac.isSelected())
				glycanInBox.add("RES" + sugar[0].split("RES",2)[1]);
			else
				glycanInBox.add(sugar[0]);
			combo.addItem("Glycan 1");
		}
		else
		{
			combo.addItem("All Glycans");
			for (int i=0; i<sugar.length; i++){
				if (iupac.isSelected())
					glycanInBox.add("RES" + sugar[i].split("RES",2)[1]);
				else
					glycanInBox.add(sugar[i]);
				combo.addItem("Glycan " + (i+1));			
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		glycanInBox.clear();
		fillCombobox();
		
		if (check.isSelected()){
			combo.setVisible(true);
			ok.setVisible(true);	
		  }
		else{
			combo.removeAllItems();
			combo.setVisible(false);
			ok.setVisible(false);	
		}
	}

	  public ArrayList<String> getGlycanInBoxInput() {
		  return glycanInBox;
	  }  
}
