#include "zelda.h"
#include "bitmap.h"
#include "datafile.h"
#include "gfx.h"
#include "misc.h"
#include "intro.h"
#include "sound.h"
#include "game.h"
#include "menu.h"
#include "dat\\menu.h"


typedef struct
{
   int id;
   char text[30];
   int x;
   int y;
   int code;
   int flags;
}MENU_ITEM;

MENU_ITEM *selMenuItem,**menuItem;
int menuPos=0;
int numMenuItems=0;
int menuCode=MENU_MAIN;
void NewMenuItems(int code);
void NewMenuItem(char *text,int code,int flags);
void DeleteMenuItems();
void DoMenuInput();
void DoMenuDraw();
void DoMenu();
MENU_ITEM *GetMenuItem(int search);
void DrawMenuItem(MENU_ITEM *item,int y,int color);
void DrawMenuItems();
int TestMenuItem(MENU_ITEM* item);


void DoMenuLoop()
{
   PlayMod("music\\tune1.jgm");
   while(menuCode!=MAIN_QUIT)
   {
     switch(menuCode)
     {
        case MAIN_INTRO:
         ShowIntro();
         menuCode=MENU_OPTIONS;
        break;
        case MAIN_GAME:
         PlayGame();
         menuCode=MENU_MAIN;
         PlayMod("music\\tune1.jgm");
        break;
        default:
         NewMenuItems(menuCode);
         DoMenu();
        break;
     }
   }
   StopMod();
   DeleteMenuItems();
}

void DoMenu()
{
  RGB white={63,63,63};
  DoMenuDraw();
  fade_in(pal[0].dat,fadeSpeed);
  set_color(255,&white);
  while(!key[KEY_ENTER]||selMenuItem->code==NOP)
  {
     DoMenuInput();
     DoMenuDraw();
  }
  PlaySample(menu[BANG_WAV].dat,digiVol,128,1100,0);
  fade_out(fadeSpeed);
  menuPos=0;
}

void DoMenuInput()
{
   MENU_ITEM *test=selMenuItem;
   if(key[KEY_UP])
    selMenuItem=GetMenuItem(SEARCH_PREV);
   if(key[KEY_DOWN])
    selMenuItem=GetMenuItem(SEARCH_NEXT);
   if(key[KEY_ESC])
    selMenuItem=GetMenuItem(SEARCH_LAST);
   if(key[KEY_TAB])
    SnapShot();
   if(key[KEY_LEFT])
   switch(selMenuItem->code)
     {
       case ACTION_MUSIC_VOLUME:
        if(musicVol>0)
         musicVol--;
        set_mod_volume(musicVol);
       break;
       case ACTION_SFX_VOLUME:
        if(digiVol>0)
         digiVol--;
        set_mod_volume(musicVol);
       break;
     }
   if(key[KEY_RIGHT])
   switch(selMenuItem->code)
     {
       case ACTION_MUSIC_VOLUME:
        if(musicVol<255)
         musicVol++;
        set_mod_volume(musicVol);
       break;
       case ACTION_SFX_VOLUME:
        if(digiVol<255)
         digiVol++;
        set_mod_volume(musicVol);
       break;
     }
   while(selMenuItem->id<menuPos)
     menuPos--;
   while(selMenuItem->id>menuPos+3)
     menuPos++;
   menuCode=selMenuItem->code;
   if(test!=selMenuItem)
    PlaySample(menu[BANG_WAV].dat,digiVol,128,1000,0);
   clear_keybuf();
}

void DoMenuDraw()
{
   clear(buffer);
   DrawMenuItems();
   draw_sprite(buffer,menu[FRAME_BMP].dat,0,0);
   draw_sprite(buffer,menu[LOGO_BMP].dat,0,-2);
   if(menuPos>0)
   {
    draw_sprite(buffer,menu[PIL_BMP].dat,20,84);
    draw_sprite(buffer,menu[PIL_BMP].dat,300,84);
   }
   if(numMenuItems>4&&(numMenuItems-4)>menuPos)
   {
    draw_sprite_v_flip(buffer,menu[PIL_BMP].dat,20,84+90);
    draw_sprite_v_flip(buffer,menu[PIL_BMP].dat,300,84+90);
   }
   vsync();
   if(svga==TRUE)
    blit(buffer,screen,0,0,160,120,320,240);
     else if(svgaStretch==TRUE)
     stretch_blit(buffer,screen,0,0,320,240,0,0,640,480);
      else
       blit(buffer,screen,0,0,0,0,320,240);
}

void DrawMenuItems()
{
   int i;
   if(numMenuItems<5)
   {
    for(i=0;i<numMenuItems;i++)
     DrawMenuItem(menuItem[i],80+30*i,255);
    return;
   }

   for(i=0;i<4;i++)
    DrawMenuItem(menuItem[menuPos+i],80+30*i,255);

}

void DrawMenuItem(MENU_ITEM *item,int y,int color)
{
    char buf[10];
    draw_sprite(buffer,menu[TestMenuItem(item)].dat,0,y);
    if(item->flags&CHANGE)
    {
     textout_centre(buffer,font,item->text,100,y+9,color);
     switch(item->code)
     {
       case ACTION_MUSIC_VOLUME:
        sprintf(buf,"%d",musicVol);
        textout_centre(buffer,font,buf,220,y+9,color);
       break;
       case ACTION_SFX_VOLUME:
        sprintf(buf,"%d",digiVol);
        textout_centre(buffer,font,buf,220,y+9,color);
       break;
     }
    }else
     textout_centre(buffer,font,item->text,160,y+9,color);


}

void NewMenuItems(int code)
{
   DeleteMenuItems();
   switch(code)
   {
      case MENU_MAIN:
       NewMenuItem("START",MAIN_GAME,NORMAL);
       NewMenuItem("OPTIONS",MENU_OPTIONS,NORMAL);
       NewMenuItem("HI-SCORE",MAIN_HISCORE,NORMAL);
       NewMenuItem("CREDITS",MAIN_CREDITS,NORMAL);
       NewMenuItem("QUIT",MENU_QUIT,NORMAL);
      break;
      case MENU_QUIT:
       NewMenuItem("QUIT GAME",NOP,ALWAYS_ON);
       NewMenuItem("NO!!",MENU_MAIN,NORMAL);
       NewMenuItem("YES!!",MAIN_QUIT,NORMAL);
      break;
      case MENU_OPTIONS:
       NewMenuItem("SOUND OPTIONS",MENU_OPTIONS_SOUND,NORMAL);
       NewMenuItem("USED KEYS",MENU_OPTIONS_KEYBOARD,NORMAL);
       NewMenuItem("SHOW INTRO",MAIN_INTRO,NORMAL);
       NewMenuItem("BACK",MENU_MAIN,NORMAL);
      break;
      case MENU_OPTIONS_SOUND:
      if(disableSound==TRUE)
      {
        NewMenuItem("SOUND DISABLED",NOP,ALWAYS_ON);
        NewMenuItem("BACK",MENU_OPTIONS,NORMAL);
        break;
      }
       NewMenuItem("MUSIC VOLUME",ACTION_MUSIC_VOLUME,NORMAL|CHANGE);
       NewMenuItem("SFX VOLUME",ACTION_SFX_VOLUME,NORMAL|CHANGE);
       NewMenuItem("BACK",MENU_OPTIONS,NORMAL);
      break;
      case MAIN_CREDITS:
       NewMenuItem("PROGRAMMING BY",NOP,ALWAYS_ON);
       NewMenuItem("ERIK JOHANSSON",NOP,NORMAL);
       NewMenuItem("BACK",MENU_MAIN,NORMAL);      
      break;
      case MENU_OPTIONS_KEYBOARD:
       NewMenuItem("USE ARROW KEYS TO MOVE",NOP,ALWAYS_ON);
       NewMenuItem("ACTION KEY1 = X",NOP,ALWAYS_ON);
       NewMenuItem("ACTION KEY2 = Z",NOP,ALWAYS_ON);
       NewMenuItem("BACK",MENU_OPTIONS,NORMAL);
      break;
      case MAIN_HISCORE:
      {
        int i;
        char buf[40];
        for(i=0;i<10;i++)
        {
         sprintf(buf,"%s   -   %d",hiscoreList[i].name,hiscoreList[i].score);
         NewMenuItem(buf,NOP,NORMAL);
        }
        NewMenuItem("BACK",MENU_MAIN,NORMAL);
      }
      break;
   }
   selMenuItem=GetMenuItem(SEARCH_FIRST);
}

void NewMenuItem(char *text,int code,int flags)
{
   menuItem[numMenuItems]=malloc(sizeof(MENU_ITEM));
   menuItem[numMenuItems]->id=numMenuItems;
   strcpy(menuItem[numMenuItems]->text,text);
   menuItem[numMenuItems]->code=code;
   menuItem[numMenuItems]->flags=flags;
   numMenuItems++;
}

void DeleteMenuItems()
{
 int i;
   if(!numMenuItems)
    return;
   for(i=0;i<numMenuItems;i++)
    if(menuItem[i]!=NULL)
    {
      free(menuItem[i]);
      menuItem[i]=NULL;
    }
   numMenuItems=0;
}

MENU_ITEM *GetMenuItem(int search)
{
  int i;
  if(search==SEARCH_FIRST)
   for(i=0;i<numMenuItems;i++)
    if(menuItem[i]->flags&NORMAL)
     return menuItem[i];
  if(search==SEARCH_LAST)
   for(i=numMenuItems-1;i>-1;i--)
    if(menuItem[i]->flags&NORMAL)
     return menuItem[i];
  if(search==SEARCH_NEXT)
  {
     i=selMenuItem->id;
     while(1)
     {
       i++;
       if(i==numMenuItems)
        return selMenuItem;
       if(menuItem[i]->flags&NORMAL)
        return menuItem[i];
     }
  }

  if(search==SEARCH_PREV)
  {
     i=selMenuItem->id;
     while(1)
     {
       i--;
       if(i<0)
        return selMenuItem;
     //   i=numMenuItems-1;
       if(menuItem[i]->flags&NORMAL)
       return menuItem[i];
     }
  }
  return selMenuItem;
}

int TestMenuItem(MENU_ITEM* item)
{
   if(item->flags&ALWAYS_ON)
    return BAR3_BMP;
   if(selMenuItem==item)
    return BAR2_BMP;
   return BAR1_BMP;
}


