/*********************************************************/
/*    Autori:                                            */
/*            Pelosi Giovanni, 346787, pelosi            */
/*            Pesce  Agatino,  456868, pesce             */
/*                                                       */
/*  Progetto: Sistemi 2: biblioteca                      */
/*  Data Pr.: 9/2/96                                     */
/*      File: client.c                                   */
/*                                                       */
/*********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "system.h"
#include "socket.h"
#include "libro.h"
#include "message.h"
#include "socket.h"
#include "support.h"

#define LINE_LEN     128


#define FALSE 0
#define TRUE 1




/*
************************************************************************
*
*  lettura messaggio query
*
************************************************************************
*/


static int ReadMsgQuery(int Socket, char* Text)
{
	 int InChars=0;
	 int MsgLen=MessageLen(MsgTypeReply);
    int NLetti=0;

	 for(InChars=0; InChars<MessageLen(MsgTypeReply);)
	 {
		  NLetti=read(Socket, Text+InChars, MessageLen(MsgTypeReply)-InChars);
		  if(NLetti==0)
				return -1;
		  if(NLetti<0)
				return -1;
		  InChars+=NLetti;
		  if ( InChars == MsgLen )
				return 0;
	 }
}

/*
************************************************************************
*
*  client
*
************************************************************************
*/

void Client(char * Host, char * Port)
{
	 char buffer[LEN_TITOLO+1]; /* Per poter leggere \n e \0 */
	 int ExitReq=FALSE;
	 int Socket=0;
	 Libro_t Libro;
	 MessageText_t Text;
	 int OutChars=0;
	 int pause=TRUE;

	 memset(Text, 0, sizeof(MessageText_t));
	 while ( !ExitReq )
	 {
		  puts("Digitare la chiave\nq per terminare\np toggle pausa");
		  fgets(buffer, sizeof(buffer), stdin);
		  if ( strcmp(buffer,"q\n") == 0 )
		  {
				ExitReq=TRUE;
				continue;
		  }
		  if ( strcmp(buffer,"p\n") == 0 )
		  {
				pause=!pause;
				if(pause)
					 puts("pause is on");
				else
					 puts("pause is off");
				continue;
		  }

		  /* Nel caso avessi terminato per troppi caratteri chiudo la stringa*/
		  buffer[LEN_TITOLO-1] = 0;
		  /* Nel caso avessi terminato per \n lo tiro via*/
		  if( buffer[strlen(buffer)-1] == '\n' )
				buffer[strlen(buffer)-1]=0;
		  strcpy(Libro.Titolo, buffer);

		  OutChars=ClPutMsgQuery(Text, FALSE, &Libro);
		  Socket=ActiveSocket(Host, Port);
		  write(Socket, Text, OutChars);
		  shutdown(Socket,1);     /* non voglio pił scrivere */

		  while ( ReadMsgQuery(Socket, Text) == 0 )
		  {
				MessageReply_t MsgR;
				memset(&MsgR, 0, sizeof(MessageReply_t));
				ClGetMsgReply(&MsgR, Text+1);   /* Tiro Via il MessageType */
				puts(MsgR.Biblio);
				puts(MsgR.Libro.Titolo);
				puts(MsgR.Libro.Autore);
				puts(MsgR.Libro.Anno);
				if (pause)
				{
					 puts("-------------------\nPremere invio\n----------------------");
					 getchar();
				}
		  }
		  close(Socket);
	 }
}

/*
************************************************************************
*
*  main client
*
************************************************************************
*/

void main(void)
{
	 char Port[10];
	 char Host[50];
	 puts("Inserire l'host");
	 gets(Host);
	 puts("Inserire la porta");
	 gets(Port);
	 Client(Host, Port);
	 exit(0);
}