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

public class MidiPlayerVisual extends Panel implements ActionListener
{
	Label label1=new Label("Ava Fail:         ");
	Label label2=new Label("Salvesta Fail:");
	Label label3=new Label("BPM:");
	Label label4=new Label("Instrument");
	Button nupp1=new Button("Analyysi");
	Button nupp2=new Button("M2ngi");
	Button nupp3=new Button("Salvesta");
	Button nupp4=new Button("Instrumendid");
	Checkbox CB1=new Checkbox("Tagurpidi");
	TextArea TA1=new TextArea(20,20);
	TextField TF1=new TextField();
	TextField TF2=new TextField();
	TextField TF3=new TextField("",3);
	TextField TF4=new TextField("",3);

	public MidiPlayerVisual()
	{
		setLayout(new BorderLayout());
		Panel tegevused_all=new Panel(new GridLayout(1,3));
			tegevused_all.add(nupp2);
			tegevused_all.add(nupp1);
			tegevused_all.add(nupp3);
		Panel ylemine1=new Panel(new BorderLayout());
			ylemine1.add(label1, BorderLayout.WEST);
			ylemine1.add(TF1, BorderLayout.CENTER);
		Panel ylemine2=new Panel(new BorderLayout());
			ylemine2.add(label2, BorderLayout.WEST);
			ylemine2.add(TF2, BorderLayout.CENTER);
		Panel ylemine=new Panel(new GridLayout(2,1));
			ylemine.add(ylemine1);
			ylemine.add(ylemine2);
		Panel efektid=new Panel(new GridLayout(20,1));
			efektid.add(CB1);
			Panel BPM=new Panel(new GridLayout(1,2));
				BPM.add(label3);
				BPM.add(TF3);
			efektid.add(BPM); 
			Panel instrumendid=new Panel(new GridLayout(1,2));
				instrumendid.add(label4);
				instrumendid.add(TF4);
			efektid.add(instrumendid);
			efektid.add(nupp4);
		Panel keskel=new Panel(new BorderLayout());
			keskel.add(TA1, BorderLayout.CENTER);
			keskel.add(efektid, BorderLayout.EAST);
		add(ylemine, BorderLayout.NORTH);
		add(keskel, BorderLayout.CENTER);
		add(tegevused_all, BorderLayout.SOUTH);
		nupp1.addActionListener(this);
		nupp2.addActionListener(this);
		nupp3.addActionListener(this);
		nupp4.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().compareTo("Analyysi")==0)
		{
			try{TA1.append(MidiPlayer.analyysi(TF1.getText()));}catch(Exception ex){System.out.println(ex);}	
		}	
		if(e.getActionCommand().compareTo("M2ngi")==0)
		{
			int bpm;
			if(TF3.getText().compareTo("")!=0)
			{
				bpm=Integer.parseInt(TF3.getText());		
			}else{
				bpm=-1;
			}
			try{MidiPlayer.mangi(TF1.getText(), bpm);}catch(Exception exc){System.out.println(exc);}	
		}
		if(e.getActionCommand().compareTo("Salvesta")==0)
		{
			boolean tagurpidi=CB1.getState();
			int instrument;
			if(TF4.getText().compareTo("")!=0)
			{
				instrument=Integer.parseInt(TF4.getText());		
			}else{
				instrument=-1;
			}
			try{MidiPlayer.salvesta(TF1.getText(), TF2.getText(), tagurpidi, instrument);}catch(Exception exc){System.out.println(exc);}	
		}
		if(e.getActionCommand().compareTo("Instrumendid")==0)
		{
			try{TA1.append(MidiPlayer.instrumendid());}catch(Exception exce){System.out.println(exce);}	
		}
	}
		
}

class Raamikuular extends WindowAdapter
{
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);	
	}	
}