#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <io.h>
#include <dos.h>
#include <stos.h>

#define MAX_ROMS 4000
#define ROMS_PER_COLUMN 22
#define NUM_COLS 6

//----------------------------------------------------------------------------

void redrawScreen(char romnames[MAX_ROMS][9], int romcount, int romdisplayindex, int romselectionindex)
{
   int row,col,temp;
   int barstart,barend;
   int i;

   //---------
   //rom names
   //---------

   for (col=0 ; col<NUM_COLS ; col++)
   {
      for (row=0 ; row<ROMS_PER_COLUMN ; row++)
      {
         temp = (romdisplayindex + col)*ROMS_PER_COLUMN + row;         
         
         gotoxy(1+col*11,1+row);
        
         textattr(temp==romselectionindex ? 0x70 : 0x07);         
         
         if (temp>=romcount)
            cprintf("        ");
         else
            cprintf("%s%s",romnames[temp],&"        \0"[strlen(romnames[temp])]);
      }
   }

   //---------
   //scrollbar
   //---------

   textattr(0x3f);
   gotoxy(64,23);
   cprintf("%c",0x1A);
   gotoxy(1,23);
   cprintf("%c",0x1B);

   barstart = 62*min(romdisplayindex*ROMS_PER_COLUMN,romcount)/romcount;
   barend = 62*min((romdisplayindex+NUM_COLS)*ROMS_PER_COLUMN,romcount)/romcount;

   textattr(0x30);
   for (i=0 ; i<barstart ; i++)
      cprintf("%c",0xb1);

   textattr(0x3f);
   for (i=barstart ; i<barend ; i++)
      cprintf("%c",0xb1);

   textattr(0x30);
   for (i=barend ; i<62 ; i++)
      cprintf("%c",0xb1);

   textattr(0x07);
}

//-------------------------------------------------------------------------------------------

void drawScreen(char romnames[MAX_ROMS][9], int romcount, int romdisplayindex, int romselectionindex)
{
   int i;

   //----------------
   //clear the screen
   //----------------

   textattr(0x0F);
   clrscr();

   //-------
   //borders
   //-------

   textattr(0x4E);

   for (i=1 ; i<24 ; i++)
   {
      gotoxy(65, i);
      cprintf(" ");
   }

   gotoxy(1, 24);
   cprintf(" Selected ROM:                                                                  ");

   redrawScreen(romnames, romcount, romdisplayindex, romselectionindex);
}

//-------------------------------------------------------------------------------------------
// Dumb bubblesort of roms

void sortRoms(char romnames[][9], int romcount)
{
   int i,j;
   char temp[9];

   for (i=0 ; i<romcount ; i++)
   {
      for (j=1 ; j<romcount-i ; j++)
      {
         if (strcasecmp(romnames[j-1],romnames[j])>0)
         {
            strcpy(temp,romnames[j-1]);
            strcpy(romnames[j-1],romnames[j]);
            strcpy(romnames[j],temp);
         }
      }
   }
}

//-------------------------------------------------------------------------------------------
// Main entry point. There's no commandline args used

int main(int argc, char *argv[])
{
   //The resultant commandline
   char *cmdline;

   //The names and number of all available roms
   char romnames[MAX_ROMS][9];
   int romcount=0;

   //Stuff for the ui
   int romdisplayindex=0;
   int romselectionindex=0;
   char selectedrom[9]={0,0,0,0,0,0,0,0,0};

   //Temp vars
   struct find_t find_t;
   FILE *f;
   int i,j;

   initialise_joysticks();

   //Read in the current rom name...
   f=fopen("bootmame.cfg","r");

   if (f!=NULL)
   {
      fread(selectedrom, 1, 8, f);
      fclose(f);
   }

   read_joystick();

   //Launch the set up if joy buttons A&B are being held down
   if (strlen(selectedrom)==0 || (joyA.B1 && joyA.B2))
   {
      //Get the rom names...

      for (i=_dos_findfirst("roms\\*.zip", _A_NORMAL, &find_t) ; !i && romcount<MAX_ROMS ; i=_dos_findnext(&find_t))
      {
         //only interested in the bit before the '.'

         for (j=0 ; j<strlen(find_t.name) ; j++)
         {
            if (j==8 || find_t.name[j]=='.')
            {
               romnames[romcount][j]=0;
               break;
            }
            else
               romnames[romcount][j]=find_t.name[j];
         }

         romcount++;
      }

      sortRoms(romnames, romcount);

      //Set the selected index

      if (strlen(selectedrom)>0)
      {
         for (i=0 ; i<romcount ; i++)
         {
            if (!strcasecmp(selectedrom, romnames[i]))
            {
               romselectionindex=i;
               romdisplayindex = i/ROMS_PER_COLUMN;
               break;
            }
         }
      }

      initSTOS();
      _setcursortype(_NOCURSOR);
      drawScreen(romnames, romcount, romdisplayindex, romselectionindex);

      //--------------
      //main menu loop
      //--------------

      while (!joyB.B1)
      {
         read_joystick();

         if (joyA.up)
            romselectionindex = max(romselectionindex-1,0);
         if (joyA.left)
            romselectionindex = max(romselectionindex-ROMS_PER_COLUMN,0);
         if (joyA.down)
            romselectionindex = min(romselectionindex+1,romcount-1);
         if (joyA.right)
            romselectionindex = min(romselectionindex+ROMS_PER_COLUMN,romcount-1);

         if (joyA.up||joyA.down||joyA.left||joyA.right)
         {
            while (romdisplayindex*ROMS_PER_COLUMN>romselectionindex)
               romdisplayindex--;

            while ((romdisplayindex+NUM_COLS)*ROMS_PER_COLUMN<romselectionindex)
               romdisplayindex++;
            
            redrawScreen(romnames, romcount, romdisplayindex, romselectionindex);
         }

         delay(50);
      }

      strcpy(selectedrom,romnames[romselectionindex]);

      closeSTOS();
      textattr(0x07);
      clrscr();

      _setcursortype(_NORMALCURSOR);

      //------------------------------
      //Write out the current rom name
      //------------------------------

      f=fopen("bootmame.cfg","w");

      if (f!=NULL)
      {
         fwrite(selectedrom, 1, 8, f);
         fclose(f);
      }
   }

   //Find out how long a buffer to malloc...
   j = strlen(selectedrom) + 6; //"mame romname" + null terminator

   for (i=1 ; i<argc ; i++)
      j+=strlen(argv[i])+1;

   cmdline = (char *)malloc(j);

   //Now build the commandline...
   sprintf(cmdline, "mame %s",selectedrom);

   for (i=1 ; i<argc ; i++)
   {
      strcat(cmdline," ");
      strcat(cmdline,argv[i]);
   }

   //finally, run mame...
   //printf("running mame...\n --> %s\n",cmdline);
   system(cmdline);
}

