/*
 ***************************************************************************
 *		   Support all the dialog with the user
 *
 ***************************************************************************
 */

#include <dos.h>
#include <conio.h>
#include <alloc.h>
#include "stdio.h"
#include "assert.h"
#define TELLING_BUFFERS
#include "impulses.h"
#include "filling.h"
#include "writing.h"
#include "telling.h"

/*
 *--------------------------------------------------------------------------
 *			Some service functions
 */

#define at(x,y,message) {gotoxy(x,y); clreol(); cputs(message);}

#define beep() putchar('\007')

static void express_sorry(const char * message)
{
  at(5,10,message);
  at(5,11,"Try once more");
  beep();
  delay(700);
  at(5,10,"");
  at(5,11,"");
}

/*
 *--------------------------------------------------------------------------
 * 			Initial dialog with the user
 *		ask him for various parameters to work with
 */

static void print_title()
{
  char * tp = "Acquire  Impulses";
  int xpos = (80 - strlen(tp))/2;
  clrscr();
  highvideo();
  gotoxy(xpos,2);
  while( *tp != '\0' )
  {
    putch(*tp++);
    delay(70);
  }
  normvideo();
}
				/* Ask no channels to poll		*/
				/* until the user gives a proper answer	*/
static void ask_no_channels()
{
  char buffer[4];
  int channel_no;
  buffer[0] = 2;		/* Only one symbol is desired + '\0'	*/
  for(;;)
  {
    gotoxy(1,1); clreol();
    cputs("No of channels to poll ");
    if( cgets(buffer), buffer[1] != 1 )
      continue;			/* No symbol has been entered		*/
    channel_no = buffer[2] - '0';	/* No channels in binary form	*/
    if( channel_no <= 0 )
      continue;
    if( set_no_channels_to_poll(channel_no) == 0 )
      break;
    else
      express_sorry("Sorry, I can't handle that no of channels You have specified");
  }
}

			/* Ask the user to comment the info he receives	*/
			/* on the channels				*/
static void ask_for_comments()
{
  const int max_comment_length = 60;	/* +2 bytes for cgets() routine	*/
					/* +1 byte for '\0' should be kept*/
					/* in mind                        */
  register int i;

  assure( (Comments_to_channels = calloc(No_channels_polled+1,sizeof(char *))) != (void *)0,
	  "No memory to allocate Comments_to_channels");
  Comments_to_channels[0] = "";
  for(i=1; i<= No_channels_polled; i++)
  {
    char * cp;
    assure( (cp = calloc(max_comment_length+3,sizeof(char))) != (void *)0,
	  "No memory to allocate the comment string");
    at(1,1+2*i,"Channel "); cprintf("%d ",i);
    cp[0] = max_comment_length;		/* Max no. chars to read	*/
    cgets(cp);
    Comments_to_channels[i] = &cp[2];
    cprintf("\n\r   %s",poll_signal_description(i));
  }
}

static void ask_for_file_name()
{
  char buffer[43];	/* Two leading bytes are needed for cgets()	*/
			/* the lats byte is reserved for '\0'		*/
  const int line_no = wherey()+1;
  for(;;)
  {
    register char * p;
    at(1,line_no,"Name for the data file ");
    if( (p=set_output_file_name(cgets(buffer))) == (char *)0 )
      break;
    else
      express_sorry(p);
    }
}


void initial_dialog()
{
  register int i;

  print_title();
  window(1,4,80,24);
  ask_no_channels();
  ask_for_comments();
  ask_for_file_name();
}

/*
 *--------------------------------------------------------------------------
 *			Display the current status
 */

void tell_current_status()
{
  at(30,14,"Total ");
  cprintf("%ld impulses have been acquired",Total_no_impulses);

  at(1,15,"Current Status: ");
  switch(Status)
  {
    case 'S':
	 cputs("Waiting for an impulse on channel 1");
	 break;

    case 'W':
	 cputs("Writing the current buffer");
	 break;

    case 'Q':
	 highvideo();
	 cputs("Terminated on user's request");
	 normvideo();
	 break;

    default:
	 cputs("Filling in the buffer");
	 break;
  }
  cprintf("\n\rBuffer being filled %c",Current_buffer->id);
  cprintf("\n\r  %u elements have been filled, %u are to be filled",
	  Current_buffer->size-No_elements_to_fill,No_elements_to_fill);
}

