#include "WBTRIS.h"

struct HiscorePart {
   int  Position;
   char Name[40];
   int  Score;
   int  Rows;
};

struct HiscorePart Hiscore[10];
extern struct TextAttr topaz8;

void HiscoreList(char *Name, int Score, int Rows, int XOffset, int YOffset)
{
   struct RastPort *rp;
   struct Window *window;
   extern struct Screen *myscreen;

   if (window = OpenWindowTags(NULL,
                                WA_Left,       XOffset,
                                WA_Top,        YOffset+(myscreen->Font->ta_YSize)+3,
                                WA_Width,      MY_WIN_WIDTH,
                                WA_Height,     MY_WIN_HEIGHT,
                                WA_Title,      "<-- Click to close!",
                                WA_Flags,      WFLG_CLOSEGADGET | WFLG_ACTIVATE | WFLG_DRAGBAR | WFLG_DEPTHGADGET,
                                WA_IDCMP,      IDCMP_CLOSEWINDOW,
                                TAG_END))
      {
      rp = window->RPort;

      LoadFile();
      UpdateHiscore(Name, Score, Rows);
      OutHiscoreList(rp);
      if (SaveFile() == FALSE)
         CloseWindow(window);

      WaitPort(window->UserPort);
      CloseWindow(window);
   }
}



/*
** Ausgabe der Liste ins Fenster
*/
void OutHiscoreList(struct RastPort *rp)
{
   int i;
   char s[80];
   struct IntuiText Zeile;

   Zeile.FrontPen = 1;
   Zeile.BackPen = 0;
   Zeile.DrawMode = JAM2;
   Zeile.LeftEdge = 0;
   Zeile.TopEdge = 0;
   Zeile.ITextFont = &topaz8;
   Zeile.NextText = NULL;

   strcpy(s, "Pos. Name                    Score Rows");
   Zeile.IText = s;
   PrintIText(rp, &Zeile, 12, 18);

   SetAPen(rp, 1);
   Move(rp, 10, 30);
   Draw(rp, MY_WIN_WIDTH - 10, 30);

   for (i=0; i<10; i++) {
      sprintf(s, "%2d   %-23s%6d%5d", i+1, Hiscore[i].Name, Hiscore[i].Score, Hiscore[i].Rows);
      Zeile.IText = s;
      PrintIText(rp, &Zeile, 12, 36+11*i);
   }
}



/*
** Sortiert Liste neu
*/
void UpdateHiscore(char *Name, int Score, int Rows)
{
   int i = 0;
   int j;
   BOOL equal = FALSE;

   while ((equal == FALSE) && (Score <= Hiscore[i].Score) && (i <= 9)) {
      if (Score == Hiscore[i].Score)
         equal = TRUE;
      else
         i++;
   }

   if (equal == TRUE) {
      while ((Score == Hiscore[i].Score) && (Rows <= Hiscore[i].Rows) && (i<=9))
         i++;
   }

   for (j = 9; j > i; j--) {
      Hiscore[j].Score = Hiscore[j-1].Score;
      strcpy(Hiscore[j].Name, Hiscore[j-1].Name);
      Hiscore[j].Rows  = Hiscore[j-1].Rows;
   }

   Hiscore[i].Score = Score;
   strcpy(Hiscore[i].Name, Name);
   Hiscore[i].Rows  = Rows;
}



/*
** Speichert das Hiscorefile ab
*/
BOOL SaveFile(void)
{
   int i;
   FILE *fp;
   char FName[80];

   if (getenv("WBTRIS"))
      strcpy(FName, getenv("WBTRIS"));
   else
      strcpy(FName, FILENAME);

   if ((fp = fopen(FName, "w")) == NULL) {
      fprintf(stderr, "Couldn't open file '%s'.\n", FName);
      return(FALSE);
   }
   for (i=0; i<10; i++) {
      if (strlen(Hiscore[i].Name) == 0)
         strcpy(Hiscore[i].Name, "Nobody");
      fprintf(fp, "%d/%s/%d/%d\n", i+1, Hiscore[i].Name, Hiscore[i].Score, Hiscore[i].Rows);
   }
   fclose(fp);
   return(TRUE);
}



/*
** Laedt das Hiscorefile ein
*/
void LoadFile(void)
{
   int i;
   FILE *fp;
   int c;
   char text[30];
   char *ptr;
   char FName[80];

   if (getenv("WBTRIS"))
      strcpy(FName, getenv("WBTRIS"));
   else
      strcpy(FName, FILENAME);

   if ((fp = fopen(FName, "r")) == NULL) {
      for (i=0; i<10; i++) {
         Hiscore[i].Position = i+1;
         strcpy(Hiscore[i].Name, "...");
         Hiscore[i].Score = 0;
         Hiscore[i].Rows = 0;
      }
   } else {
      i = 0;
      while ((c = fgetc(fp)) != EOF) {
         while (c != '/') {
            c = fgetc(fp);
         }
         Hiscore[i].Position = i+1;

         ptr = &text[0];
         *ptr = '\0';
         while ((c = fgetc(fp)) != '/') {
            *ptr = c;
            ptr++;
         }
         *ptr = '\0';
         strcpy(Hiscore[i].Name, text);

         ptr = &text[0];
         *ptr = '\0';
         while ((c = fgetc(fp)) != '/') {
            *ptr = c;
            ptr++;
         }
         *ptr = '\0';
         Hiscore[i].Score = atoi(text);

         ptr = &text[0];
         *ptr = '\0';
         while ((c = fgetc(fp)) != '\n') {
            *ptr = c;
            ptr++;
         }
         *ptr = '\0';
         Hiscore[i].Rows = atoi(text);
         i++;
      }
      fclose(fp);
   }
}
