#include "main.h"
#include "setup.h"
#include "ex.h"
#include "pixmap.h"
#include "timer.h"
#include "title.h"

/* 新しく ranking file を作る */
void mk_best()
{
  FILE *fp;
  int i,j;

  printf("creating ranking file...");

  for(i=0;i<20;i++)
  {
    best[i].score =(20-i)*50000;
    best[i].line  =(20-i)*5;
    best[i].cosmo =(20-i)*10;
    best[i].level =(20-i)*5;
    best[i].slevel=0;
    best[i].uid   =0;
    for(j=0;j<8;j++) best[i].name[j]='A'+i;
    best[i].name[j]='\0';
  }

  if( (fp=fopen(RANK_FILE,"w"))==NULL )
  {
    printf("NG\n");
    return;
  }

  for(i=0;i<20;i++) fwrite(&best[i],sizeof(UserStatus),1,fp);
  fclose(fp);
  printf("OK\n");
}

/* ranking data を読み込む */
void get_best()
{
  int i;
  FILE *fp;

  if( (fp=fopen(RANK_FILE,"r")) == NULL )
  {
    fprintf(stderr,"%s: cannot open ranking data file!\n",av[0]);
    mk_best();
  }
  else
  {
    for(i=0;i<20;i++)
      if(fread(&best[i],sizeof(UserStatus),1,fp)==0) break;
    fclose(fp);
    if(i<20) mk_best(); /* ranking file が異常 */
  }
}

/* best20 に入ったら ranking data を更新 */
void ranking()
{
  FILE *fp;
  int i,j,k;

  get_best();

  for(i=0;i<20;i++)
  {
    if(user.score> best[i].score) break;
    if(user.score==best[i].score)
    {
      /* score が同じ */
      if(user.level> best[i].level) break;
      if(user.level==best[i].level)
      {
	/* level も同じ */
	if(user.cosmo> best[i].cosmo) break;
	if(user.cosmo==best[i].cosmo)
	{
	  /* cosmo も同じ */
	  if(user.line> best[i].line) break;
	    /* line も同じ、もう知らん ^^; */
	}
      }
    }
  }

  /* 今回より上位に ranking されてるか？ */
  for(j=0;j<i;j++)
    if(!strcmp(user.name,best[j].name)) { i=20; break; }

  /* 今回より下位に ranking されてるか？ */
  for(k=i;k<19;k++)
    if(!strcmp(user.name,best[k].name)) break;

  if(i<20)
  {
    for(j=k;j>i;j--)
      memcpy(&best[j],&best[j-1],sizeof(UserStatus));
    memcpy(&best[i],&user,sizeof(UserStatus));
  }

  if( (fp=fopen(RANK_FILE,"w"))==NULL )
  {
    printf("cannot renew score file...\n");
    return;
  }

  for(i=0;i<20;i++) fwrite(&best[i],sizeof(UserStatus),1,fp);
  fclose(fp);

  get_best();
}

/* レベルセレクト */
signed int level_select()
{
  signed int level=0;
  int tmpid;

  mk_game_window();

  set_font(FNT24);
  set_col(White);
  XDrawString(disp,mwin,gc,
	      SIZE*3-x_get_word_width(FNT24,"LEVEL SELECT")/2,
	      SIZE*5-SIZE/2-6,"LEVEL SELECT",12);

  set_font(FNT14);
  set_col(Easy);
  XDrawString(disp,mwin,gc,SIZE+4  ,SIZE* 7-SIZE/2-10,"EASY",4);
  XDrawString(disp,mwin,gc,4*SIZE+4,SIZE* 7-SIZE/2-10,"LV.1",4);
  set_col(Normal);
  XDrawString(disp,mwin,gc,SIZE+4  ,SIZE* 8-SIZE/2-10,"NORMAL",6);
  XDrawString(disp,mwin,gc,4*SIZE+4,SIZE* 8-SIZE/2-10,"LV.10",5);
  set_col(Hard);
  XDrawString(disp,mwin,gc,SIZE+4  ,SIZE* 9-SIZE/2-10,"HARD",4);
  XDrawString(disp,mwin,gc,4*SIZE+4,SIZE* 9-SIZE/2-10,"LV.40",5);
  set_col(Expert);
  XDrawString(disp,mwin,gc,SIZE+4  ,SIZE*10-SIZE/2-10,"EXPERT",6);
  XDrawString(disp,mwin,gc,4*SIZE+4,SIZE*10-SIZE/2-10,"LV.100",6);
  set_col(Quit);
  XDrawString(disp,mwin,gc,SIZE+4   ,SIZE*11-SIZE/2-10,"QUIT",4);

  tmpid=put_sp(mwin,COSMO,0,(6+level)*SIZE-SIZE/2,0);
  minx=miny=0;
  maxx=w2attr.width; maxy=w2attr.height;
  x_flush();

  for(;;)
  {
    get_event(1);
    if(event.type!=KeyPress) continue;
    return_back(tmpid);

    if(key==keys.up    ) level--;
    if(key==keys.down  ) level++;
    if(key==keys.quit  ) return QUIT;
    if(key==keys.decide) break;

    if(level==-1) level=4;
    if(level== 5) level=0;

    tmpid=put_sp(mwin,COSMO,0,(6+level)*SIZE-SIZE/2,0);
    x_flush();
  }

  switch(level)
  {
    case 0: return 1;
    case 1: return 10;
    case 2: return 50;
    case 3: return 100;
    case 4: return QUIT;
  }

  return 0;
}

static char rk[20][5]= {
  " 1st"," 2nd"," 3rd"," 4th"," 5th"," 6th"," 7th"," 8th"," 9th","10th",
  "11th","12th","13th","14th","15th","16th","17th","18th","19th","20th"
};

static int rankcol[20] = {
  Blue15,Blue14,Blue13,Blue12,Blue11,Blue10,Blue9,Blue8,Blue7,Blue6,
  Blue7,Blue8,Blue9,Blue10,Blue11,Blue12,Blue13,Blue14,Blue15,Blue16
};

static char *itoa(int n,int m)
{
  static char num[10];
  int i;

  num[m]='\0'; m--;

  for(i=m;i>=0;i--)
  {
    num[i]=n%10+'0';
    n/=10;
  }

  return num;
}

/* タイトル */
signed int title()
{
  Window win;
  Pixmap rankm,rankc;
  int gx,nx,sx,cx,lx,lvx;
  int dy;
  signed int ty,by;
  int ph,py,sf=0;
  int i;

  XUnmapWindow(disp,w2attr.win);
  XUnmapWindow(disp,nextattr.win);
  XUnmapWindow(disp,sgwattr.win);
  XUnmapWindow(disp,scrwattr.win);
  XUnmapWindow(disp,csmwattr.win);
  XUnmapWindow(disp,linwattr.win);
  XUnmapWindow(disp,levwattr.win);
  while(get_event(0));

  win=w1attr.pix;
  rankc=XCreatePixmap(disp,win,351,24*20,XDefaultDepth(disp,0));
  rankm=XCreatePixmap(disp,win,351,24*20,XDefaultDepth(disp,0));
  XSetForeground(disp,gc,0);
  XFillRectangle(disp,rankc,gc,0,0,351,24*20);
  XSetForeground(disp,gc,0xffffffff);
  XFillRectangle(disp,rankm,gc,0,0,351,24*20);

  get_best();
  init_tmp();

  XCopyArea(disp,background,w1attr.pix,gc,0,0,w1attr.width,w1attr.height,0,0);

  set_font(FNT24);
  set_col(Title);
  XDrawString(disp,win,gc,
	      (w1attr.width-x_get_word_width(FNT24,TITLE))/2,48,
	      TITLE,strlen(TITLE));
  set_font(FNT14);
  set_col(Ver);
  XDrawString(disp,win,gc,
	      (w1attr.width-x_get_word_width(FNT14,"VERSION 2"))/2,68,
	      "VERSION 2",9);

  XDrawString(disp,win,gc,
	      (w1attr.width-x_get_word_width(FNT14,"PUSH ANY KEY"))/2,394,
	      "PUSH ANY KEY",12);

  set_font(FNT10);
  XDrawString(disp,win,gc,
	      (w1attr.width-x_get_word_width(FNT10,PRODUCE))/2,432,
	      PRODUCE,strlen(PRODUCE));

  set_font(FNT14);
  set_col(White);

  gx=0; nx=45; sx=126; cx=207; lx=262; lvx=306;
  dy=24;

  XDrawString(disp,win,gc, nx+17,110,"NAME",4);
  XDrawString(disp,win,gc, sx+17,110,"SCORE",5);
  XDrawString(disp,win,gc, cx+17,110,"COSMO",5);
  XDrawString(disp,win,gc, lx+17,110,"LINE",4);
  XDrawString(disp,win,gc,lvx+17,110,"LV",2);

  XCopyArea(disp,win,w1attr.win,gc,0,0,w1attr.width,w1attr.height,0,0);
  XFlush(disp);

  for(i=0;i<20;i++)
  {
    set_col(rankcol[i]);
    XDrawString(disp,rankc,gc, gx,10+i*dy,rk[i],4);
    XDrawString(disp,rankc,gc, nx,10+i*dy,best[i].name,strlen(best[i].name));
    XDrawString(disp,rankc,gc, sx,10+i*dy,itoa(best[i].score,8),8);
    XDrawString(disp,rankc,gc, cx,10+i*dy,itoa(best[i].cosmo,5),5);
    XDrawString(disp,rankc,gc, lx,10+i*dy,itoa(best[i].line,4),4);
    XDrawString(disp,rankc,gc,lvx,10+i*dy,itoa(best[i].level,3),3);

    XSetForeground(disp,gc,0);
    XDrawString(disp,rankm,gc, gx,10+i*dy,rk[i],4);
    XDrawString(disp,rankm,gc, nx,10+i*dy,best[i].name,strlen(best[i].name));
    XDrawString(disp,rankm,gc, sx,10+i*dy,itoa(best[i].score,8),8);
    XDrawString(disp,rankm,gc, cx,10+i*dy,itoa(best[i].cosmo,5),5);
    XDrawString(disp,rankm,gc, lx,10+i*dy,itoa(best[i].line,4),4);
    XDrawString(disp,rankm,gc,lvx,10+i*dy,itoa(best[i].level,3),3);
  }

  ty=24*20-1;
  ph=1; py=0;

#ifdef USE_PAD
  clear_pad_buffer();
#endif

  for(;;)
  {
    get_event(0);

         if(key==keys.up    ) { ty-=8; sf=1; }
    else if(key==keys.down  ) { ty+=8; sf=1; }
    else if(event.type==KeyPress) break;

    if(sf==0) ty-=4;
    if(ty<0) { ty=0; sf=1; }
    if(ty>24*20-1) ty=24*20-1;

    if(ty!=py)
    {
      by=ty+24*10;
      if(by>24*20) by=24*20;
      XCopyArea(disp,background,win,gc,17,125,351,ph,17,125);
      XCopyArea(disp,rankm,win,and_gc,0,ty,351,by-ty,17,125);
      XCopyArea(disp,rankc,win, or_gc,0,ty,351,by-ty,17,125);
      if(by-ty>ph) ph=by-ty;
      XCopyArea(disp,win,w1attr.win,gc,17,125,351,ph,17,125);
      XFlush(disp);
      py=ty;
    }

    nap(3-rough);
  }

  XFreePixmap(disp,rankc);
  XFreePixmap(disp,rankm);

  if(key==keys.quit) end_session();
  while(key!=0) get_event(1);

  return level_select();
}
