import java.awt.*;
import java.awt.event.*;

public class GUI extends Frame {
	
	private static Digest[] algorithm={
		new MD4(), new MD5(), new Sha160() , new RipeMD160() 
	};
	
	private Digest f = algorithm[0];
	
	private TextArea entry = new TextArea("OVDE UNESITE TEKST");
	private Label result = new Label("Rezultat:");
	private TextField hash = new TextField("",50);
	private Label executionTimeLabel = new Label("Vreme:  ");
	private TextField executionTimeTextField = new TextField("",50);
	private Button calc= new Button("Heširaj");
	private Button term= new Button("Završi");
	
	private class RadioChange implements ItemListener{
		public void itemStateChanged(ItemEvent d){
			String name = ((Checkbox)d.getSource()).getLabel();
			int i;
			for (i = 0; i < algorithm.length; i++)
				if((algorithm[i].name()).equals(name))break;
				
			f=algorithm[i];
		}
	}
	
	private void makeGUI () {
		
		add(entry, "Center");
		
		Panel panel =new Panel(new GridLayout(0,1));
		add(panel,"West");
		
		CheckboxGroup group = new CheckboxGroup();
		RadioChange rc = new RadioChange();
		for (int i = 0; i < algorithm.length; i++) {
			Checkbox radio = new Checkbox(algorithm[i].name(),group,i==0);
			radio.addItemListener(rc);
			panel.add(radio);
		}
		
		panel = new Panel(new GridLayout(3,0));
		add(panel, "South");
		Panel panel1 = new Panel(new GridBagLayout());
		Panel panel2 = new Panel(new GridLayout());
		Panel panel3 = new Panel(new GridBagLayout());
		panel.add(panel1);
		panel.add(panel3);
		panel.add(panel2);
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.RELATIVE;
		
	
		panel1.add(result,c);
		panel1.add(hash,c);
		
		panel3.add(executionTimeLabel,c);
		panel3.add(executionTimeTextField,c);
		
		
		calc.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent d) {
				long executionBeginingTime = System.nanoTime();
				f.update((entry.getText()).getBytes());
				byte[]md=f.digest();
				long executionEndingTime = System.nanoTime() - executionBeginingTime;
				hash.setText(Main.toString(md));
				executionTimeTextField.setText(Long.toString(executionEndingTime) + " ns");
			}
		});
		panel2.add(calc);
		
		term.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent d) {
				dispose();
			}
		});
		panel2.add(term);
		
		
	}
	
	
	private void addMenu(){
		
		MenuBar bar = new MenuBar(); setMenuBar(bar);
		Menu action = new Menu("Akcija"); bar.add(action);
		
		MenuItem item = new MenuItem("Heširaj", new MenuShortcut('P'));
		item.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent d) {
				long executionBeginingTime = System.nanoTime();
				f.update((entry.getText()).getBytes());
				byte[]md=f.digest();
				long executionEndingTime = System.nanoTime() - executionBeginingTime;
				hash.setText(Main.toString(md));
				executionTimeTextField.setText(Long.toString(executionEndingTime) + " ns");
			}
		});
		action.add(item);
		
		item = new MenuItem("Završi", new MenuShortcut('T'));
		item.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent d) {
				dispose();
			}
		});
		action.add(item);
	}
	
	
	public GUI(){
		
		super("Heš algoritmi"); setBounds(100,100,600,250);
		
		makeGUI(); addMenu(); setVisible(true);
		
		addWindowListener(new WindowAdapter(){
			
			public void windowClosing(WindowEvent d){dispose();}
		});
	}

}
