/*
 *  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_global.h"
#include "psp_danzeff.h"
#include "psp_sdl.h"
#include "psp_fmgr.h"
#include "psp_menu.h"
#include "psp_battery.h"
#include "psp_help.h"

# define MENU_SCREENSHOT   0
# define MENU_HELP         1
# define MENU_COMMAND      2
# define MENU_BACK         3
# define MENU_EXIT         4

# define MAX_MENU_ITEM (MENU_EXIT + 1)

  static menu_item_t menu_list[] =
  { 
    "Save Screenshot :",

    "Help",

    "Command :",

    "Back",
    "Exit"
  };

# define MAX_COMMAND_LINE      256

  static char* psp_command[MAX_COMMAND_LINE];
  static int   psp_command_size    = -1;
  static int   psp_command_current = 0;

  static int cur_menu_id = MENU_COMMAND;
  static int cur_slot    = 0;

void
string_fill_with_space(char *buffer, int size)
{
  int length = strlen(buffer);
  int index;

  for (index = length; index < size; index++) {
    buffer[index] = ' ';
  }
  buffer[size] = 0;
}

void
psp_display_screen_menu_battery(void)
{
  char buffer[64];

  int Length;
  int color;

  snprintf(buffer, 50, " [%s] ", psp_get_battery_string());
  Length = strlen(buffer);

  if (psp_is_low_battery()) color = PSP_MENU_RED_COLOR;
  else                      color = PSP_MENU_GREEN_COLOR;

  psp_sdl_back_print(240 - ((8*Length) / 2), 6, buffer, color);
}

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

  {
    psp_sdl_blit_menu();

    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_back_print( 30, 6, " Console ", PSP_MENU_NOTE_COLOR);

    psp_display_screen_menu_battery();

    psp_sdl_back_print(30, 254, " []: Cancel  O/X: Valid  SELECT: Back ", 
                       PSP_MENU_BORDER_COLOR);

    psp_sdl_back_print(370, 254, " By Zx-81 ",
                       PSP_MENU_AUTHOR_COLOR);
  }
  
  x      = 20;
  y      = 25;
  y_step = 10;
  
  for (menu_id = 0; menu_id < MAX_MENU_ITEM; menu_id++) {
    color = PSP_MENU_TEXT_COLOR;
    if (cur_menu_id == menu_id) color = PSP_MENU_SEL_COLOR;
    else 
    if (menu_id == MENU_EXIT) color = PSP_MENU_WARNING_COLOR;
    else
    if (menu_id == MENU_HELP) color = PSP_MENU_GREEN_COLOR;

    psp_sdl_back_print(x, y, menu_list[menu_id].title, color);

    if (menu_id == MENU_SCREENSHOT) {
      sprintf(buffer,"%d", PSPSSH.psp_screenshot_id);
      string_fill_with_space(buffer, 4);
      psp_sdl_back_print(160, y, buffer, color);
      y += y_step;
    } else
    if (menu_id == MENU_HELP) {
      y += y_step;
    } else
    if (menu_id == MENU_COMMAND) {
      strncpy(buffer, psp_command[psp_command_current], 32);
      string_fill_with_space(buffer, 32);
      psp_sdl_back_print(100, y, buffer, color);
    } else
    if (menu_id == MENU_BACK) {
      y += y_step;
    }

    y += y_step;
  }
}

int
psp_ssh_menu_exit(void)
{
  SceCtrlData c;

  psp_display_screen_menu();
  psp_sdl_back_print(270,  80, "press X to confirm !", PSP_MENU_WARNING_COLOR);
  psp_sdl_flip();

  psp_kbd_wait_no_button();

  do
  {
    sceCtrlReadBufferPositive(&c, 1);
    c.Buttons &= PSP_ALL_BUTTON_MASK;

    if (c.Buttons & PSP_CTRL_CROSS) psp_sdl_exit(0);

  } while (c.Buttons == 0);

  psp_kbd_wait_no_button();

  return 0;
}

void
psp_initialize_command()
{
  char  FileName[MAX_PATH+1];

  char  Buffer[512];
  char *Scan;
  unsigned int Value;
  FILE* FileDesc;

  /* Already done ? */
  if (psp_command_size > 0) return; 

  getcwd(FileName, MAX_PATH);
  strcat(FileName, "/command.txt");
  FileDesc = fopen(FileName, "r");

  psp_command_current = 0;

  if (FileDesc == (FILE *)0 ) {
    psp_command[0] = strdup( "ls");
    psp_command[1] = strdup( "clear");
    psp_command_size = 2;
    return;
  }

  psp_command_size = 0;
  while (fgets(Buffer,512, FileDesc) != (char *)0) {

    Scan = strchr(Buffer,'\n');
    if (Scan) *Scan = '\0';
    /* For this #@$% of windows ! */
    Scan = strchr(Buffer,'\r');
    if (Scan) *Scan = '\0';

    psp_command[psp_command_size++] = strdup(Buffer);
    if (psp_command_size >= MAX_COMMAND_LINE) break;
  }
  fclose(FileDesc);
}

void
psp_command_go_right()
{
  psp_command_current++;
  if (psp_command_current >= psp_command_size) psp_command_current = 0;
}

void
psp_command_go_fast_right()
{
  psp_command_current += 10;
  if (psp_command_current >= psp_command_size) psp_command_current = psp_command_size-1;
}

void
psp_command_go_left()
{
  psp_command_current--;
  if (psp_command_current < 0) psp_command_current = psp_command_size-1;
}

void
psp_command_go_fast_left()
{
  psp_command_current -= 10;
  if (psp_command_current < 0) psp_command_current = 0;
}

void
psp_ssh_menu_screenshot()
{
  PSPSSH.psp_screenshot_mode = 10;
}

void
psp_ssh_menu_command()
{
  psp_ssh_put_data_to_ssh(psp_command[psp_command_current], strlen(psp_command[psp_command_current]));
}

void
psp_ssh_menu_find_command(char key)
{
  int index    = 0;
  for (index = 0; index <  psp_command_size; index++) {
    if (psp_command[index][0] == key) {
      psp_command_current = index;
      break;
    } 
    if (psp_command[index][0] > key) break;
  }
}

int 
psp_ssh_menu(void)
{
  SceCtrlData c;
  long        new_pad;
  long        old_pad;

  int         last_time;
  int         end_menu;

  int         danzeff_mode = 0;
  int         danzeff_key;

  psp_initialize_command();

  psp_kbd_wait_no_button();

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

  while (! end_menu)
  {
    psp_display_screen_menu();

    if (danzeff_mode) {
      danzeff_moveTo(-50, -50);
      danzeff_render();
    }
    psp_sdl_flip();

   	sceKernelDelayThread(1000);

    sceCtrlReadBufferPositive(&c, 1);
    c.Buttons &= PSP_ALL_BUTTON_MASK;

    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 (danzeff_mode) {

      danzeff_key = danzeff_readInput(c);

      if (danzeff_key > DANZEFF_START) {
        if (danzeff_key > ' ') {
          psp_ssh_menu_find_command(danzeff_key);
        } else
        if ((danzeff_key == DANZEFF_ENTER) ||
            (danzeff_key == DANZEFF_DOWN )) {
          psp_ssh_menu_command();
          end_menu  = 1;

        } else
        if (danzeff_key == DANZEFF_LEFT) { /* Left */
          psp_command_go_left();
        } else
        if (danzeff_key == DANZEFF_RIGHT) { /* Right */
          psp_command_go_right();
        }

      } else 
      if ((danzeff_key == DANZEFF_START ) || 
          (danzeff_key == DANZEFF_SELECT)) 
      {
        danzeff_mode = 0;
        old_pad = new_pad = 0;

        psp_kbd_wait_no_button();
      }

      continue;
    }


    if ((c.Buttons & (PSP_CTRL_LTRIGGER|PSP_CTRL_RTRIGGER|PSP_CTRL_START)) ==
        (PSP_CTRL_LTRIGGER|PSP_CTRL_RTRIGGER|PSP_CTRL_START)) {
      /* Exit ! */
      psp_sdl_exit(0);
    } else
    if ((new_pad & PSP_CTRL_CROSS ) || 
        (new_pad & PSP_CTRL_CIRCLE))
    {
      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 ) 
      {
# if 0
        case MENU_KEYBOARD   : psp_keyboard_menu();
                               old_pad = new_pad = 0;
        break;
        case MENU_SETTINGS   : psp_settings_menu();
                               old_pad = new_pad = 0;
        break;
# endif
        case MENU_COMMAND   : psp_ssh_menu_command();
                              end_menu = 1;
        break;

        case MENU_SCREENSHOT : psp_ssh_menu_screenshot();
                               end_menu = 1;
        break;              

        case MENU_BACK      : end_menu = 1;
        break;

        case MENU_EXIT      : psp_ssh_menu_exit();
        break;

        case MENU_HELP      : psp_help_menu();
                              old_pad = new_pad = 0;
        break;              
      }

    } else
    if(new_pad & PSP_CTRL_START) {
      danzeff_mode = 1;
    } 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 Console */
      end_menu = 1;
    } else
    if(new_pad & PSP_CTRL_RIGHT) {
      psp_command_go_right();
    } else
    if(new_pad & PSP_CTRL_LEFT) {
      psp_command_go_left();
    } else
    if(new_pad & PSP_CTRL_RTRIGGER) {
      psp_command_go_fast_right();
    } else
    if(new_pad & PSP_CTRL_LTRIGGER) {
      psp_command_go_fast_left();
    }
  }
 
  psp_kbd_wait_no_button();

  return 1;
}

