
#include "app.h"
#include "FileMenu.h"
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int gamesFound;
int scrollCount = 0;
class FileMenu FileMenu;
int selected = 1;
float scaleZ2 = 0;
FileMenu::FileMenu()
{
	memset(this,0,sizeof(*this));
}

int FileMenu::init()
{
	memset(this,0,sizeof(*this));
	strcpy(currentPath,HOME "roms");

	return 0;
}

int FileMenu::scan()
{
	char path[260];

	memset(path,0,sizeof(path));

	// Scan for all the roms in the current directory:
	nameReset();

	sprintf(path,"%.240s\\*", currentPath); nameFind(path);
	//sprintf(path,"%.240s\\*.zip", currentPath); nameFind(path);
	//sprintf(path,"%.240s\\*.sfc",currentPath); nameFind(path);

	return 0;
}

unsigned int color = 0xffffff;
void FileMenu::exit()
{
	free(nameList);
	memset(this,0,sizeof(*this));
}

int x2 = 0;
int y2 = 0;
int lastChoice = 0;
WCHAR previous[80]={0};
int FileMenu::render()
{
	int x=0,y=0;
	int pos=0,index=0;
	WCHAR text[64];
	int height=18;

	memset(text,0,sizeof(text));

	x=45; y=80;


	x2 = (choiceFocus*height)>>8;
	if(x2 != 0 && menuChanged && x2 != y2) {
		if(x2 < y2) {
			y2 = x2;
			selected--;
		}
		else {
			y2 = x2;
			selected++;
		}
	}


	//selected = (choiceFocus*height)>>8;
	//if((scrollCount > (10)))
	if((selected < gamesFound-15) && (gamesFound > 17)) {
	  y-=(choiceFocus*height)>>8;
	  lastChoice = y;
	}
	else {
	  if (gamesFound <= 17) y = 80; else y = lastChoice;
	}

	//if( y > 


	while (pos<nameSize)
	{
		char *name=NULL;
		//char *name2=NULL;
		name=nameList+pos;
		//name2=nameList+pos;

		//int t = strlen(name2);
		////name2[t-1] = 'g';
		//name2[t-2] = 'n';
		//name2[t-3] = 'p';
		//D3DXCreateTextureFromFile(Device, name2, &TexPreview );
		//WCHAR text23[80]={0};
		//wsprintfW(text23,L"Games: %s",name2);
		//FontSetColour(0x60c0ff);
		//FontText(text23,495,225);

		if (y>=80 && y<380)
		{
			
            unsigned int colour=0xffffff;
			// If this line is visible:
			wsprintfW(text,L"%.64S",name);
			if (index==(choiceFocus>>8)) {
				colour=0xffff00;
				WCHAR text23[80]={0};
				//wsprintfW(text23,L"Games: %s",text);
				//int t = strlen(text);
                 int z = 0;
			    //for(z = 0; z < 80; z++) previous[z] = '\0';
				for(z = 0; z < 80; z++) { 
					if (name[z] == '\0') {
					    text23[z] = name[z];
						//previous[z] = name[z];
						break;
					}
					text23[z] = name[z];
				}




				if(menuChanged && fastScroll == false) {
					menuChanged = false;
					for(int i = 0; i < 80; i++) {
						if(text23[i] == '.' && text23[i+1] == 'z' && text23[i+2] == 'i' && text23[i+3] == 'p') {
							text23[i+1] = 'p';
							text23[i+2] = 'n';
							text23[i+3] = 'g';
							//if(TexPreview) TexPreview->Release;

							int a = z + 11 + 3;
							char* w = new char[a];

							w[0]= 'D';
							w[1]= ':';
							w[2]= '\\';
							//w[3]= '\\';
							w[3]= 'p';
							w[4]= 'r';
							w[5]= 'e';
							w[6]= 'v';
							w[7]= 'i';
							w[8]= 'e';
							w[9]= 'w';
							w[10]= 's';
							w[11]= '\\';
							w[12]= '\\';
							w[a-1]= '\0';
							for(int q = 0; q < z; q++) {
								w[q+13] = (char)text23[q];
					        }
						
						//	printf("%s\n", w);
                        

						
	                    //wsprintfW(text23L, "Start Game");
	                    //FontSetColour(0xffff00);
	                    //FontText(text23,295,325, 600);

					 
					  if(TexPreview) {
							TexPreview->Release();
							TexPreview = NULL;
					  }
					  if( FAILED( D3DXCreateTextureFromFile(Device, w, &TexPreview ) ) )
					  {
						  useNoRom = true;
						  //D3DXCreateTextureFromFile(Device, "snes.jpg", &TexPreview );
					  }
					  else useNoRom = false;
					  //delete w;

					  break;
				  }
			  }
				}
				
				//text23[t - 1] = 'A';
				

			}

			FontSetColour(colour);
			FontText(text,x+30+15,y+20, 205);//was 195


		}

		
		//if(selected < gamesFound) {
			y+=height;
			pos+=strlen(name)+1; // Skip to next string
		    index++;
		//}
	}

	return 0;
}

int FileMenu::scroll(int amount)
{
	int max=0;


	choiceFocus+=amount;

	max=(nameCount-1)<<8;
	
	if (choiceFocus<0) choiceFocus=0;
	if (choiceFocus>=max) choiceFocus=max;

	return 0;
}

// Get the currently highlighted filename
int FileMenu::getFilePath(char *path)
{
	int focus=0;
	int pos=0;
	char *name=NULL;

	// Find where the user is focused
	focus=choiceFocus>>8;
	pos=nameOffset(focus); if (pos<0) return 1;

	name=nameList+pos;

	// Return path and name:
	sprintf(path,"%.128s\\%.128s",currentPath,name);
	return 0;
}

// ----------------------------------------------------------------------
int FileMenu::nameReset()
{
	free(nameList); nameList=NULL;
	nameSize=nameMax=nameCount=0;

	return 0;
}

int FileMenu::nameFind(char *path)
{
	gamesFound=0;
	HANDLE find=NULL;
	WIN32_FIND_DATA wfd;

	memset(&wfd,0,sizeof(wfd));

	find=FindFirstFile(path,&wfd);
	if (find==INVALID_HANDLE_VALUE) return 1;

	std::string t1;
	vector<string> t;
	for (;;)
	{
		if(wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
		   std::string t1(wfd.cFileName);
		   t.push_back(t1);
		   gamesFound++;
		}
		if (FindNextFile(find,&wfd)==0) break;
	}

    sort(t.begin(), t.end());
	FindClose(find);
    for(int i = 0; i < t.size(); i++) nameAdd((char*)(t[i].c_str()));


	
	return 0;
}

int FileMenu::nameAdd(char *entry)
{
	int len=0;

	len=strlen(entry);
	// Check we have room for this entry:
	if (nameSize+len+1>nameMax) nameSizeUp();
	if (nameSize+len+1>nameMax) return 1;

	// Add entry with zero at the end:
	memcpy(nameList+nameSize,entry,len);
	nameSize+=len+1;
	nameCount++;

	return 0;
}

int FileMenu::nameSizeUp()
{

	void *mem=NULL;
	int add=256;

	// Allocate more memory for the list:
	mem=realloc(nameList,nameMax+add); if (mem==NULL) return 1;

	nameList=(char *)mem;
	memset(nameList+nameMax,0,add); // Blank new memory
	nameMax+=add;
	return 0;
}

int FileMenu::nameOffset(int index)
{
	int pos=0,i=0;

	while (pos<nameSize)
	{
		char *name=nameList+pos;

		if (i==index) return pos;

		pos+=strlen(name)+1; // Skip to next string
		i++;
	}

	return -1; // Unknown index
}
