
// MirrorRead (MemoTec) by Ivanhoe/Quality^PAT
// Sun Jun 24 14:16:37 1999
// kontakt:
// Szymon Jessa, ul. Warszawska 20/6, 89-600 Chojnice
// lub (w czasie roku akademickiego):
// Szymon Jessa, DS 10, ul. Wyspiaïskiego 7, 80-434 Gdaïsk Wrzeszcz
// email: noco@box43.gnet.pl

// include
#include <stdio.h>
#include <ctype.h>
#include <time.h>

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/diskfont.h>
#include <proto/graphics.h>

#include "sc:source/wbargs.c"

// define
#define SCREEN_TXT "MirrorRead (MemoTec) by Ivanhoe/Quality^PAT (24.06.1999) * USE YOUR MIND! *"
#define WINDOW_TXT1 "MirrorRead (MemoTec) Window"
#define WINDOW_TXT2 "Use your memory & imagination"
#define WINDOW_TXT3 "See you tomorrow!"
#define DATA_FILE "PROGDIR:MRBase.dat"
#define MAX_LINE_LENGHT 50
#define MAX_QUESTION_LENGHT 20
#define MAX_ANSWER_LENGHT 20
#define MAX_WIDE_CHAR 'W'
#define MAX_WORD_LENGHT 20
#define REMEMBER 1
#define ANSWER 2
#define BLACK 1
#define WHITE 2
#define CODE_ESC 27
#define CODE_ENTER 13
#define CODE_DEL 8

// variable
struct IntuitionBase *IntuitionBase=NULL;
struct Library *DiskfontBase=NULL;

struct Window *win=NULL;
struct Screen *scr=NULL;
struct RastPort *rp;
struct TextFont *font;
struct TextAttr fontattr;

struct
{
   char fontname[50];
   int fontsize;
   int leftedge;
   int topedge;
   int questions;
   int remembertime;
   int answertime;
} tooltypes;

int datalenght=0;
UBYTE data[11000][13];

// prototype

void main(int argc, char *argv[]);
void safe_exit(void);
void error_exit(UBYTE err[]);
void bag_libraries(void);
void bag_font(void);
void bag_window(void);
void bag_data(void);

int new_intuitext(int x,int y,UBYTE txt[],int col);

void get_question(UBYTE *txt);
void loop(void);

// function

void main(int argc, char *argv[])
{
   if (argc==0)
   {
      argc=_WBArgc;
      argv=_WBArgv;
   }

   if (argc==8)
   {
 	   sprintf(tooltypes.fontname, "%.*s",strcspn(argv[1],";"),argv[1]);
      tooltypes.fontsize=atoi(argv[2]);
      tooltypes.leftedge=atoi(argv[3]);
      tooltypes.topedge=atoi(argv[4]);
      tooltypes.questions=atoi(argv[5]);
      tooltypes.remembertime=atoi(argv[6]);
      tooltypes.answertime=atoi(argv[7]);
   }
   else
      error_exit("za maîo lub za duûo lini Tool Types ikonki");

   bag_libraries();
   bag_font();
   bag_window();
   bag_data();
   loop();
}

void safe_exit(void)
{
   if (font!=NULL) CloseFont(font);
   if (win!=NULL) CloseWindow(win);
   if (DiskfontBase!=NULL) CloseLibrary(DiskfontBase);
   if (IntuitionBase!=NULL) CloseLibrary((struct Library *) IntuitionBase);
   exit(0);
}

void error_exit(UBYTE err[])
{
   printf("wystâpiî bîâd: %s\n",err);
   safe_exit();
}

void bag_libraries(void)
{
   IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",36);
   if (IntuitionBase==NULL) error_exit("nie mogë otworzyê 'intuition.library' v>=36");

   DiskfontBase=OpenLibrary("diskfont.library",36);
	if (DiskfontBase==NULL) error_exit("nie mogë otworzyê 'diskfont.library' v>=36");
}

void bag_font(void)
{
   fontattr.ta_Name=tooltypes.fontname;
   fontattr.ta_YSize=tooltypes.fontsize;
   fontattr.ta_Style=FS_NORMAL;
   fontattr.ta_Flags=FPF_PROPORTIONAL;

   font=(struct TextFont *)OpenDiskFont(&fontattr);
   if (font==NULL) error_exit("nie mogë otworzyê czcionki");
}

void bag_window(void)
{
   int width,height;
   struct IntuiText intuitext;
   char temp[MAX_LINE_LENGHT];

   scr=LockPubScreen(NULL);
   if (scr==NULL) error_exit("nie mogë otworzyê okna na jakimkolwiek ekranie");

   // dostosowanie wymiarów okna do rodzaju czcionki
   memset(temp,MAX_WIDE_CHAR,MAX_LINE_LENGHT-1);
   temp[MAX_LINE_LENGHT-1]=NULL;

   intuitext.ITextFont=&fontattr;
   intuitext.IText=temp;

   width=IntuiTextLength(&intuitext);
   height=4*font->tf_YSize+10;

   // ewentualne centrowanie okna na ekranie
   if (tooltypes.leftedge==-1) tooltypes.leftedge=(scr->Width-width)/2;
   if (tooltypes.topedge==-1) tooltypes.topedge=(scr->Height-height)/2;

   win=OpenWindowTags(NULL,
       WA_Left,tooltypes.leftedge,
       WA_Top,tooltypes.topedge,
       WA_Width,width,
       WA_Height,height,
       WA_Title,WINDOW_TXT1,
       WA_ScreenTitle,SCREEN_TXT,
       WA_PubScreen,scr,
       WA_GimmeZeroZero,TRUE,
       WA_DragBar,TRUE,
       WA_DepthGadget,TRUE,
       WA_CloseGadget,TRUE,
       WA_Activate,TRUE,
       WA_SmartRefresh,TRUE,
       WA_AutoAdjust,TRUE,
       WA_IDCMP,IDCMP_CLOSEWINDOW | IDCMP_INTUITICKS | IDCMP_VANILLAKEY | IDCMP_INACTIVEWINDOW,
       TAG_END,0);

   UnlockPubScreen(NULL,scr);

   if (win==NULL) error_exit("nie mogë otworzyê okna");

	rp=win->RPort;
   SetFont(rp,font);
}

void bag_data(void)
{
   FILE *file;
   char temp[100];
   int x;

   x=new_intuitext(-1,-1,"loading data...",WHITE);

   file=fopen(DATA_FILE,"r");

   if (file==NULL) error_exit("nie mogë odczytaê bazy danych");

   for (datalenght=0;;datalenght++)
   {
      if (fgets(temp,100,file)==NULL) break;

      sprintf(data[datalenght],"%.*s",strlen(temp)-1,temp);
   }
   fclose(file);

   sprintf(temp," %d",datalenght);
   x=new_intuitext(x,-1,temp,BLACK);
   new_intuitext(x,-1," words loaded",WHITE);

   Delay(50);
}

int new_intuitext(int x,int y,UBYTE txt[],int col)
{
   struct IntuiText intuitext;

   intuitext.FrontPen=col;
   intuitext.BackPen=BACKGROUNDPEN;
   intuitext.DrawMode=JAM1;
   intuitext.LeftEdge=0;
   intuitext.TopEdge=0;
   intuitext.ITextFont=&fontattr;
   intuitext.IText=txt;
   intuitext.NextText=NULL;

   if (x==-1) x=10;
   if (y==-1) y=font->tf_YSize-2;

   Move(rp,x,y+font->tf_YSize-2);
   ClearEOL(rp);

   PrintIText(rp,&intuitext,x,y);

   return IntuiTextLength(&intuitext)+x;
}

void get_question(UBYTE *txt)
{
   int x1;
   UBYTE temp[MAX_WORD_LENGHT];
   ULONG seconds,micros;

   x1=new_intuitext(-1,-1,"remember: ",BLACK);

   CurrentTime(&seconds,&micros);
   srand(seconds+micros+clock());

   sprintf(temp,"%s",data[rand() % datalenght]);
   new_intuitext(x1,-1,temp,WHITE);

   for (x1=0;x1<strlen(temp);x1++)
      txt[x1]=temp[strlen(temp)-x1-1];

   txt[x1]=NULL;
}

void loop(void)
{
   struct IntuiMessage *msg;
   int ile_quest=0,x1=0,ile_ok=0;
   int mode=REMEMBER;
   UBYTE temp[MAX_LINE_LENGHT];
   UBYTE answer[MAX_WORD_LENGHT]={NULL};
   UBYTE question[MAX_WORD_LENGHT]={NULL};
   UBYTE title[MAX_LINE_LENGHT];
   ULONG old_time;;

   SetWindowTitles(win,WINDOW_TXT2,SCREEN_TXT);

   get_question(question);
   old_time=clock();

   for (;;)
   {
      Wait((1<<win->UserPort->mp_SigBit));

      while (msg=(struct IntuiMessage *) GetMsg(win->UserPort))
      {
         ULONG clas=msg->Class;
         UWORD code=msg->Code;
         ReplyMsg((struct Message *) msg);

         if (clas==IDCMP_CLOSEWINDOW ||
            (clas==IDCMP_VANILLAKEY && code==CODE_ESC))
         {
            SetWindowTitles(win,WINDOW_TXT3,SCREEN_TXT);
            Delay(150);
            safe_exit();
         }
         else if (clas==IDCMP_INACTIVEWINDOW)
         {
            ActivateWindow(win);
            DisplayBeep(scr);
         }

         // * * *

         if (mode==REMEMBER)
         {
            if (clock()>=old_time+tooltypes.remembertime ||
               (clas==IDCMP_VANILLAKEY && code==CODE_ENTER))
            {
               mode=ANSWER;
               answer[0]=NULL;
               x1=new_intuitext(-1,-1,"answer: ",BLACK);
               old_time=clock();
            }
         }
         else if (mode==ANSWER)
         {
            if (clas==IDCMP_VANILLAKEY)
            {
               if (code==CODE_DEL && strlen(answer)>0)
                  sprintf(answer,"%.*s",strlen(answer)-1,answer);
               else if (!iscntrl(code) && strlen(answer)<MAX_WORD_LENGHT)
                  sprintf(answer,"%s%lc",answer,code);

               sprintf(temp,"%s_",answer);
               new_intuitext(x1,-1,temp,WHITE);
            }

            if (clock()>=old_time+tooltypes.answertime ||
               (clas==IDCMP_VANILLAKEY && code==CODE_ENTER))
            {
               if (strcmp(answer,question)==0)
               {
                  ile_ok++;
                  new_intuitext(-1,-1,"CORRECT! ",WHITE);
               }
               else
                  new_intuitext(new_intuitext(-1,-1,"WRONG! ",WHITE),-1,question,BLACK);

               Delay(50);

               ile_quest++;
               if (ile_quest>tooltypes.questions)
               {
                  sprintf(temp,"result: %d / %d",ile_ok,tooltypes.questions);
                  new_intuitext(-1,-1,temp,BLACK);
                  SetWindowTitles(win,WINDOW_TXT3,SCREEN_TXT);
                  Delay(100);
                  safe_exit();
               }

               mode=REMEMBER;
               get_question(question);

               sprintf(title,"question: %d/%d, result: %d/%d",ile_quest,tooltypes.questions,ile_ok,tooltypes.questions);
               SetWindowTitles(win,title,SCREEN_TXT);

               old_time=clock();
            }
         }
      }
   }
}
