package gui;

import glycoMain.UnicarbKbDico;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.util.HashMap;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import action.AllEnzymeAction;
import action.CheckDisplayInAction;
import action.CheckDisplayOutAction;
import action.ClearAction;
import action.DisplayGlycanInputAction;
import action.DisplayGlycanOutputAction;
import action.DoubleClickEnzList;
import action.DownloadResultsAction;
import action.IdLoadAction;
import action.LoadFileAction;
import action.MatchUnicarbAction;
import action.SubmitAction;
import action.TranslationAction;
import action.TranslationActionBis;
import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class SugarPanel extends JPanel {

	
	HashMap<String,String> unicarbKb;
	String[] enz_list = {"JBM", "BTG", "SPG", "CBG", "BKF", "AMF", "XMF", "GUH", "JBH", "ABS", "NAN1"};

	public SugarPanel(){
		MigLayout miglay = new MigLayout ("align 50%", "[]related*3[][][]related*3","[][]related*3[][]related*3[][]related*3[][][]related*3[][][][]");
		setLayout(miglay);
		unicarbKb = new HashMap<String,String>();
		UnicarbKbDico un = new UnicarbKbDico();
		unicarbKb = un.getUnicarbHash();
		initPanel();
	}
	
	
	public JLabel addImageLabel(String way){
		JLabel jl = new JLabel (new ImageIcon(getClass().getResource(way)));
		return jl;
	}
	
	public JLabel addTextLabel(String text, int format){
		JLabel j = new JLabel();
		j.setText(text);
		if (format == 2)
			j.setFont(new Font("Cambria", Font.BOLD, 14));		
		else if (format == 3)
			j.setFont(new Font("Times New Roman", Font.PLAIN, 13));
		return j;
	}
	
	public JSeparator addSeparator(int format){
		JSeparator js = new JSeparator();
		if (format==1)
			js.setForeground(Color.DARK_GRAY);
		if (format==2)
			js.setForeground(new Color(192,192,192));
		return js;
	}
	
	public void createListEnzyme(DefaultListModel<String> listModel){
		for (int i=0; i<enz_list.length; i++)
			listModel.addElement(enz_list[i]);
	}
	
	public JCheckBox addCheckBox (String name, int format){
		JCheckBox check = new JCheckBox(name);
		if (format ==2)
			check.setFont(new Font("Cambria", Font.BOLD, 14));	
		else if (format==1)
			check.setFont(new Font("Times New Roman", Font.LAYOUT_LEFT_TO_RIGHT, 14));
		return check;
	}
	
	public JButton addSimpleButton(String name, boolean visible){
		JButton jb = new JButton(name);
		jb.setMargin(new Insets(0,0,0,0));
		jb.setFont(new Font("Times New Roman", Font.LAYOUT_LEFT_TO_RIGHT, 14));
		jb.setForeground(Color.black);
		jb.setVisible(visible);
		return jb;
	}
	
	public JButton addActionButton(String name, int format, AbstractAction action){
		JButton jb = new JButton(action);
		jb.setText(name);
		if (format ==1){
			jb.setMargin(new Insets(0,0,0,0));
			jb.setFont(new Font("Times New Roman", Font.LAYOUT_LEFT_TO_RIGHT, 14));
			jb.setForeground(Color.black);
		}
		else if (format==5){
			jb.setFont(new Font("Cambria", Font.BOLD+Font.ITALIC, 16));
			jb.setForeground(new Color(187, 210, 225));
			jb.setBackground(new Color (3, 34, 76));
		}
		jb.setVisible(true);
		return jb;
	}
	
	public void addRadioButton (JRadioButton glycoCT, JRadioButton iupac, JRadioButton cfg, ButtonGroup bgroup, JTextArea areaInput){
		iupac.setAction(new TranslationAction(areaInput, this, iupac));
		cfg.setAction(new TranslationActionBis(areaInput, this, iupac));
		iupac.setText("IUPAC");
		cfg.setText("CFG");
		glycoCT.setFont((new Font("Cambria", Font.BOLD, 13)));
		iupac.setFont((new Font("Cambria", Font.BOLD, 13)));
		cfg.setFont((new Font("Cambria", Font.BOLD, 13)));
		bgroup.add(glycoCT);
		bgroup.add(iupac);
		bgroup.add(cfg);
	}
	
	public void retrieveText(JCheckBox displayInputCheck,JCheckBox displayOutputCheck, JButton displayInputOk,JButton displayOutputOk, JCheckBox allEnz) {
//		when I assign something.setAction, the name of button disappears, why? so I rename the button
		displayInputCheck.setText("Display Glycan Structures");
		displayOutputCheck.setText("Display Glycan Structures");
		displayInputOk.setText("Ok");
		displayOutputOk.setText("Ok");
		allEnz.setText("All");	
	}
	
	@SuppressWarnings("static-access")
	public void initPanel(){
		
/*		title and separator	*/
		JLabel pictureTitle = addImageLabel("/pictures/title.png");
		add(pictureTitle, "spanx 5, center");
		add(addSeparator(1), "newline, spanx 5, growx");
		
/*		3 steps and separators  */
		JLabel step1 = addTextLabel ("1. Load glycan sequence(s)", 2);
		JLabel step2 = addTextLabel ("2. Choose enzyme(s) to digest and SUBMIT", 2);
		JLabel step3 = addTextLabel ("3. Simulation results", 2);
		add(step1, "newline");
		add(step2, "spanx 3");
		add(step3);
		add(addSeparator(2), "newline, growx");
		add(addSeparator(2), "spanx 3, growx");
		add(addSeparator(2), "growx");
		
/*		Step 1 - Input 1: scrollpane with textArea */
		JLabel input1 = addTextLabel("- enter sequence(s) and choose the format", 3);
		JLabel input2 = addTextLabel("- or load a file containing sequence(s)", 3);
		JLabel input3 = addTextLabel("- or enter a UniCarb id", 3);
		JTextArea areaInput = new JTextArea(20,30);
		JScrollPane scrollpaneInput = new JScrollPane(areaInput);
		scrollpaneInput.setHorizontalScrollBarPolicy(scrollpaneInput.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JTextField file = new JTextField(17);
		JTextField id = new JTextField (17);
		JButton fileButton = addActionButton("...", 1, new LoadFileAction(this, file, areaInput));
		JButton idButton = addActionButton("OK",1, new IdLoadAction(areaInput, id, unicarbKb));
		JRadioButton iupac = new JRadioButton("IUPAC");
		JRadioButton glycoCT = new JRadioButton("GlycoCT");
		JRadioButton cfg = new JRadioButton("CFG");
		ButtonGroup bgroup = new ButtonGroup();
		addRadioButton(glycoCT, iupac, cfg, bgroup, areaInput);
		JLabel blank = new JLabel("");
		
/*		Step 2 - List of enzymes  */
		DefaultListModel<String> listModel1 = new DefaultListModel<String>();
		DefaultListModel<String> listModel2 = new DefaultListModel<String>();
		JList<String> listEnzyme = new JList<String>(listModel1);
		JList<String> listSelectedEnzyme = new JList<String>(listModel2);
		createListEnzyme(listModel1);
		JScrollPane scrollEnzyme = new JScrollPane(listEnzyme);
		JScrollPane scrollSelectedEnzyme = new JScrollPane(listSelectedEnzyme);
		scrollEnzyme.setPreferredSize(new Dimension(150,200));	
		scrollSelectedEnzyme.setPreferredSize(new Dimension(150,200));
		listEnzyme.addMouseListener(new DoubleClickEnzList(listEnzyme, listModel1, listModel2));
		HelpEnzymeActivity infoEnzyme = new HelpEnzymeActivity();
		EnzButtonIn buttEnzIn = new EnzButtonIn(listEnzyme, listModel1, listModel2);
		EnzButtonOut buttEnzOut = new EnzButtonOut(listSelectedEnzyme, listModel1, listModel2);

	
/*		Step 3 - scrollpane with output  */
		JTextArea areaOutput = new JTextArea (20,30);
		JScrollPane scrollpaneOutput = new JScrollPane(areaOutput);
		scrollpaneOutput.setHorizontalScrollBarPolicy(scrollpaneOutput.HORIZONTAL_SCROLLBAR_NEVER);
		JLabel matchingNumber = new JLabel();
		JButton matchUnicarb = addSimpleButton("Matching UnicarbKB", true);
		JButton downloadResult = addActionButton("Download raw results", 1, new DownloadResultsAction(areaOutput));
		matchUnicarb.setAction(new MatchUnicarbAction(areaOutput, unicarbKb, matchingNumber, matchUnicarb));
		matchUnicarb.setText("Matching Unicarb DB");
		
/*		Glycan Structures  */
		
		JLabel waitPictures = addImageLabel("WaitPictures.png");
		JCheckBox displayInputCheck = addCheckBox("Display Glycan Structures", 2);
		JCheckBox displayOutputCheck = addCheckBox("Display Glycan Structures", 2);
		JComboBox<String> displayInputCombo = new JComboBox<String>();
		JComboBox<String> displayOutputCombo = new JComboBox<String>();
		displayInputCombo.setVisible(false);
		displayOutputCombo.setVisible(false);
		JButton displayInputOk = addSimpleButton("Ok", false);
		JButton displayOutputOk = addSimpleButton("Ok", false);
		CheckDisplayInAction checkInAction = new CheckDisplayInAction(areaInput, displayInputCombo, displayInputOk, iupac, displayInputCheck);
		CheckDisplayOutAction checkOutAction = new CheckDisplayOutAction(areaOutput, displayOutputCombo, displayOutputOk, displayOutputCheck);
		displayInputCheck.setAction(checkInAction);
		displayOutputCheck.setAction(checkOutAction);
		StructurePanel structurePanelInput = new StructurePanel();
		displayInputOk.setAction(new DisplayGlycanInputAction(areaInput, this, displayInputCombo, checkInAction.getGlycanInBoxInput(), unicarbKb, structurePanelInput));
		StructurePanel structurePanelOutput = new StructurePanel();
		displayOutputOk.setAction(new DisplayGlycanOutputAction(areaOutput, this, displayOutputCombo, listSelectedEnzyme, checkOutAction.getGlycanInBoxOutput(), unicarbKb, structurePanelOutput));

		
/*		Other buttons (clear, all enzymes, check, submit)  */
		JCheckBox allEnzCheck = addCheckBox("All", 1);
		JCheckBox checkUnknownLink = addCheckBox("Accepted unknown linkages", 1);
		checkUnknownLink.setFont(new Font("Times New Roman", Font.LAYOUT_LEFT_TO_RIGHT, 14));
		JButton submit = addActionButton("Digest", 5, new SubmitAction(this, areaInput, areaOutput, listSelectedEnzyme, checkUnknownLink, iupac));
		allEnzCheck.setAction(new AllEnzymeAction(listModel1, listModel2, allEnzCheck, enz_list));
		retrieveText(displayInputCheck, displayOutputCheck, displayInputOk, displayOutputOk, allEnzCheck);

		JButton clear = addActionButton(" Clear ", 1, new ClearAction(this, areaInput, file, id, bgroup, listModel1, listModel2, areaOutput, 
				matchingNumber, matchUnicarb, displayInputCheck, displayOutputCheck, displayInputCombo, displayOutputCombo, displayInputOk, displayOutputOk, 
				allEnzCheck, checkUnknownLink, structurePanelInput, structurePanelOutput));
		
		
/*Addition of component to the panel from left to right, from top to bottom  */
		add(input1, "newline");
		add(scrollpaneInput, "newline, spany 5, growx");
		add(blank, "spanx 3");
		add(scrollpaneOutput, "spany 8, grow");
		add(checkUnknownLink, "newline  related *2, spanx 2");	
		add(clear, "right");
		add(scrollEnzyme, "newline related *2");
		add(infoEnzyme, "split 3, flowy, top, center");
		add(buttEnzIn, "center, gaptop 40");
		add(buttEnzOut, "center");
		add(scrollSelectedEnzyme);
		add(allEnzCheck,"newline, spanx 3");
		add(submit, "newline, center, growx, spanx3");

		add(input2, "newline");
		add(file, "newline, split 2, growx");
		add(fileButton, "sizegroup 2");
		add(glycoCT, "newline, split 3, center");
		add(iupac, "center");
//		add(cfg, "center");
		
		add(input3, "newline");
		add(matchUnicarb, "skip 3, split 2, growx");
		add(downloadResult, "growx");
		add(id, "newline, split 2, growx");
		add(idButton, "sizegroup 2");
		add(matchingNumber, "skip 3");
		
		add(displayInputCheck, "newline");
		add(displayOutputCheck, "skip 3");
		add(addSeparator(2), "newline, growx");
		add(addSeparator(2), "skip 3, growx");		
		add(displayInputCombo, "newline, split 2, growx");
		add(displayInputOk, "sizegroup 2");
		add(displayOutputCombo, "skip 3, split 2, growx");
		add(displayOutputOk, "sizegroup 2");	
		add(waitPictures, "newline");
		
	}



}
