import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.lang.*;

public class AgendaGui extends Frame
{
  public static MyWindow window,window2,window3;
  public MyCharButton buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, buttonG, buttonH, buttonI, buttonJ, buttonK, buttonL, buttonM ;
  public MyCharButton buttonN, buttonO, buttonP, buttonQ, buttonR, buttonS, buttonT, buttonU, buttonV, buttonW, buttonX, buttonY, buttonZ ;
  public MyButton buttonDelete,buttonEdit, buttonSearch, buttonSaveAgenda, buttonLoadAgenda;
  public MyButton  buttonAddNew, buttonNext, buttonPrevious, buttonSave, buttonOk3;
  public MyTextField textFirstName, textLastName, textEmailAddress, textPhone;
  public MyTextField textFirstName2, textLastName2, textEmailAddress2, textPhone2, textStatusBar;
  public static MyDisplayField displaySearch, displayData, displaySaveAgenda, displayLoadAgenda ;
  public static MyDisplayField displayFirstName, displayLastName, displayEmailAddress, displayPhone, displayStatusBar;
  public static MyDisplayField displayFirstName2, displayLastName2, displayEmailAddress2, displayPhone2;
  public static MyDisplayField displayError3;


  /**
    * Constructor for objects of class AgendaGui
    */
     
  public AgendaGui(Modem connection) {

       
       //W  I  N  D  O  W  S'    D  E  F  I  N  T  I  O  N

       window = new MyWindow("ADDRESS BOOK", 800, 500, 80, 80, true,1);
       window2 = new MyWindow("", 800, 170, 100, 300, false,2);
	 window3 = new MyWindow("", 620, 100, 225, 400, false,3);
       
 //****************************************************************************************************************************************************
 //****************************************************************************************************************************************************      
       //S  E  T  T  I  N  G  S    F  O  R    T  H  E    F  I  R  S  T    W  I  N  D  O  W


       //D  I  S  P  L  A  Y     S  E  T  T  I  N  G  S    

       textFirstName      = new MyTextField("  First Name", 50, 150, 130, 25, false,1);
	 textLastName       = new MyTextField("  Last  Name", 50, 175, 130, 25, false,1);
	 textEmailAddress   = new MyTextField("  Email Address ", 50, 200, 130, 25, false,1);
	 textPhone          = new MyTextField("  Phone  ", 50, 225, 130, 25, false,1);
       textStatusBar      = new MyTextField("    Status:  ", 10, 465, 80, 25, false,1);


       // B  U  T  T  O  N  S    F  O  R    C  H  A  R  A  C  T  E  R  S
      
       buttonA = new MyCharButton("A",  51, 80, 27, 40); 
       buttonB = new MyCharButton("B",  78, 80, 27, 40); 
       buttonC = new MyCharButton("C", 105, 80, 27, 40);  
       buttonD = new MyCharButton("D", 132, 80, 27, 40); 
       buttonE = new MyCharButton("E", 159, 80, 27, 40); 
       buttonF = new MyCharButton("F", 186, 80, 27, 40); 
       buttonG = new MyCharButton("G", 213, 80, 27, 40); 
       buttonH = new MyCharButton("H", 240, 80, 27, 40); 
       buttonI = new MyCharButton("I", 267, 80, 27, 40); 
       buttonJ = new MyCharButton("J", 294, 80, 27, 40); 
       buttonK = new MyCharButton("K", 321, 80, 27, 40); 
       buttonL = new MyCharButton("L", 348, 80, 27, 40); 
       buttonM = new MyCharButton("M", 375, 80, 27, 40); 
       buttonN = new MyCharButton("N", 402, 80, 27, 40); 
       buttonO = new MyCharButton("O", 429, 80, 27, 40); 
       buttonP = new MyCharButton("P", 456, 80, 27, 40); 
       buttonQ = new MyCharButton("Q", 483, 80, 27, 40); 
       buttonR = new MyCharButton("R", 510, 80, 27, 40); 
       buttonS = new MyCharButton("S", 537, 80, 27, 40); 
       buttonT = new MyCharButton("T", 564, 80, 27, 40); 
       buttonU = new MyCharButton("U", 591, 80, 27, 40); 
       buttonV = new MyCharButton("V", 618, 80, 27, 40); 
       buttonW = new MyCharButton("W", 645, 80, 27, 40); 
       buttonX = new MyCharButton("X", 672, 80, 27, 40); 
       buttonY = new MyCharButton("Y", 699, 80, 27, 40); 
       buttonZ = new MyCharButton("Z", 726, 80, 27, 40); 


       //T  E  X  T    F  I  E  L  D  S    F  O  R    G  I  V  I  N  G    D  A  T  A    

       displaySearch          = new MyDisplayField(553, 150, 200,  30, true, 1);
       displayFirstName       = new MyDisplayField(180, 150, 270,  25, false, 1);
       displayLastName		= new MyDisplayField(180, 175, 270,  25, false, 1);
       displayEmailAddress    = new MyDisplayField(180, 200, 270,  25, false, 1);
	 displayPhone           = new MyDisplayField(180, 225, 270,  25, false, 1);
       displaySaveAgenda      = new MyDisplayField(225, 350, 225, 30, true, 1);
       displayLoadAgenda      = new MyDisplayField(225, 400, 225, 30, true, 1);
       displayStatusBar       = new MyDisplayField(90, 465, 700, 25, false, 1);


       //B  U  T  T  O  N  S    F  O  R    O  P  E  R  A  T  I  O  N   

       buttonSearch         = new MyButton("Search", 'F', 483, 150, 60, 30, 1); 
       buttonEdit           = new MyButton(" Edit ", 'E', 553, 200, 200, 30, 1);
	 buttonAddNew	    = new MyButton(" Add New Person", 'A', 553, 250, 200, 30, 1);
       buttonDelete	    = new MyButton(" Delete ", 'D', 553, 300, 200, 30, 1);
       buttonSaveAgenda     = new MyButton(" Save Address Book as ", 'S', 50, 350, 165, 30, 1);
	 buttonLoadAgenda     = new MyButton(" Load Address Book ", 'L', 50, 400, 165, 30, 1);
       buttonNext		    = new MyButton(" Next Registration ", 'N', 300, 275, 150, 30, 1);
       buttonPrevious	    = new MyButton(" Previous Registration ", 'P', 50, 275, 150, 30, 1);

 //****************************************************************************************************************************************************
 //****************************************************************************************************************************************************


       //S  E  T  T  I  N  G  S    F  O  R    T  H  E    S  E  C  O  N  D    W  I  N  D  O  W


       //D  I  S  P  L  A  Y     S  E  T  T  I  N  G  S    

       textFirstName2     = new MyTextField("  First Name", 50, 50, 100, 25, false, 2);
       textLastName2      = new MyTextField("  Last Name", 170, 50, 130, 25, false, 2);
       textEmailAddress2  = new MyTextField("      EmailAddress", 325, 50, 180, 25, false, 2);
       textPhone2         = new MyTextField("  Telephone", 525, 50, 100, 25, false, 2);
             

       //T  E  X  T    F  I  E  L  D  S    F  O  R    G  I  V  I  N  G    D  A  T  A   

       displayFirstName2      = new MyDisplayField(50, 100, 100,  25, true, 2);
       displayLastName2       = new MyDisplayField(170, 100, 130,  25, true, 2);       
       displayEmailAddress2   = new MyDisplayField(325, 100, 180,  25, true, 2);
       displayPhone2          = new MyDisplayField(525, 100, 100,  25, true, 2);     


       // B  U  T  T  O  N  S    F  O  R    O  P  E  R  A  T  I  O  N

       buttonSave = new MyButton("Save", 's', 650, 100, 75,  25, 2); 
 
  //****************************************************************************************************************************************************
  //****************************************************************************************************************************************************

       //S  E  T  T  I  N  G  S    F  O  R    T  H  E    T  H  I  R  D    W  I  N  D  O  W


       //T  E  X  T    F  I  E  L  D  S    F  O  R    G  I  V  I  N  G    D  A  T  A    

	 displayError3          = new MyDisplayField(20, 50, 500, 20, false, 3); 


      // B  U  T  T  O  N  S    F  O  R    O  P  E  R  A  T  I  O  N   
       buttonOk3 = new MyButton("Ok", 'O', 550, 50, 50,  25, 3); 

       }
  }

 //****************************************************************************************************************************************************
 //****************************************************************************************************************************************************

                                  //  Y   L   O   P   O  I   H   S   H


// W  I  N  D  O  W    D  E  F  I  N  I  T  I  O  N

class MyWindow extends Frame {
    private String title;
    private int d1,d2,d3,d4,elegxosParathirou;
    private boolean b;
    public MyWindow(String title, int d1, int d2, int d3, int d4, boolean b, int elegxosParathirou){
      super(title);
      this.d1=d1;this.d2=d2;this.d3=d3;this.d4=d4;this.elegxosParathirou=elegxosParathirou;
      this.setLayout(null);
      this.setFont(new Font("TimesRoman", Font.PLAIN, 14));
      this.setBackground(Color.black);


      //W  I  N  D  O  W     S  E  T  T  I  N  G  S
      
      this.resize(d1,d2);                                    	       //orizw to megethos toy parathirou
      this.setLocation(d3,d4);      				   		 //orizw th thesi toy parathirou
      if(b){this.show();}						             //to emfanizw sthn othoni 
      this.setResizable(false); 					             //Gia na mhn metabaletai to megethos tou frame kai xanontai 
												 //ta plhktra

      this.addWindowListener(new CloseWindowAndExit(elegxosParathirou)); //Gia na mporei kaneis na kleisei (Alt-F4)
      }  											 // to window tou frame
    
    }


class MyCharButton extends Button {
    public String label;
    public int d1,d2,d3,d4;
    public MyCharButton(String label,int d1,int d2, int d3, int d4) {
       super(label);
       this.d1=d1;this.d2=d2;this.d3=d3;this.d4=d4;
       this.reshape(d1,d2,d3,d4);
       this.setFont(new Font("TimesRoman", Font.PLAIN,14));
       this.setForeground(Color.blue);
       this.addActionListener(new ButtonCharHandler(label));
       AgendaGui.window.add(this);
       }
    }


class MyButton extends Button {
    public String label;
    public char c;
    public int d1,d2,d3,d4;
    private int elegxosParathirou; 
    public MyButton(String label,char c,int d1,int d2, int d3, int d4, int elegxosParathirou) {
       super(label);
       this.c=c;this.d1=d1;this.d2=d2;this.d3=d3;this.d4=d4;this.elegxosParathirou=elegxosParathirou;
       this.reshape(d1,d2,d3,d4);
       this.setFont(new Font("TimesRoman", Font.PLAIN,14));
       this.setForeground(Color.blue);
       this.addActionListener(new ButtonHandler(c));
       switch(elegxosParathirou ) {

	   case 1: AgendaGui.window.add(this);
                 break;

	   case 2: AgendaGui.window2.add(this);
		     break; 

	   case 3: AgendaGui.window3.add(this);
		     break; 
         }
       }
    }


       

class MyTextField extends TextField {
  public String label2;
  public int d1,d2,d3,d4,elegxosParathirou;
  public boolean b;
  public MyTextField(String label2,int d1, int d2, int d3, int d4, boolean b, int elegxosParathirou) {
     super(label2);
     this.d1=d1;this.d2=d2;this.d3=d3;this.d4=d4;this.b=b;this.elegxosParathirou=elegxosParathirou;
     this.reshape(d1, d2, d3, d4);
     this.setEditable(b);
     switch(elegxosParathirou){ 
       
	   case 1: AgendaGui.window.add(this);
		     break;
      
         case 2: AgendaGui.window2.add(this);
		     break;

	   case 3: AgendaGui.window3.add(this);
		     break;
         }

     }
  }
	 



class MyDisplayField extends TextField {
  private String label2;
  private int d1,d2,d3,d4,elegxosParathirou;
  private boolean editable;
  public MyDisplayField(int d1,int d2,int d3,int d4,boolean editable, int elegxosParathirou) {
    super("");
    this.d1=d1;this.d2=d2;this.d3=d3;this.d4=d4;this.editable=editable;this.elegxosParathirou=elegxosParathirou;
    this.reshape(d1, d2, d3, d4);
    this.setEditable(editable);

    switch(elegxosParathirou) {
      case 1: AgendaGui.window.add(this);
              break;

      case 2: AgendaGui.window2.add(this);
              break;

      case 3: AgendaGui.window3.add(this);
              break;

      }
    }
  }





//           B     U     T     T    O    N               H     A     N     D     L     E     R     
//          ------------- C       L       A       S       S       E       S --------------------
//                      ----------------------------------------------------


class CloseWindowAndExit extends WindowAdapter{
    int elegxosParathirou;
    public CloseWindowAndExit(int elegxosParathirou) {this.elegxosParathirou=elegxosParathirou;}
    public void windowClosing(WindowEvent closeWindowAndExit){
      switch(elegxosParathirou){

         case 1: try{
           		Agenda.out.close();
		      Agenda.in.close();
           		Agenda.agendaClient.close();
	            System.exit(0);
       	      }
         
	           catch(java.io.IOException e) 
        		{System.exit(0);}
    

         case 2: AgendaGui.displayFirstName2.setText("");
	           AgendaGui.displayLastName2.setText("");
	           AgendaGui.displayEmailAddress2.setText("");
	           AgendaGui.displayPhone2.setText("");
	           AgendaGui.window2.hide();
		     break;


         case 3: AgendaGui.window3.hide();
		     break;

        }
      }
    }


class ButtonCharHandler implements ActionListener {
  private String label, orisma,apantisi;
  public ButtonCharHandler(String label) {this.label=label;}
  public void actionPerformed(ActionEvent pushingButton) {
    orisma = new String(label); 
    apantisi = new String(Agenda.connection.sendAndReceive("01",orisma));
    Agenda.connection.unpackaging(apantisi);
    if(apantisi.charAt(0) == '0') {								 //gia na mhn prospathei na emganisei dedomena otan den yparxoyn

      //emfanizontai ta diafora stoixeia ths kataxwrhshs sta antistoixa displays
	AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
      AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
	AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
	AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
      }   
   }
}



class ButtonHandler implements ActionListener {
   public char c;
   public int length;
   public String apantisi,name,firstName,lastName,emailAddress,phone,fileName,firstNameSent,lastNameSent,emailAddressSent,phoneSent;
   public String orisma;
   public static boolean editOrAdd;   
   public DiaryEntry tempDiary,diary;
   public ButtonHandler(char c) {this.c=c;}

   public void actionPerformed(ActionEvent pushingButton) {

     switch(c) {

       case 'N'     : apantisi = new String(Agenda.connection.sendAndReceive("02","")); 			    //o Client stelnei ta dedomena kai epeita 
																	    //dexetai thn apantisi ap' to Server
																	
                      Agenda.connection.unpackaging(apantisi);							    //h pliroforia apokwdikopoieitai
																	    //kai eisagetai sto DiaryEntry	
			    if(apantisi.charAt(0) == '0'){

                        //emfanizontai ta diafora stoixeia ths kataxwrhshs sta antistoixa displays
				AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
				AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
				AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
				AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
				}
			    break;


       case 'P'     : apantisi = new String(Agenda.connection.sendAndReceive("03","")); 			    //o Client stelnei ta dedomena kai epeita 
																	    //dexetai thn apantisi ap' to Server
													  
                      Agenda.connection.unpackaging(apantisi);							    //h pliroforia apokwdikopoieitai																	    //kai eisagetai sto DiaryEntry	

			    if(apantisi.charAt(0) == '0') {

                        //emfanizontai ta diafora stoixeia ths kataxwrhshs sta antistoixa displays
				AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
				AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
				AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
				AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
				}
			    break;


       case 'D'     : apantisi = new String(Agenda.connection.sendAndReceive("06","")); 			    //o Client stelnei ta dedomena kai epeita 
																	    //dexetai thn apantisi ap' to Server
													  
                      Agenda.connection.unpackaging(apantisi);							    //h pliroforia apokwdikopoieitai
																	    //kai eisagetai sto DiaryEntry	
			    if(apantisi.charAt(0) == '0') {

                        //emfanizontai ta diafora stoixeia ths kataxwrhshs sta antistoixa displays
				AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
				AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
				AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
				AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
				}
			    break;


	 case 'F'	  : name = new String(AgendaGui.displaySearch.getText());
			    if(name.length() != 0) {
  	                    apantisi = new String(Agenda.connection.sendAndReceive("01",name)); 		    //o Client stelnei ta dedomena kai epeita 
																	    //dexetai thn apantisi ap' to Server
													
				  Agenda.connection.unpackaging(apantisi);						    //h pliroforia apokwdikopoieitai
																	    //kai eisagetai sto DiaryEntry	
                          if(apantisi.charAt(0) == '0') {

                              //emfanizontai ta diafora stoixeia ths kataxwrhshs sta antistoixa displays
	  				AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
					AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
					AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
					AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
					}
  	                    AgendaGui.displaySearch.setText("");			 
				  }
                      else
      			  {AgendaGui.displayStatusBar.setText(" You must give a name");}
  
                      break;


       case 'S'	  : fileName = new String(AgendaGui.displaySaveAgenda.getText());
			    if(fileName.length() != 0) {
	                    apantisi = new String(Agenda.connection.sendAndReceive("07",fileName)); 	    //o Client stelnei ta dedomena kai epeita 
																	    //dexetai thn apantisi ap' to Server
													
				  Agenda.connection.unpackaging(apantisi);						    //h pliroforia apokwdikopoieitai
																	    //kai eisagetai sto DiaryEntry		
                          if(apantisi.charAt(0) == '0') {

                                //emfanizontai ta diafora stoixeia ths kataxwrhshs sta antistoixa displays
	    				  AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
					  AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
					  AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
					  AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
				 	  AgendaGui.displaySaveAgenda.setText("");			    
			              }
				  }
			    else 
                   	  {AgendaGui.displayStatusBar.setText(" You must give a file name");}

                      break;


       case 'L'	  : fileName = new String(AgendaGui.displayLoadAgenda.getText());
                      if(fileName.length() != 0) {
				  apantisi = new String(Agenda.connection.sendAndReceive("08",fileName)); 	    //o Client stelnei ta dedomena kai epeita 
																	    //dexetai thn apantisi ap' to Server
													
				  Agenda.connection.unpackaging(apantisi);						    //h pliroforia apokwdikopoieitai
																	    //kai eisagetai sto DiaryEntry	
                          if(apantisi.charAt(0) == '0') {

                                //emfanizontai ta diafora stoixeia ths kataxwrhshs sta antistoixa displays
	     			 	  AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
					  AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
					  AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
					  AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
					  AgendaGui.displayLoadAgenda.setText("");
					  }
				  }
			    else 
 				  {AgendaGui.displayStatusBar.setText(" You must give a file name");}

            	     break; 
				 
                     			    

       case 'E'     : AgendaGui.window2.show();
                      AgendaGui.displayFirstName2.setText(AgendaGui.displayFirstName.getText());
                      AgendaGui.displayLastName2.setText(AgendaGui.displayLastName.getText());
                      AgendaGui.displayEmailAddress2.setText(AgendaGui.displayEmailAddress.getText());
                      AgendaGui.displayPhone2.setText(AgendaGui.displayPhone.getText());
                      editOrAdd = false;
                      break;

      
       case 'A'     : AgendaGui.displayFirstName2.setText("");
                      AgendaGui.displayLastName2.setText("");
                      AgendaGui.displayEmailAddress2.setText("");
                      AgendaGui.displayPhone2.setText("");
                      AgendaGui.window2.show();
                      editOrAdd = true;
			    break;

       
       case 's'     : tempDiary = new DiaryEntry(AgendaGui.displayFirstName2.getText(),AgendaGui.displayLastName2.getText(),AgendaGui.displayEmailAddress2.getText(),AgendaGui.displayPhone2.getText());
 		         
                      if((tempDiary.emailAddress).length() != 0) {
				 //"paketarw" tin pliroforia symfwna me to prwtokollo epikoinwnias
                         orisma = Agenda.connection.packaging(tempDiary.firstName,tempDiary.lastName,tempDiary.emailAddress,tempDiary.phone); 
 	                   if(editOrAdd){
                            apantisi = new String(Agenda.connection.sendAndReceive("05",orisma)); 	    //o Client stelnei ta dedomena kai epeita 
																	    //dexetai thn apantisi ap' to Server
													
				    Agenda.connection.unpackaging(apantisi);						    //h pliroforia apokwdikopoieitai
																	    //kai eisagetai sto DiaryEntry	
				    AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
			          AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
			          AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
			          AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
			          
				    
                            }

    			       else {
				    apantisi = new String(Agenda.connection.sendAndReceive("04",orisma)); 	    //o Client stelnei ta dedomena kai epeita 
																	    //dexetai thn apantisi ap' to Server
													
				    Agenda.connection.unpackaging(apantisi);
				    AgendaGui.displayFirstName.setText(Agenda.connection.diary.firstName);
			          AgendaGui.displayLastName.setText(Agenda.connection.diary.lastName);
			          AgendaGui.displayEmailAddress.setText(Agenda.connection.diary.emailAddress);
			          AgendaGui.displayPhone.setText(Agenda.connection.diary.phone);
	 			    }

                         AgendaGui.displayFirstName2.setText("");
                         AgendaGui.displayLastName2.setText("");
                         AgendaGui.displayEmailAddress2.setText("");
                         AgendaGui.displayPhone2.setText("");
                         AgendaGui.window2.hide();
                         }
			    
                      else {
			       AgendaGui.displayError3.setText(" You must give an email address");
				 AgendaGui.window3.show();
    				 break;
				 }

                      break;


	 case 'O'	  : AgendaGui.window3.hide();			    
                      break;


                 
       }
    }
   }

