/*
 *  Copyright (C) 2006 Ludovic Jacomme (ludovic.jacomme@gmail.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_ttf.h>

#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspiofilemgr.h>

#include "psp_sdl.h"
#include "psp_fmgr.h"
#include "psp_kbd.h"
#include "psp_run.h"
#include "psp_menu.h"
#include "psp_menu_kbd.h"
#include "beebwinc.h"

extern SDL_Surface *screen_ptr;

# if 0 //TO_BE_DONE !
# define MENU_CPU           
# define MENU_SKIP_FPS      
# define MENU_RENDER        
# endif

# define MENU_SOUND        0
# define MENU_ANALOG       1
# define MENU_CLOCK        2
# define MENU_SCREENSHOT   3
                          
# if 0 //TO_BE_DONE !
# define MENU_CUR_SLOT      
# define MENU_LOAD_SLOT     
# define MENU_SAVE_SLOT     
# define MENU_DEL_SLOT      
# define MENU_LOAD_STATE    
# endif
                          
# define MENU_LOAD_DISK    4
# define MENU_KEYBOARD     5
# define MENU_SAVE_CFG     6

# define MENU_RESET        7
# define MENU_BACK         8

# define MAX_MENU_ITEM (MENU_BACK + 1)


  static menu_item_t menu_list[] =
  { 
    "Sound enable        :",
# if 0 //TO_BE_DONE
    "CPU display         :",
    "Skip frame          :",
    "Render mode         :",
# endif
    "Swap Analog/Cursor  :",
    "Clock frequency     :",
    "Save Screenshot     :",

# if 0 //TO_BE_DONE
    "Change Slot",           
    "Load Slot",
    "Save Slot",
    "Delete Slot",
    "Load State",   
# endif

    "Load Disk",     
    "Keyboard", 
    "Save Config",

    "Reset BBC",
    "Back to BBC",
  };

  static int cur_menu_id = MENU_LOAD_DISK;
  static int cur_slot    = 0;

  static int beeb_snd_enabled       = 0;
# if 0 //TO_BE_DONE
  static int beeb_scr_cpu           = 0;
  static int beeb_render_mode       = 0;
  static int beeb_skip_fps          = 0;
# endif
  static int psp_reverse_analog     = 0;
  static int psp_cpu_clock          = 222;

void 
psp_menu_display_save_name()
{
  char buffer[32];
  int Length;
  snprintf(buffer, 20, "Game: %s", beeb_get_save_name());
  Length = strlen(buffer);
  psp_sdl_fill_print(460 - (8*Length), 25, buffer, PSP_MENU_TEXT2_COLOR, PSP_MENU_BACKGROUND_COLOR);
}

static void 
psp_display_screen_menu(void)
{
  char buffer[32];
  int menu_id = 0;
  int slot_id = 0;
  int color   = 0;
  int x       = 0;
  int y       = 0;
  int y_step  = 0;

  psp_sdl_blit_background();

  psp_sdl_draw_rectangle(10,10,459,249,PSP_MENU_BORDER_COLOR,0);
  psp_sdl_draw_rectangle(11,11,457,247,PSP_MENU_BORDER_COLOR,0);

  psp_sdl_fill_print( 30, 6, " LTrigger: EXIT ", 
                     PSP_MENU_WARNING_COLOR,  PSP_MENU_BACKGROUND_COLOR);

  psp_sdl_fill_print( 310, 6, " RTrigger: RESET ", 
                     PSP_MENU_NOTE_COLOR,  PSP_MENU_BACKGROUND_COLOR);

  psp_sdl_fill_print(30, 254, " []: Cancel  O/X: Valid  SELECT: Back ", 
                     PSP_MENU_BORDER_COLOR, PSP_MENU_BACKGROUND_COLOR);
  psp_sdl_fill_print(370, 254, " By Zx-81 ",
                     PSP_MENU_AUTHOR_COLOR, PSP_MENU_BACKGROUND_COLOR);
  
  x      = 20;
  y      = 25;
  y_step = 10;
  
  for (menu_id = 0; menu_id < MAX_MENU_ITEM; menu_id++) {
    if (cur_menu_id == menu_id) color = PSP_MENU_SEL_COLOR;
    else                        color = PSP_MENU_TEXT_COLOR;
    psp_sdl_fill_print(x, y, menu_list[menu_id].title, color, PSP_MENU_BACKGROUND_COLOR);

    if (menu_id == MENU_SOUND) {
      if (beeb_snd_enabled) strcpy(buffer,"yes");
      else                 strcpy(buffer,"no ");
      psp_sdl_fill_print(210, y, buffer, color, PSP_MENU_BACKGROUND_COLOR);
    } 
# if 0 //TO_BE_DONE
    else
    if (menu_id == MENU_CPU) {
      if (beeb_scr_cpu) strcpy(buffer,"on ");
      else             strcpy(buffer,"off");
      psp_sdl_fill_print(210, y, buffer, color, PSP_MENU_BACKGROUND_COLOR);
    } else
    if (menu_id == MENU_SKIP_FPS) {
      sprintf(buffer,"%d", beeb_skip_fps);
      psp_sdl_fill_print(210, y, buffer, color, PSP_MENU_BACKGROUND_COLOR);
    } else
    if (menu_id == MENU_RENDER) {
      if (beeb_render_mode == BEEB_RENDER_NORMAL   ) strcpy(buffer, "normal");
      else 
      if (beeb_render_mode == BEEB_RENDER_FIT_WIDTH) strcpy(buffer, "fit width");
# if 0 //LUDO: TO_BE_DONE !
      else
      if (beeb_render_mode == BEEB_RENDER_X125     ) strcpy(buffer, "x1.25");
      else                                           strcpy(buffer, "x1.5");
# endif
      psp_sdl_fill_print(210, y, buffer, color, PSP_MENU_BACKGROUND_COLOR);
    } else
# endif
    if (menu_id == MENU_ANALOG) {
      if (psp_reverse_analog) strcpy(buffer,"yes");
      else                    strcpy(buffer,"no ");
      psp_sdl_fill_print(210, y, buffer, color, PSP_MENU_BACKGROUND_COLOR);
    } else
    if (menu_id == MENU_CLOCK) {
      sprintf(buffer,"%d", psp_cpu_clock);
      psp_sdl_fill_print(210, y, buffer, color, PSP_MENU_BACKGROUND_COLOR);
    } else
    if (menu_id == MENU_SCREENSHOT) {
      sprintf(buffer,"%d", beeb_get_psp_screenshot_id());
      psp_sdl_fill_print(210, y, buffer, color, PSP_MENU_BACKGROUND_COLOR);
      y += y_step;
    }
# if 0 //TO_BE_DONE
    if (menu_id == MENU_DEL_SLOT) {
      y += y_step;
    } else
# endif
    if (menu_id == MENU_KEYBOARD) {
      y += y_step;
    }

    y += y_step;
  }

# if 0 //LUDO: TO_BE_DONE !
  y_step = 10;
  y      = 105;

  for (slot_id = 0; slot_id < BEEB_MAX_SAVE_STATE; slot_id++) {
    if (slot_id == cur_slot) color = PSP_MENU_SEL2_COLOR;
    else                     color = PSP_MENU_TEXT_COLOR;

    if (BEEB.beeb_save_used[slot_id]) sprintf(buffer, "%d - used" , slot_id + 1);
    else                              sprintf(buffer, "%d - empty", slot_id + 1);
    psp_sdl_fill_print(160, y, buffer, color, PSP_MENU_BACKGROUND_COLOR);

    y += y_step;
  }
# endif

  psp_menu_display_save_name();
}

static void
psp_main_menu_reset(void)
{
  /* Reset ! */
  psp_display_screen_menu();
  psp_sdl_fill_print(188, 160, "RESET BEEB !", 
                     PSP_MENU_WARNING_COLOR, PSP_MENU_BACKGROUND_COLOR);
  psp_sdl_flip();
  beeb_emulator_reset();
  beeb_reset_save_name();
  sleep(1);
}

static void
psp_main_menu_clock(int step)
{
  if (step > 0) {
    if (psp_cpu_clock == 222) {
      psp_cpu_clock = 300;
    } else if (psp_cpu_clock == 300) {
      psp_cpu_clock = 333;
    } else {
      psp_cpu_clock = 222;
    }
  } else {
    if (psp_cpu_clock == 222) {
      psp_cpu_clock = 333;
    } else if (psp_cpu_clock == 333) {
      psp_cpu_clock = 300;
    } else {
      psp_cpu_clock = 222;
    }
  }
}

# if 0 //TO_BE_DONE
static void
psp_main_menu_skip_fps(int step)
{
  if (step > 0) {
    if (beeb_skip_fps < 25) beeb_skip_fps++;
  } else {
    if (beeb_skip_fps > 0) beeb_skip_fps--;
  }
}

static void
psp_main_menu_render(int step)
{
  if (step > 0) {
    if (beeb_render_mode < BEEB_LAST_RENDER) beeb_render_mode++;
    else                                     beeb_render_mode = 0;
  } else {
    if (beeb_render_mode > 0) beeb_render_mode--;
    else                      beeb_render_mode = BEEB_LAST_RENDER;
  }
}
# endif

static void
psp_main_menu_load(int format)
{
  int ret;

  ret = psp_fmgr_menu(format);
  if (ret ==  1) /* load OK */
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "File loaded !", 
                       PSP_MENU_NOTE_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
  else 
  if (ret == -1) /* Load Error */
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "Can't load file !", 
                       PSP_MENU_WARNING_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
}

# if 0 //LUDO: TO_BE_DONE !
static void
psp_main_menu_load_slot()
{
  int error;

  if (! BEEB.beeb_save_used[cur_slot]) {

    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "Slot is empty !", 
                       PSP_MENU_NOTE_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
    return;
  }

  error = cap32_snapshot_load_slot(cur_slot);

  if (! error) /* save OK */
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "File loaded !", 
                       PSP_MENU_NOTE_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
  else 
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "Can't load file !", 
                       PSP_MENU_WARNING_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
}
# endif

# if 0 //LUDO: TO_BE_DONE !
static void
psp_main_menu_save_slot()
{
  int error;
  error = cap32_snapshot_save_slot(cur_slot);

  if (! error) /* save OK */
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "File saved !", 
                       PSP_MENU_NOTE_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
  else 
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "Can't save file !", 
                       PSP_MENU_WARNING_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
}
# endif

# if 0 //LUDO: TO_BE_DONE !
static void
psp_main_menu_del_slot()
{
  int error;

  if (! BEEB.beeb_save_used[cur_slot]) {

    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "Slot is empty !", 
                       PSP_MENU_NOTE_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);

    return;
  }

  error = cap32_snapshot_del_slot(cur_slot);

  if (! error) /* save OK */
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "File deleted !", 
                       PSP_MENU_NOTE_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
  else 
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "Can't delete file !", 
                       PSP_MENU_WARNING_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
}
# endif

# if 0 //TO_BE_DONE
static void
psp_main_menu_cur_slot(int step)
{
  if (step == 1) {
    cur_slot++; if (cur_slot >= BEEB_MAX_SAVE_STATE) cur_slot = 0;
  } else if (step == -1) {
    cur_slot--; if (cur_slot < 0) cur_slot = BEEB_MAX_SAVE_STATE - 1;
  }
}
# endif

static void
psp_main_menu_screenshot(void)
{
  psp_screenshot_mode = 10;
}

static void
psp_main_menu_validate(void)
{
  /* Validate */
  beeb_set_psp_cpu_clock      ( psp_cpu_clock );
  beeb_set_psp_reverse_analog ( psp_reverse_analog );
  beeb_set_snd_enabled        ( beeb_snd_enabled );
# if 0 //LUDO: TO_BE_DONE
  beeb_set_skip_max_frame     ( beeb_skip_fps );
  beeb_set_scr_cpu            ( beeb_scr_cpu );
  beeb_set_render_mode        ( beeb_render_mode );
  beeb_set_skip_cur_frame ( 0 );
# endif

  scePowerSetClockFrequency(psp_cpu_clock, psp_cpu_clock, psp_cpu_clock/2);
}

static void
psp_main_menu_save_config()
{
  int error;

  psp_main_menu_validate();
  error = beeb_save_configuration();

  if (! error) /* save OK */
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "File saved !", 
                       PSP_MENU_NOTE_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
  else 
  {
    psp_display_screen_menu();
    psp_sdl_fill_print(170, 160, "Can't save file !", 
                       PSP_MENU_WARNING_COLOR, PSP_MENU_BACKGROUND_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
}

int 
psp_main_menu(void)
{
  long new_pad;
  long old_pad;
  int  last_time;
  int  end_menu;

  beeb_audio_pause();

  psp_kbd_wait_no_button();

  old_pad   = 0;
  last_time = 0;
  end_menu  = 0;

# if 0 //TO_BE_DONE
  beeb_scr_cpu           = beeb_get_scr_cpu();
  beeb_render_mode       = beeb_get_render_mode();
  beeb_skip_fps          = beeb_get_skip_max_frame();
# endif
  beeb_snd_enabled       = beeb_get_snd_enabled();
  psp_cpu_clock          = beeb_get_psp_cpu_clock();
  psp_reverse_analog     = beeb_get_psp_reverse_analog();

  while (! end_menu)
  {
    psp_display_screen_menu();
    psp_sdl_flip();


    SceCtrlData c;
    sceCtrlReadBufferPositive(&c, 1);

    new_pad = c.Buttons;

    if ((old_pad != new_pad) || ((c.TimeStamp - last_time) > PSP_MENU_MIN_TIME)) {
      last_time = c.TimeStamp;
      old_pad = new_pad;

    } else continue;

    if ((c.Buttons & PSP_CTRL_LTRIGGER) == PSP_CTRL_LTRIGGER) {
      /* Exit ! */
      SDL_Quit();
      exit(0);
    } else
    if ((c.Buttons & PSP_CTRL_RTRIGGER) == PSP_CTRL_RTRIGGER) {
      psp_main_menu_reset();
      end_menu = 1;
    } else
    if ((new_pad & PSP_CTRL_CROSS ) || 
        (new_pad & PSP_CTRL_CIRCLE) || 
        (new_pad & PSP_CTRL_RIGHT ) ||
        (new_pad & PSP_CTRL_LEFT  )) 
    {
      int step;

      if (new_pad & PSP_CTRL_LEFT)  step = -1;
      else 
      if (new_pad & PSP_CTRL_RIGHT) step =  1;
      else                          step =  0;

      switch (cur_menu_id ) 
      {
        case MENU_SOUND     : beeb_snd_enabled = ! beeb_snd_enabled;
        break;              
# if 0 //TO_BE_DONE
        case MENU_CPU       : beeb_scr_cpu     = ! beeb_scr_cpu;
        break;              
        case MENU_SKIP_FPS  : psp_main_menu_skip_fps( step );
        break;              
        case MENU_RENDER    : psp_main_menu_render( step );
        break;              
        case MENU_CUR_SLOT  : psp_main_menu_cur_slot(step);
        break;
        case MENU_LOAD_SLOT : if (step) psp_main_menu_cur_slot(step);
                              else      psp_main_menu_load_slot();
        break;
        case MENU_SAVE_SLOT : if (step) psp_main_menu_cur_slot(step);
                              else      psp_main_menu_save_slot();
        break;
        case MENU_DEL_SLOT  : if (step) psp_main_menu_cur_slot(step);
                              else      psp_main_menu_del_slot();
        break;

        case MENU_LOAD_STATE : psp_main_menu_load(FMGR_FORMAT_STATE);
                               old_pad = new_pad = 0;
        break;              
# endif
        case MENU_ANALOG    : psp_reverse_analog = ! psp_reverse_analog;
        break;              
        case MENU_CLOCK     : psp_main_menu_clock( step );
        break;

        case MENU_LOAD_DISK  : psp_main_menu_load(FMGR_FORMAT_DISK);
                               old_pad = new_pad = 0;
        break;              
        case MENU_KEYBOARD   : psp_keyboard_menu();
                               old_pad = new_pad = 0;
        break;
        case MENU_SCREENSHOT : psp_main_menu_screenshot();
                               end_menu = 1;
        break;              
        case MENU_SAVE_CFG   : psp_main_menu_save_config();
        break;               
        case MENU_RESET      : psp_main_menu_reset();
                               end_menu = 1;
        break;               
        case MENU_BACK       : end_menu = 1;
        break;
      }

    } else
    if(new_pad & PSP_CTRL_UP) {

      if (cur_menu_id > 0) cur_menu_id--;
      else                 cur_menu_id = MAX_MENU_ITEM-1;

    } else
    if(new_pad & PSP_CTRL_DOWN) {

      if (cur_menu_id < (MAX_MENU_ITEM-1)) cur_menu_id++;
      else                                 cur_menu_id = 0;

    } else  
    if(new_pad & PSP_CTRL_SQUARE) {
      /* Cancel */
      end_menu = -1;
    } else 
    if(new_pad & PSP_CTRL_SELECT) {
      /* Back to BEEB */
      end_menu = 1;
    }
  }
 
  if (end_menu > 0) {
    psp_main_menu_validate();
  }

  psp_kbd_wait_no_button();

  psp_sdl_clear_screen( PSP_MENU_BLACK_COLOR );
  psp_sdl_flip();
  psp_sdl_clear_screen( PSP_MENU_BLACK_COLOR );
  psp_sdl_flip();

  beeb_audio_resume();

  return 1;
}

