#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <linux/joystick.h>

#include <pthread.h>
#include <semaphore.h>
#include   <ctype.h>
#include "osd.h"
#include "thread.h"
#include "plugin.h"


#include "SDL/SDL.h"

#include "gbcosd.h"
#include "gbcmenu.h"

DIR *infodir;
struct dirent *dp;

#define NBMAXROMS	900

int nbRoms;
char RomsName[900][70];

char theRomsName[900];

int PosInMenu;
int OffsetMenu;
int Selected;
int redraw;

int LastDiplay;
int maxDisplay;

void ReadRomsInDirectory(char *dir)
{
  char *ptr;
  char car;
  nbRoms = 0;
  int i,j;
  unsigned int k;
  char nmc[100];
  char nmil[100];
  char nmjl[100];

  infodir = opendir(dir);
  if( infodir != NULL )
  {
      dp = readdir (infodir);
      while( dp !=  NULL )
      {
        RomsName[ nbRoms ][60] = 0;
        RomsName[ nbRoms ][61] = 0;
        RomsName[ nbRoms ][62] = 0;
        if( nbRoms < NBMAXROMS )
        {
          ptr = strrchr( dp->d_name , '.' );

          if( ptr != NULL )
          {
            ptr++;
            car = *ptr;
            if( car == 'g' || car == 'G' )
            {
              strncpy( RomsName[ nbRoms ] , dp->d_name , 60);
              nbRoms++;
            }
          }
        }
        dp = readdir (infodir);
      }

      closedir(infodir);

      VDRGBCLOG("Sorting ROMS (");

      /* Sort by names */
      for(i=0;i<nbRoms;i++)
      {
        for(j=0;j<nbRoms;j++)
        {
          strcpy( nmil , RomsName[i] );
          strcpy( nmjl , RomsName[j] );
          for(k=0;k<strlen(nmil);k++) nmil[k] = tolower( nmil[k] );
          for(k=0;k<strlen(nmjl);k++) nmjl[k] = tolower( nmjl[k] );
          if( strcmp( nmil , nmjl ) < 0 )
          {
            strcpy( nmc , RomsName[j] );
            strcpy( RomsName[j] , RomsName[i] );
            strcpy( RomsName[i] , nmc );
          }
        }
      }
      VDRGBCLOG(" )\n");

  }
  else
  {
        VDRGBCLOG("unable to open directory\n");
  }
}


void DisplayMenu(cgbcOsd *pgbcOsd)
{
  int y,i;
  int yHautList,yBasList;
  int yHautBar,yLngBar;

  y = 0;

  pgbcOsd->m_pOsd->DrawRectangle(0,0, SIZEOSDX, SIZEOSDY, clrBlack);
  pgbcOsd->m_pOsd->DrawText( SIZEOSDX/2 , 0, "gbc" , clrYellow, clrGray50, pgbcOsd->m_Font,0,0,taCenter|taTop);
  y += 1.2*pgbcOsd->m_Font->Height();

  yHautList=y;

  i = OffsetMenu;
  while( (y+pgbcOsd->m_Font->Height()) < SIZEOSDY && i < nbRoms )
  {
    if( i == PosInMenu )
      pgbcOsd->m_pOsd->DrawText( 10 , y, RomsName[i] , clrBlack, clrGreen, pgbcOsd->m_Font);
    else
      pgbcOsd->m_pOsd->DrawText( 10 , y, RomsName[i] , clrWhite, clrGray50, pgbcOsd->m_Font);

    y += pgbcOsd->m_Font->Height();
    i++;
  }
  LastDiplay = i;
  yBasList = y;

  maxDisplay = LastDiplay - OffsetMenu;

  /*
  OffsetMenu       0                         0
                nbRoms - maxDisplay       (yBasList-yLngBar)-yHautList
  */

  if( nbRoms != 0 )
    yLngBar = (( yBasList-yHautList ) * maxDisplay)/nbRoms ;
  else
    yLngBar = yBasList-yHautList;

  if( (nbRoms - maxDisplay) != 0 )
    yHautBar = yHautList + (((yBasList-yLngBar)-yHautList)*OffsetMenu)/(nbRoms - maxDisplay);
  else
    yHautBar = yHautList;

  pgbcOsd->m_pOsd->DrawRectangle(SIZEOSDX -4 ,yHautList, SIZEOSDX, yBasList, 0xffD5FFD5);
  pgbcOsd->m_pOsd->DrawRectangle(SIZEOSDX -4 ,yHautBar, SIZEOSDX, yHautBar+yLngBar, clrGreen);

  pgbcOsd->m_pOsd->Flush();
}

char *ChooseRomMenu(cgbcOsd *pgbcOsd)
{
  OffsetMenu = 0;
  PosInMenu = 0;
  Selected = -1;
  redraw = 1;

  VDRGBCLOG("ReadRomsInDirectory\n");
  ReadRomsInDirectory( "/data/games/GameBoy" );


  while( Selected == -1 )
  {
    if( redraw == 1 )
    {
      redraw = 0;
      DisplayMenu(pgbcOsd);
    }
    usleep(50000);
  }

  strcpy( theRomsName , "/data/games/GameBoy/" );
  strcat( theRomsName , RomsName[ Selected ] );

  return( theRomsName );
}

eOSState ProcessKeyMenuMode(eKeys keys)
{
  eOSState state;
  state = osContinue;

  switch (keys)
  {
  case kUp:
    if( PosInMenu > 0 )
      PosInMenu -- ;
    break;

  case kDown:
    if( PosInMenu < nbRoms )
      PosInMenu ++ ;
    break;

  case kRight:
    PosInMenu += ( LastDiplay - OffsetMenu )-1 ;
    break;

  case kLeft:
    PosInMenu -= ( LastDiplay - OffsetMenu ) ;
    break;


  case kOk:
    Selected = PosInMenu;
    break;

  default:
    break;
  }


  if( PosInMenu >= LastDiplay )
  {
    //        if( PosInMenu <= nbRoms - ( LastDiplay - OffsetMenu ) )
    OffsetMenu = (PosInMenu - ( LastDiplay - OffsetMenu )) + 1 ;
  }

  if( PosInMenu <= OffsetMenu )
  {
    OffsetMenu = PosInMenu ;

    //			OffsetMenu --;
  }

  if( PosInMenu >= nbRoms )
    PosInMenu = nbRoms-1 ;
  if( PosInMenu < 0 )
    PosInMenu = 0 ;

  if( OffsetMenu < 0 )
    OffsetMenu = 0;
  if( OffsetMenu > nbRoms - maxDisplay )
    OffsetMenu = nbRoms - maxDisplay;

  redraw = 1;

  return( state );
}
