#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 "osd.h"
#include "thread.h"
#include "plugin.h"


#include "SDL.h"

#include "cGngbOsd.h"
#include "GngbMenu.h"

DIR *infodir;
struct dirent *dp;

#define NBMAXROMS	90

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;

	infodir = opendir(dir);
    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);

}


void DisplayMenu(cGngbOsd *pGngbOsd)
{
	int y,i;
    int yHautList,yBasList;
    int yHautBar,yLngBar;

	y = 0;

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

    yHautList=y;

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

		y += pGngbOsd->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;

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

	pGngbOsd->m_pOsd->Flush();
}

char *ChooseRomMenu(cGngbOsd *pGngbOsd)
{
	OffsetMenu = 0;
	PosInMenu = 0;
	Selected = -1;
	redraw = 1;

	VDRGNGBLOG("ChooseRomMenu\n");

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

	while( Selected == -1 )
	{
		if( redraw == 1 )
		{
			redraw = 0;
			DisplayMenu(pGngbOsd);
		}
		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 );
}