#include "ot_header.h"
#include "n64dd.h"
#include "ko_language.h"

#include "ot_course.h"
#include "sot_define.h"
#include "sot_pack.h"
#include "sot_disk.h"
#include "sot_diskproc.h"
#include "sot_draw.h"
#include "sot_nameinput.h"
#include "sot_buffer8m.h"
#include "sot_string.h"
#include "mo_diskproc.h"
#include "l_static.h"

extern void draw_file_window(Gfx **glistp, int x, int y, int wd, int ht); /* wd, ht は文字数 */

int selected_entry = 0;

Gfx *print_entry(Gfx *gp)
{
  int i;
  int x = 24+48*4-8, y = 56+16;
  //int x = 24+48*4 ,y = 20+32;
  int selectedNum = (cursol_y - 52) / 8;
  
  if(status2.editMode != EDIT_MODE_ENTRY) return (gp);
  
  gSPDisplayList(gp++, init_texture_rectangle_no_LUT);

  draw_file_window(&gp, x - 8, y - 8, 10+2, 6+2);
  for(i=0;i<6;i++){
	  gDPPipeSync(gp++);
	  gDPSetCombineMode(gp++, G_CC_PRIMITIVE, G_CC_PRIMITIVE);
	  if(i == selectedNum){ /* カーソルが指しているファイルは赤で */
		  gDPSetPrimColor(gp++, 0,0, 190,175,255, 255);
	  }else{
		  gDPSetPrimColor(gp++, 0,0, 64,64,64, 0); /* ふつうは灰色 */
	  }
	  gSPTextureRectangle(gp++,
						  (x-2) << 2,	(y+8*i) <<2,
						  (x+8*(INPUT_MAX+2)+2) << 2,	(y+8*i+8) << 2,
						  0, /* 採用するテクスチャの番号 */
						  0<<5, 0<<5,
						  1<<10,1<<10 /* テクスチャの拡大率 */
	  );
	  gDPPipeSync(gp++);
	  gDPSetCombineMode(gp++, G_CC_INT4, G_CC_INT4);
	  if(entry_course[i][0] == '\0'){
		  gDPSetPrimColor(gp++, 0,0, 0,255,0, 255);
		  if(i == selectedNum) gDPSetPrimColor(gp++, 0,0, 255,0,0, 255);
#if LANGUAGE_SW==LG_JPN
		  kana_mode = KANA_MODE_KATAKANA;
		  gp = PrintString_DD(gp, x, y+i*8, "%d: %c%c%c%c%c", i+1, 0x8E, 0xCC, 0xAB, 0xD9, 0xC4);/* デフォルトの表示 */
#else
#if (LANGUAGE_SW==LG_ENG || LANGUAGE_SW==LG_ZH )
		  gp = PrintString_DD(gp, x, y+i*8, "%d: DEFAULT", i+1);/* DEFAULT の表示 */
#endif
#endif		  
	  }else{
		  gDPSetPrimColor(gp++, 0,0, 255,255,255, 255);
		  if(i == selectedNum) gDPSetPrimColor(gp++, 0,0, 255,0,0, 255);
		  gp = PrintString_DD(gp, x, y+i*8, "%d:%s", i+1, (char *)entry_course[i]);
	  }
  }
  
  return (gp);
}

void cancel_entry(void)
{
  if( !(cont[cont_no[0]].trig & CONT_B) ) return;
  if(status2.editMode != EDIT_MODE_ENTRY) return;

  status2.editMode = EDIT_MODE_COURSE;
}

void cursol_move_for_entry(void)
{
  int x,y, f;
  
  x = cont[cont_no[0]].x;
  y = cont[cont_no[0]].y;
  
  f = x*x + y*y;
  
  if(f > 10*10){
	  cursol_x += x*0.15;
	  cursol_y += y*-0.15;
  }
  
  /* エントリーで動ける範囲内にカーソルを持ってくる */
  if(cursol_x < 24+48*4+8*7) cursol_x = 24+48*4+8*7;
  if(24+48*4+8*7 < cursol_x) cursol_x = 24+48*4+8*7;

  if(cursol_y < 52+4) cursol_y = 52+4;
  if(52+8*6-4 < cursol_y) cursol_y = 52+8*6-4;
}

void entry_select(void)
{
  if( !(cont[cont_no[0]].trig & CONT_A) ) return;
  if(status2.editMode != EDIT_MODE_ENTRY) return;

  selected_entry = (cursol_y - 52) / 8;
  file_access_mode = FILE_ACCESS_MODE_ENTRY;
#if 1
  thread_get_file_names(1, (u8 *)FZ64_64DD_FILETYPE);
#else
  get_file_names(1, (u8 *)FZ64_64DD_FILETYPE);
#endif  
  disk_access_mode = DISK_ACCESS_MODE_get_file_names;
}

/* 指定した名前がエントリされていたら消す */
void check_entry_and_delete_entry(char *ptr)
{
  int i;

  for(i=0;i<6;i++){
	  if(n64dd_strcmp(ptr, (char *)&entry_course[i][0]) == 0){
		  entry_course[i][0] = 0;
	  }
  }
}
