/**
	* GUI Class draw user interface and get input from user 
	* draw result to user
	*/
import GCRawData.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class GUI extends Frame{
	private Label lb_graph;
	private TextField tf_input;
	private MyButton bt_val;
	private Panel pn_result;
	private java.awt.List ls_result;
	private int frame_width = 400;
	private int frame_height = 500;
	private final int color_patch_size = 35;
	// constructor called when user run this program without parameters
	public GUI() {
		// default window size
		this( 400, 500 );
	}
	
	// constructor called when user run this program with parameters
	public GUI(int f_width, int f_height) {
		// set window non-resizable and attach event-processor to window
		this.setResizable( false );
		this.addWindowListener( new WindowAdapter() {
			public void windowClosing(WindowEvent e) { System.exit(0); }} // exit this program when window closed
		);
		// create components
		lb_graph = new Label("G = ");
		tf_input = new TextField();
		pn_result = new Panel();
		ls_result = new java.awt.List();
		bt_val = new MyButton("Get!", tf_input, pn_result, ls_result);
		// components setting - position and size
		lb_graph.setBounds(5, 35, 25, 25);
		tf_input.setBounds(30, 35, f_width-30-50, 25);
		tf_input.setEditable(true);
		bt_val.setBounds(frame_width-51, 35, 50, 25);
		pn_result.setBounds(0, 60, f_width, 100);
		ls_result.setBounds(0, 160, f_width, f_height-35-25-100);
		// window setting - add created component to window
		this.setLayout( null );
		this.setBounds(0,0, frame_width, frame_height);
		this.add(lb_graph);
		this.add(tf_input);
		this.add(pn_result);
		this.add(bt_val);
		this.add(ls_result);
		
	}
	
	// new MyButton component which inherit Button.
	// when user press MyButton( "Get!" ), MyButton get text which describe graph
	// from TextField then calculate min-color size using Parser(another Class) 
	// and finally display result to Panel( Color and Vertices ) and 
	// to List( History about job ) 
	private class MyButton extends Button implements ActionListener {
		TextField tf = null;
		Panel pn = null;
		java.awt.List ls = null;
		
		// constructor1
		public MyButton() { super(); addActionListener(this); }
		public MyButton(String arg) { super(arg); addActionListener(this); }
		// cinstructor2 with in, out component 
		public MyButton(String arg, Component get, Component show, Component show2) {
			super(arg);
			tf = (TextField)get;
			pn = (Panel)show;
			ls = (java.awt.List)show2;
			addActionListener(this);
		}
		// this function must be called after user use constructor1 to create MyButton 
		public void linkInOutComponents(TextField get, Panel show, java.awt.List show2) {
			tf = get;
			pn = show;
			ls = show2;
			
		}
		// this function performed when MyButton pressed!
		public void actionPerformed(ActionEvent e) {
			if ( tf != null && (tf.getText()).length() != 0) {
				// create Parser Class and draw result to Panel and List
				Parser ps = new Parser();
				if ( ps.getMinColor( new String(tf.getText()) ) != -1 )	drawResult(ps);
			}
		}// end of actionPerformed()
		
		public void drawResult(Parser ps) {
			Vertices vt = ps.getColorVertices();
			Graphics g = pn.getGraphics();
			Stack st = ps.getGraphStack();
		
			if (g != null) {
				int length = st.size();
				Vertices tmp_ver = null;
				String tmp_st = null;
				
				// draw result to Panel
				g.setColor(Color.white);
				g.fillRect(0, 0, frame_width, 100);
				g.setColor(Color.black);
				g.drawString("Minimum color size: "+vt.getColorSize(), 10, 25);
				// draw fisrt rect
				vt.setStart();
				String tmp = null;
				Color cl = null;
				for (int i=0; i< vt.size(); i++) {
					tmp = vt.getNextVertex();
					cl = vt.getColor(tmp);
					g.drawString(tmp, i*color_patch_size+2, 50);
					g.setColor(cl);
					g.fillRect(i*color_patch_size+2, 50, color_patch_size, 50);
					g.setColor(Color.black);
					g.drawRect(i*color_patch_size+2, 50, color_patch_size, 50);
				}
				// fill List by Histroy
				System.out.println("[Debug] GUI.MyButton.drawResult().length :"+length);
				ls.clear();
				for (int i = length; i>0; i--) {
					tmp_ver = (Vertices)st.pop();
					tmp_ver.setStart();
					tmp_st = "{";
					for (int j = 0; j< tmp_ver.size()-1; j++) {
						tmp = tmp_ver.getNextVertex();
						tmp_st += tmp;
						if ( tmp_ver.getColor(tmp) != null ) {
							tmp_st +=  
											("["+( tmp_ver.getColor(tmp) ).getRed()
											+","+( tmp_ver.getColor(tmp) ).getGreen()
											+","+( tmp_ver.getColor(tmp) ).getBlue()+"]");
						}
						tmp_st += ",";
					}
					tmp_st += tmp_ver.getNextVertex();
					tmp_st += "}";
					System.out.println("[Debug] GUI.MyButton.drawResult().tmp_st :"+tmp_st);
					ls.add(tmp_st,0);
				}
				ls.add("#### Stack ####", 0); 
			}// end of 'if' in drawResult()
		}// end of drawResult()
	}// end of class MyButton

}// end of class GUI