/*
 *      XCountdown - X version of the popular Channel 4 quiz show
 *
 * This software comes with NO warranty whatsoever. I therefore take no
 * responsibility for any damages, losses or problems caused through use
 * or misuse of this program.
 *
 * I hereby grant permission for this program to be freely copied and
 * distributed by any means, provided no charge is made for it.
 *
 * Matthew Chapman, csuoq@csv.warwick.ac.uk
 *    June 1995.
 */

/* xcounundrum.c - interface routines for the conundrum */

#include "xcountdown.h"
#include "globals.h"

void Conun(void);
void Bing(Widget w, XtPointer client_data, XtPointer call_data);
void EndConun(void);

static Boolean has_answered_conun[NP],got_conun,in_ans;
static char orig[NL+1];

/* main interface to the conundrum */
void Conun()
{
    char buff[NL+1],mixed[NL+1],txt[NL+1]="COUNTDOWN";
    int i,k;	

    DoMess("And now for the final round: the Conundrum.",3000);
    InitRound();

    /* initialize flags */
    stop_the_clock=False;
    got_conun=False;
    in_ans=False;
    in_conundrum=True;

    /* pick a conundrum */
    pick_conun(orig,mixed);
    /* display conundrum */
    for (i=0; i<NP; i++)
	for (k=0; k<NL; k++)
	{
	    sprintf(buff,"%c",txt[k]);
	    XtVaSetValues(lett[i][k][1],XtNlabel,buff,SETBG(steelblue),NULL);
	    XtVaSetValues(lett[i][k][0],XtNlabel," ",SETBG(gray40),NULL);
	}
    DoMess("Right, please reveal today's Countdown Conundrum.",3000);
    for (i=0; i<NP; i++)
	has_answered_conun[i]=False;
    WaitForMess();
    for (i=0; i<NP; i++)
	for (k=0; k<NL; k++)
	{
	    sprintf(buff,"%c",mixed[k]);
	    XtVaSetValues(lett[i][k][0],XtNlabel,buff,SETBG(steelblue),NULL);
	}
    /* start the clock */
    XtAppAddTimeOut(app_context,CLOCK_DELAY,Click,NULL);
    ChangeCarol(1);
    DoMess("Enter your answer and press RETURN...",3000);
    /* wait for time to run out, or two incorrect answers */
    while (comp < 5000)
	ProcessEvent();	
    ChangeCarol(2);
    in_conundrum=False;
    if (!got_conun)
	EndConun();
}

/* callback for pressing return in input box */
void Bing(Widget w, XtPointer client_data, XtPointer call_data)
{
    /* must be during the conundrum round, and waiting for an answer,
       or waiting for names at start of game, otherwise do nothing */
    if (in_conundrum && !in_ans)
    {
	int dnum = (XtDisplay(w)==dpy[0]) ? 0 : 1;
	String ans;
	char word[NL+1],buff[1024];
	int i,j,k;

	/* signal answer to block other player from answering */
	in_ans=True;
	/* check player hasn't previously answered */
	if (!has_answered_conun[dnum])
	{
	    has_answered_conun[dnum]=True;		
	    stop_the_clock = True;
	    /* get answer from input box */
	    XtVaGetValues((dnum==0) ? input1[0] : input2[1],XtNstring,&ans,NULL);
	    strncpy(word,ans,NL);
#ifdef DEBUG
	    printf("word=%s.\n",word);
#endif
	    if ((int)strlen(word)>0)
	    {
		XawTextBlock txt;

		/* show answer to other player */
		txt.firstPos=0;
		txt.format=FMT8BIT;
		txt.length=strlen(word);
		txt.ptr=word;
		XawTextReplace((dnum==0) ? input1[1] : input2[0],0,NL,&txt);

		/* convert to upper case */
		for (j=0; j<(int)strlen(word); j++)
		    word[j]=toupper(word[j]);
		/* remove any trailing spaces */
		while (word[strlen(word)-1]==' ')
		    word[strlen(word)-1]='\0';
	    }
	    if (!strcmp(orig,word))
	    {
		/* answer was correct - display it */
		sprintf(buff,"Let's see if you're right %s..."	,
			app_data.name[dnum]);
		DoMess(buff,1000);
		WaitForMess();
		for (i=0; i<NP; i++)
		    for (k=0; k<NL; k++)
		    {
			sprintf(buff,"%c",word[k]);
			XtVaSetValues(lett[i][k][1],XtNlabel,buff,SETBG(steelblue),NULL);
		    }
		sprintf(buff,"Well done. So %s gets 10 points.",app_data.name[dnum]);
		DoMess(buff,3000);
		UpdateScore(dnum,10);
		got_conun=True;
		comp=5000;
	    }
	    else
	    {
		if (has_answered_conun[1-dnum])
		{
		    /* both players have got the answer wrong */
		    sprintf(buff,"Sorry, that is wrong %s.",app_data.name[dnum]);
		    DoMess(buff,2000);
		    WaitForMess();
		    /* jump to end of round */
		    comp=5000;
		}
		else
		{
		    /* restart clock for other player */
		    sprintf(buff,"Sorry, that is wrong %s. Restart the clock.",
			    app_data.name[dnum]);
		    DoMess(buff,2000);
		    WaitForMess();
		    stop_the_clock = False;	
		    XtAppAddTimeOut(app_context,CLOCK_DELAY,Click,NULL);
		}
	    }
	}
	in_ans=False;
    }
    /* for entering names at the start (nothing to do with conundrum) */
    if (waiting_for_names)
    {
	int dnum = (XtDisplay(w)==dpy[0]) ? 0 : 1;
	String ans;

	if (!got_name[dnum])
	{		
	    XawTextBlock txt;
	    char empty='\0';
	    int i;
			
	    XtVaGetValues((dnum==0) ? input1[0] : input2[1],XtNstring,&ans,NULL);
	    if ((int)strlen(ans)>0)
	    {
		got_name[dnum]=True;
		strncpy(app_data.name[dnum],ans,11);
		for (i=0; i<NP; i++)
		    XtVaSetValues((dnum==0) ? name1[i] : name2[i],XtNlabel,
				  app_data.name[dnum],NULL);

		/* clear box */
		txt.firstPos=0;
		txt.format=FMT8BIT;
		txt.length=0;
		txt.ptr=&empty;
		XawTextReplace((dnum==0) ? input1[0] : input2[1],0,999,&txt);
	    }
	}
    }
}

/* display answer if no-one gets it */
void EndConun()
{
    int i,k;
    char buff[NL+1];

    DoMess("Never mind. Let's see what it was.",3000);
    Wait(2000);
    for (i=0; i<NP; i++)
	for (k=0; k<NL; k++)
	{
	    sprintf(buff,"%c",orig[k]);
	    XtVaSetValues(lett[i][k][1],XtNlabel,buff,SETBG(steelblue),NULL);
	}
    Wait(2000);
}
