package gui;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import action.DownloadPicturesAction;
import action.LinkToUnicarbAction;
import net.miginfocom.swing.MigLayout;

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

	HashMap<byte[], String> picturesAndId = new HashMap<byte[], String>();
	ArrayList<byte[]> picturesOrdered = new ArrayList<byte[]>();
	String put = "";
	int choice;
	ArrayList<String> enz ;
		
	public StructurePanel(){
	}
	
	public void fill(){
		initStructurePanel();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setElement(HashMap piId, ArrayList piOr){
		this.picturesAndId = piId;
		this.picturesOrdered = piOr;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setElementForOutput(HashMap piId, ArrayList piOr, String put, ArrayList enz, int choice){
		this.picturesAndId = piId;
		this.picturesOrdered = piOr;
		this.put = put;
		this.enz = enz;
		this.choice = choice;
		
	}
	
	public JButton downloadPicturesButton(AbstractAction action, String text) {
		JButton jb = new JButton(action);
		jb.setText(text);
		jb.setFont(new Font("Times New Roman", Font.LAYOUT_LEFT_TO_RIGHT, 12));
		jb.setForeground(Color.black);
		return jb;

	}
	
	public void initStructurePanel(){
		MigLayout miglay = new MigLayout ("align 50%","[][]");
		setLayout(miglay);
		setBackground(Color.white);
		
		JLabel labEnz;
		JLabel link;
		JLabel image;
		JLabel labId;
		JButton save;

		for (int i=0; i<picturesOrdered.size(); i++){
			labEnz = new JLabel("");
			link = new JLabel("");
			String id = picturesAndId.get(picturesOrdered.get(i));
			
			image = new JLabel(new ImageIcon(picturesOrdered.get(i)));
			save = downloadPicturesButton(new DownloadPicturesAction(picturesOrdered.get(i), this), "download pictures");
			save.setText("download pictures");
			
			System.out.println("id : " + id);
			if (!id.contains("Unicarb")){
				link = new LinkToUnicarbAction(id);
				id = "Unicarb id : " + id;
			}
			labId = new JLabel(id.trim());

			if(put.equals("output"))
				labEnz.setText(enz.get(i));
			
			JSeparator js = new JSeparator();
			js.setForeground(new Color(192,192,192));
	
			add(labEnz, "newline related *3, center");
			add(labId, "newline");
			add(link, "newline");
			add(image, "newline, left");
			add(save, "newline, center");
			add(js, "newline related *2, growx");
		
		}
		
	}
	
}
