#include "i_menu.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "doomdef.h"
#include "i_sound.h"
#include "m_argv.h"
#include "d_main.h"
#include "i_psp.h"
#include "PSP.h"


#define MAXDEPTH  16
#define MAXDIRNUM 1024
#define MAXPATH   0x108
#define PATHLIST_H 7
#define REPEAT_TIME 0x40000
#define BUFSIZE		65536

dirent_t		dlist[MAXDIRNUM];
int				dlist_num;

SceIoDirent		dirlist[MAXDIRNUM];

dirent_t		dlist2[MAXDIRNUM];
int				dlist_num2;

char			now_path[MAXPATH];
extern char		target[MAXPATH];
char			path_tmp[MAXPATH];
int				dlist_start;
int				dlist_curpos;
int				dlist_drawstart;
int				cbuf_start[MAXDEPTH];
int				cbuf_curpos[MAXDEPTH];
int				now_depth;

//txtfile reading
int				txtLoaded=0;
int				txtPos=0;
char			buf[BUFSIZE];

extern int loadextrawad=0;
extern int loaddehacked=0;
extern char *doomwaddir;
extern char extrawad[256];
extern char dehackedfile[256];
char doomwaddir2[256];


void Get_DirList(char *path) {
	int ret,fd;

	// Directory read
	fd = sceIoDopen(path);
	dlist_num = 0;
	ret = 1;
	while((ret>0) && (dlist_num<MAXDIRNUM)) {
		ret = sceIoDread(fd, &dlist[dlist_num]);
		if (dlist[dlist_num].name[0] == '.' && dlist[dlist_num].name[1] !='.')
			continue;
		if (ret>0)
			dlist_num++;
	}
	sceIoDclose(fd);

	if (dlist_start  >= dlist_num)
		dlist_start  = dlist_num-1;

	if (dlist_start  <  0)         
		dlist_start  = 0;

	if (dlist_curpos >= dlist_num)
		dlist_curpos = dlist_num-1;

	if (dlist_curpos <  0)
		dlist_curpos = 0;
}
void GetDirList(char* root)
{
	int dfd;
	int current=0;

//	sceIoMkdir(write_loc, 0777);

	dfd = sceIoDopen(root);
	if(dfd > 0)
	{
//		SceIoDirent dir;

		while(sceIoDread(dfd, &dirlist[current]) > 0)
		{
/*			if(dirlist[current].d_stat.st_attr & FIO_SO_IFDIR)
			{
				if(dirlist[current].d_name[0] != '.')
				{
					mh_print(100, current*15+30, dirlist[current].d_name, rgb2col(255,255,0), 0, 0);
				}
			}
			else
			{
				mh_print(100, current*15+30, dirlist[current].d_name, rgb2col(255,255,0), 0, 0);
				//finished
				//write_file(root, write_loc, dir.d_name);
			}*/
			current++;
		}
		sceIoDclose(dfd);
	}
}
void Read_Key2() {
	static int n=0;
	sceCtrlReadBufferPositive(&ctl, 1);

	if (ctl.Ly >= 0xD0) ctl.Buttons|=PSP_CTRL_DOWN;  // DOWN
	if (ctl.Ly <= 0x30) ctl.Buttons|=PSP_CTRL_UP;    // UP
	if (ctl.Lx <= 0x30) ctl.Buttons|=PSP_CTRL_LEFT;  // LEFT
	if (ctl.Lx >= 0xD0) ctl.Buttons|=PSP_CTRL_RIGHT; // RIGHT

	now_pad = ctl.Buttons;
	new_pad = now_pad & ~old_pad;
	if(old_pad==now_pad){
		n++;
		if(n>=25){
			new_pad=now_pad;
			n = 20;
		}
	}else{
		n=0;
		old_pad = now_pad;
	}
}

int Control_DirList(void) {

	int i;

	Read_Key2();
	pgWaitV();

	if (new_pad & PSP_CTRL_UP) {
		if (dlist_curpos > 0)
		{
			dlist_curpos--;
			if (dlist_curpos < dlist_start) 
				dlist_start = dlist_curpos;
		}
		txtLoaded=0;
	}
	if (new_pad & PSP_CTRL_DOWN) {
		if (dlist_curpos < (dlist_num-1))
		{
			dlist_curpos++;
			if (dlist_curpos >= (dlist_start+PATHLIST_H))
				dlist_start++;
		}
		txtLoaded=0;
	}
	if (new_pad & PSP_CTRL_LEFT) {
		dlist_curpos -= PATHLIST_H;
		if (dlist_curpos <  0)          { dlist_curpos = 0;           }
		if (dlist_curpos < dlist_start) { dlist_start = dlist_curpos; }
		txtLoaded=0;
	}
	if (new_pad & PSP_CTRL_RIGHT) {
		dlist_curpos += PATHLIST_H;
		if (dlist_curpos >= dlist_num) { dlist_curpos = dlist_num-1; }
		while (dlist_curpos >= (dlist_start+PATHLIST_H)) { dlist_start++; }
		txtLoaded=0;
	}

	if (new_pad & PSP_CTRL_CROSS) {
		txtLoaded=0;
		if (dlist[dlist_curpos].type & TYPE_DIR) 
		{
			if(dlist[dlist_curpos].name[0]=='.')
			{
				if (now_depth > 0) {
					for(i=0;i<MAXPATH;i++) {
						if (doomwaddir2[i]==0) break;
					}
					i--;
					while(i>4) {
						if (doomwaddir2[i-1]=='/') {
							doomwaddir2[i]=0;
							break;
						}
						i--;
					}
					now_depth--;
					dlist_start  = cbuf_start[now_depth];
					dlist_curpos = cbuf_curpos[now_depth];
					return 1;
				}
				return 3;
			}
			if (now_depth<MAXDEPTH) 
			{
				// path
				strcat(doomwaddir2,dlist[dlist_curpos].name);
				strcat(doomwaddir2,"/");
				cbuf_start[now_depth] = dlist_start;
				cbuf_curpos[now_depth] = dlist_curpos;
				dlist_start  = 0;
				dlist_curpos = 0;
				now_depth++;
				return 1;
			}
		} else {
			//
			strcpy(target,doomwaddir2);
			strcat(target,dlist[dlist_curpos].name);
			return 3;

		}
	}
	if (new_pad & PSP_CTRL_START)
	{
		return 2;
	}
	if (new_pad & PSP_CTRL_TRIANGLE) {
		txtLoaded=0;
		if (dlist[dlist_curpos].type & TYPE_FILE)
		{
			loadextrawad=1;
			strcpy(extrawad, doomwaddir2);
			strcat(extrawad, dlist[dlist_curpos].name);
		}
		return 3;
	}
	if (new_pad & PSP_CTRL_CIRCLE) {
		txtLoaded=0;
		if (now_depth > 0) {
			for(i=0;i<MAXPATH;i++) {
				if (doomwaddir2[i]==0) break;
			}
			i--;
			while(i>4) {
				if (doomwaddir2[i-1]=='/') {
					doomwaddir2[i]=0;
					break;
				}
				i--;
			}
			now_depth--;
			dlist_start  = cbuf_start[now_depth];
			dlist_curpos = cbuf_curpos[now_depth];
			return 1;
		}
	}
	if (new_pad & PSP_CTRL_SQUARE) {
		txtLoaded=0;
		if (dlist[dlist_curpos].type & TYPE_FILE)
		{
			loaddehacked=1;
			strcpy(dehackedfile, doomwaddir2);
			strcat(dehackedfile, dlist[dlist_curpos].name);
		}
		return 3;
	}
	if (new_pad & PSP_CTRL_LTRIGGER) {
		if(txtPos>=0)
			txtPos--;
		return 1;
	}
	if (new_pad & PSP_CTRL_RTRIGGER) {
		if(txtPos<600)
			txtPos++;
		return 1;
	}

	
	return 0;
}

void drawDirectory()
{
	int			i,col;
	pgFillvram(0);

	strncpy(path_tmp,doomwaddir2,40);

	mh_print(5,5,"Please choose a game to launch (Square for Dehacked file)",rgb2col(255,255,255),0,0);
	mh_print(5,15, "(Use Cross to select main WAD and Triangle to select (optional) PWAD, START to run game)",rgb2col(255,255,255),0,0);
	mh_print(320,5,"DoomPSP-0.04 S.S. b6 fixed",rgb2col(50,255,50),0,0);

	i = dlist_start;
	while (i<(dlist_start+PATHLIST_H))
	{
		if (i<dlist_num)
		{
			col = rgb2col(255,255,255);
			if (dlist[i].type & TYPE_DIR)
			{
				col = rgb2col(255,255,0);
			}
			if (i==dlist_curpos)
			{
				col = rgb2col(255,0,0);
			}
			strncpy(path_tmp,dlist[i].name,40);
			mh_print(32,((i-dlist_start)+2)*10+10,path_tmp,col,0,0);
		}
		i++;
	}
	if(dlist_start!=0)
		mh_print(120,30,"+",rgb2col(255,255,50),0,0);
	
	if(i!=dlist_num && dlist_num>=PATHLIST_H)
		mh_print(120,90,"+",rgb2col(255,255,50),0,0);

	displayFile();
	mh_print(200,40,"Main WAD:",rgb2col(50,255,50),0,0);
	mh_print(250,40,target,rgb2col(50,255,50),0,0);
	mh_print(200,50,"PWAD:",rgb2col(50,255,50),0,0);
	mh_print(230,50,extrawad,rgb2col(50,255,50),0,0);
	mh_print(200,60,"Dehacked file:",rgb2col(50,255,50),0,0);
	mh_print(280,60,dehackedfile,rgb2col(50,255,50),0,0);
	mh_print(30,24,"-------------------",rgb2col(255,255,255),0,0);
	mh_print(30,97,"------------------------------------------------------------------------------",rgb2col(255,255,255),0,0);
//	drawRectangle(30,25,19,8, rgb2col(255,255,255));
//	pgDrawRectangle(29,27,55,100, 0);
	pgScreenFlipV();
}/*
void drawRectangle(int x, int y, int stepsx, int stepsy, unsigned short col)
{
	int i=0;
	while(i<stepsx)
	{
		mh_print(x+i*10,y,"-",rgb2col(255,255,255),0,0);
		mh_print(x+i*10,stepsy*10+y,"-",rgb2col(255,255,255),0,0);
		i++;
	}

}*/
void displayFile()
{
	int foundTxt=0;
	int i=0;
	if(dlist[dlist_curpos].type & TYPE_FILE)
	{
		for(i=0;i<MAXPATH;i++) 
		{
			if (dlist[dlist_curpos].name[i]==0) 
				break;
		}
		i--;
		while(i>2) 
		{
			if (dlist[dlist_curpos].name[i-1]=='.') 
			{
				if(dlist[dlist_curpos].name[i]=='t' || dlist[dlist_curpos].name[i]=='T')
				{
					foundTxt=1;
				}
				break;
			}
			i--;
		}
	}
	if(foundTxt==1)
	{
		if(txtLoaded==0)
		{
			txtPos=0;
			strcpy(path_tmp,doomwaddir2);
			strcat(path_tmp,dlist[dlist_curpos].name);
			FILE* fp=fopen(path_tmp,"r");
			fseek(fp,SEEK_END, 0);
			int length=ftell(fp);
			fseek(fp, SEEK_SET, 0);
			i=0;
			while(!feof(fp) && i<600)
			{
				fgets(buf+100*i,100, fp);
				i++;
			}
			fclose(fp);
			txtLoaded=1;
		}
		int j=0;
		for(i=txtPos;i<txtPos+15;i++)
		{
			mh_print(30,110+j*10,buf+100*i,rgb2col(50,255,50),0,0);
			j++;
		}
		mh_print(70,260,"(Use Left trigger and Right trigger to scroll up and down in text)",rgb2col(255,255,255),0,0);
	}
}
void enterMenu(int		myargc,
			   char**	myargv )
{
	char *ni;
	doomwaddir = myargv[0];
	ni = strrchr(doomwaddir, '/');
	*++ni = 0;
	strcpy(doomwaddir2,doomwaddir);
	strcat(doomwaddir2,"WADS/");

	Get_DirList(doomwaddir2);
	dlist_start  = 0;
	dlist_curpos = 0;
	now_depth    = 0;
	
	while(1) {
		drawDirectory();
		
		switch(Control_DirList()) {
			case 1:
				Get_DirList(doomwaddir2);
				break;
			case 2:
				pgFillvram(0);
				pgScreenFlipV();
				pspDebugScreenInit();
				I_InitMusic();
				D_DoomMain (); 
				break;
			case 3:
				break;
		}
	}    

//	return 0;
}


