
import java.lang.*;
import java.io.*;
import java.net.*;

public class Agenda {

  public static final int DEFAULTPORT    = 2411;
  public static Socket agendaClient = null; 
  public static BufferedReader in = null;
  public static String hostname = "localhost";
  public static PrintWriter out = null;
  public static Modem connection;

  public static void main (String [] args) {
    try {
      agendaClient = new Socket(hostname,DEFAULTPORT); //dimiourgw tin syndesi
      in           = new BufferedReader(new InputStreamReader(agendaClient.getInputStream())); //orizw ti diataksi poy tha dexetai
															     //ta dedomena apo to Server
      out          = new PrintWriter(agendaClient.getOutputStream(),true);       //orizw ti diataksi poy tha 
     												         //stelnei ta dedomena sto Server
      connection   = new Modem();						//dimiourgw to stigmiotypo pou tha epeksergazetai thn pliroforia pou lambanei h pairnei
	AgendaGui ag = new AgendaGui(connection);
      }

    catch (UnknownHostException e) {
      System.out.println("Don't know about host: client.");
      System.exit(1);
      }

    catch(IOException e) {
      System.out.println("Could not make a connection.");
      System.exit(1);
      }
    }
  }


/****************************************************************************************************************************************
*     D  H  M  I  O  U  R  G  W    T  H  N    K  L  A  S  I    P  O  Y    T  H  A    S  T  E  L  N  E  I    K  A  I    T  H  A          *
*					       																    *
*     		      D  E  X  E  T  A  I    K  W  D  I  K  O  P  O  I  H  M  E  N  A    M  H  N  Y  M  A  T  A			          *	
*****************************************************************************************************************************************/
        
class Modem{
  private String apantisi,firstName,lastName,emailAddress,phone;
  private static String firstNameSent,lastNameSent,emailAddressSent,phoneSent;
  public DiaryEntry diary;

  //ORIZW TIS DIASTASEIS POU THA EXOUN OI DIAFORES KATAXWRISEIS 
  public static final int firstNameCorrectLength    = 20;
  public static final int lastNameCorrectLength     = 20; 
  public static final int emailAddressCorrectLength = 40; 
  public static final int phoneCorrectLength        = 15; 



  //ORIZW TH THESI POY KATALAMBANOYN STO STRING OI DIAFORES KATAXWRISEIS 
  public static final int FIRSTNAMESTART = 1; 
  public static final int LASTNAMESTART  = 21;
  public static final int EMAILSTART     = 41;
  public static final int PHONESTART     = 81;
  public static final int PHONEEND       = 96;

  


//H METHODOS PACKAGING KWDIKOPOIEI THN PLIROFORIA SYMFWNA ME TO PROTOKOLLO EPIKOINWNIAS
  public static String packaging(String firstName,String lastName,String emailAddress,String phone) {

    if(firstName.length() <= firstNameCorrectLength) {
	firstNameSent= new String(firstName);
	for (int i=0;i<(firstNameCorrectLength-(firstName.length()));i++) 
	  {firstNameSent = new String(firstNameSent.concat(" "));}
      }
    else
      {firstNameSent = new String(firstName.substring(0,firstNameCorrectLength));}


    if(lastName.length() <= lastNameCorrectLength) {
	lastNameSent= new String(lastName);
	for (int i=0;i<(lastNameCorrectLength-(lastName.length()));i++) 
	  {lastNameSent = new String(lastNameSent.concat(" "));}
      }
    else
      {lastNameSent = new String(lastName.substring(0,lastNameCorrectLength));}


   if(emailAddress.length() <= emailAddressCorrectLength) {
	emailAddressSent= new String(emailAddress);
	for (int i=0;i<(emailAddressCorrectLength-(emailAddress.length()));i++) 
	  {emailAddressSent = new String(emailAddressSent.concat(" "));}
      }
     else
      {emailAddressSent = new String(emailAddress.substring(0,emailAddressCorrectLength));}


    if(phone.length() <= phoneCorrectLength) {
	phoneSent= new String(phone);
	for (int i=0;i<(phoneCorrectLength-(phone.length()));i++) 
	  {phoneSent = new String(phoneSent.concat(" "));}
      }
     else
      {phoneSent = new String(phone.substring(0,phoneCorrectLength));}


     return firstNameSent.concat(lastNameSent).concat(emailAddressSent).concat(phoneSent);

    }


   //H METHODOS SENDANDRECEIVE STELNEI KAI EPEITA DEXETAI THN APANTISI TOU SERVER
   public String sendAndReceive(String kwdikosEntolis, String orismata){

      //O CLIENT STELNEI THN PLIROFORIA
	String orismataSent = new String(kwdikosEntolis.concat(orismata));  //etsi stelnetai o kwdikos thw entolis kai to orisma                                                                             
      Agenda.out.println(orismataSent);
      Agenda.out.flush();

	//O CLIENT DEXETAI THN APANTISI
	try{
        apantisi = new String(Agenda.in.readLine());
	  return apantisi;
        }

      catch(IOException e) {
         System.out.println("Could not take data from server.");
         System.exit(1);
         return "";
         }
      }


	//H METHODOS UNPACKAGING APOKWDIKOPOIEI THN APANTISI TOY SERVER KAI THN EISAGEI STHN KATAXWRHSH DIARY ENTRY	
      public void unpackaging(String apantisi){        
        if(apantisi.charAt(0) == '0') {
           try{
	       firstName     = new String(apantisi.substring(FIRSTNAMESTART,LASTNAMESTART));
	       lastName      = new String(apantisi.substring(LASTNAMESTART,EMAILSTART));
	       emailAddress  = new String(apantisi.substring(EMAILSTART,PHONESTART));
	       phone         = new String(apantisi.substring(PHONESTART,PHONEEND));
	       diary         = new DiaryEntry(firstName,lastName,emailAddress,phone);
	       
	       AgendaGui.displayStatusBar.setText(" Ready");
	       }


            catch(StringIndexOutOfBoundsException e)
              {AgendaGui.displayStatusBar.setText(" Stringindex is out of bounds");}
           }
 
        else
           {AgendaGui.displayStatusBar.setText(apantisi);} // E  M  F  A  N  I  Z  W    M  Y  N  H  M  A    L  A  T  H  O  Y  S

        }

      }
