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.JTextArea;

@SuppressWarnings("serial")
public class CheckDisplayOutAction extends AbstractAction {

	JTextArea area;
	JComboBox<String> combo;
	JButton ok;
	JCheckBox check;
	ArrayList<String> glycanInBox = new ArrayList<String>(); //array of glycan of output area
	
	public CheckDisplayOutAction(JTextArea area, JComboBox<String> combo, JButton ok, JCheckBox check){
		this.area = area;
		this.combo = combo;
		this.ok = ok;
		this.check = check;
	}

	public void fillCombobox(){
//		fill the combobox, depending on the number of glycan in input and fill the glycanInBox array
		
		//sugar : glycan all step
		String[] sugar = area.getText().split("GLYCAN");
		for (int i=1; i<sugar.length; i++){
			combo.addItem("Glycan " + String.valueOf(i));
			//subsugar : diff step
			String[] subsugar = sugar[i].trim().split("\n\n");
			
			int le = subsugar.length;
			for (int j=1; j<le; j++){
				System.out.println("SUBSUG : " + subsugar[j]);
				String title = subsugar[j].split("\n")[0];
				String glyTemp = subsugar[j].split("RES",2)[1];
				glycanInBox.add("RES" + glyTemp);
				if(title.contains(":"))
					combo.addItem("    "+i+"." + title.split(": ")[1]);
				else
					combo.addItem("    " +i+ "." +title);
			}
		}	
	
		
		
	}
	
	@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> getGlycanInBoxOutput() {
		  return glycanInBox;
	  }  
}
