/*
 *  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 <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"
#include "psp_irkeyb.h"

# define MENU_SCREENSHOT   0
# define MENU_SAVE_LOG     1
# define MENU_VISUAL_BELL  2
# define MENU_HELP         3
# define MENU_COMMAND      4
# define MENU_WORD         5
# define MENU_EDIT_COMMAND 6
# define MENU_EDIT_WORD    7
# define MENU_SAVE_CONFIG  8
# define MENU_BACK         9
# define MENU_DISCONNECT   10
# define MENU_EXIT         11

# define MAX_MENU_ITEM (MENU_EXIT + 1)

  static menu_item_t menu_list[] =
  { 
   { "Save Screenshot :" },
   { "Save Log        :" },
   { "Visual Bell     :" },

   { "Help" },

   { "Paste Command :" },
   { "Paste Word    :" },

   { "Edit Command" },
   { "Edit Word" },
   { "Save config" },

   { "Back" },

   { "Disconnect" },
   { "Exit" }
  };

# define MAX_COMMAND_LINE      512

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

# define MAX_WORD_LINE         512

  static char* psp_word[MAX_WORD_LINE];
  static int   psp_word_size     = -1;
  static int   psp_word_current  = 0;

  static int cur_menu_id = MENU_COMMAND;

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 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, " O/X: Valid  Sel: Back  Start: Keyboard ", 
                       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);
    } else
    if (menu_id == MENU_SAVE_LOG) {
      sprintf(buffer,"%d", PSPSSH.psp_log_id);
      string_fill_with_space(buffer, 4);
      psp_sdl_back_print(160, y, buffer, color);
    } else
    if (menu_id == MENU_VISUAL_BELL) {
      sprintf(buffer, "%s", PSPSSH.psp_visual_bell ? "yes" : "no");
      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(160, y, buffer, color);
    } else
    if (menu_id == MENU_WORD) {
      strncpy(buffer, psp_word[psp_word_current], 32);
      string_fill_with_space(buffer, 32);
      psp_sdl_back_print(160, y, buffer, color);
      y += y_step;
    } else
    if (menu_id == MENU_SAVE_CONFIG) {
      y += y_step;
    }
    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
  {
    myCtrlPeekBufferPositive(&c, 1);
    c.Buttons &= PSP_ALL_BUTTON_MASK;

# ifdef USE_PSP_IRKEYB
    psp_irkeyb_set_psp_key(&c);
# endif

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

  } while (c.Buttons == 0);

  psp_kbd_wait_no_button();

  return 0;
}

int
psp_ssh_menu_disconnect(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
  {
    myCtrlPeekBufferPositive(&c, 1);
    c.Buttons &= PSP_ALL_BUTTON_MASK;

# ifdef USE_PSP_IRKEYB
    psp_irkeyb_set_psp_key(&c);
# endif

    if (c.Buttons & PSP_CTRL_CROSS) {
      cli_session_set_terminated();
    }

  } 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;
  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;
  psp_command_size = 0;

  if (FileDesc != (FILE *)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);
  }

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

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_command()
{
  psp_ssh_put_data_to_ssh(psp_command[psp_command_current], strlen(psp_command[psp_command_current]));
}

static int
psp_find_first_command(char first_char, int check_last)
{
static char last_first_char  = 0;
static int  last_first_index = -1;

  int index  = 0;
  if ((first_char >= 'a') && (first_char <= 'z')) {
    first_char = (first_char - 'a') + 'A';
  }
  if (check_last && (last_first_index >= 0 )) {
    if (first_char == last_first_char) {
      last_first_index++;
      if (last_first_index < psp_command_size) { 
        char test_char = toupper(psp_command[last_first_index][0]);
        if (test_char == first_char) index = last_first_index;
      } else {
        last_first_index = -1;
      }
    }
  }
  while (index < psp_command_size) {
    char test_char = toupper(psp_command[index][0]);
    if (test_char == first_char) {
      last_first_index = index;
      last_first_char  = first_char;
      return index;
    }
    index++;
  }
  last_first_index = -1;
  return -1;
}

void
psp_ssh_menu_find_command(char key)
{
  int index = psp_find_first_command( key, 1 );
  if (index >= 0) psp_command_current = index;
}

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

  char  Buffer[512];
  char *Scan;
  FILE* FileDesc;

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

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

  psp_word_current = 0;
  psp_word_size = 0;

  if (FileDesc != (FILE *)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_word[psp_word_size++] = strdup(Buffer);
      if (psp_word_size >= MAX_WORD_LINE) break;
    }
    fclose(FileDesc);
  }

  if (psp_word_size == 0) {
    psp_word[0] = strdup( "hello");
    psp_word[1] = strdup( "bye");
    psp_word_size = 2;
  }
}

void
psp_word_go_right()
{
  psp_word_current++;
  if (psp_word_current >= psp_word_size) psp_word_current = 0;
}

void
psp_word_go_fast_right()
{
  psp_word_current += 10;
  if (psp_word_current >= psp_word_size) psp_word_current = psp_word_size-1;
}

void
psp_word_go_left()
{
  psp_word_current--;
  if (psp_word_current < 0) psp_word_current = psp_word_size-1;
}

void
psp_word_go_fast_left()
{
  psp_word_current -= 10;
  if (psp_word_current < 0) psp_word_current = 0;
}

void
psp_ssh_menu_word()
{
  psp_ssh_put_data_to_ssh(psp_word[psp_word_current], strlen(psp_word[psp_word_current]));
}

void
psp_ssh_menu_edit_command()
{
  char TmpFileName[MAX_PATH];
  sprintf(TmpFileName, "%s/command.txt", PSPSSH.psp_homedir);
  psp_editor_menu(TmpFileName);
  psp_command_size = 0;
  psp_initialize_command();
}

void
psp_ssh_menu_edit_word()
{
  char TmpFileName[MAX_PATH];
  sprintf(TmpFileName, "%s/word.txt", PSPSSH.psp_homedir);
  psp_editor_menu(TmpFileName);
  psp_word_size = 0;
  psp_initialize_word();
}

static int
psp_find_first_word(char first_char, int check_last)
{
static char last_first_char  = 0;
static int  last_first_index = -1;

  int index  = 0;
  if ((first_char >= 'a') && (first_char <= 'z')) {
    first_char = (first_char - 'a') + 'A';
  }
  if (check_last && (last_first_index >= 0 )) {
    if (first_char == last_first_char) {
      last_first_index++;
      if (last_first_index < psp_word_size) { 
        char test_char = toupper(psp_word[last_first_index][0]);
        if (test_char == first_char) index = last_first_index;
      } else {
        last_first_index = -1;
      }
    }
  }
  while (index < psp_word_size) {
    char test_char = toupper(psp_word[index][0]);
    if (test_char == first_char) {
      last_first_index = index;
      last_first_char  = first_char;
      return index;
    }
    index++;
  }
  last_first_index = -1;
  return -1;
}

void
psp_ssh_menu_find_word(char key)
{
  int index = psp_find_first_word( key, 1 );
  if (index >= 0) psp_word_current = index;
}

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

void
psp_ssh_menu_save_log()
{
  PSPSSH.psp_save_log_mode = 10;
}


static void
psp_ssh_menu_save_config()
{
  int error;

  error = psp_login_save_config();

  if (! error) /* save OK */
  {
    psp_display_screen_menu();
    psp_sdl_back_print(270,  80, "File saved !", PSP_MENU_NOTE_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
  else 
  {
    psp_display_screen_menu();
    psp_sdl_back_print(270,  80, "Can't save file !", 
                       PSP_MENU_WARNING_COLOR);
    psp_sdl_flip();
    sleep(1);
  }
}

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;
  int         irkeyb_key;

  psp_initialize_command();
  psp_initialize_word();

  psp_kbd_wait_no_button();

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

# ifdef USE_PSP_IRKEYB
  irkeyb_key = PSP_IRKEYB_EMPTY;
# endif

  while (! end_menu)
  {
    psp_display_screen_menu();

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

    while (1) {

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

# ifdef USE_PSP_IRKEYB
      irkeyb_key = psp_irkeyb_set_psp_key(&c);
# endif
      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;
        break;
      }
# ifdef USE_PSP_IRKEYB
      if (irkeyb_key != PSP_IRKEYB_EMPTY) break;
# endif
    }

# ifdef USE_PSP_IRKEYB
    irkeyb_key = psp_irkeyb_read_key();

    if (irkeyb_key > 0) {
      if (irkeyb_key > ' ') {
        if (cur_menu_id == MENU_COMMAND) {
          psp_ssh_menu_find_command(irkeyb_key);
        } else 
        if (cur_menu_id == MENU_WORD) {
          psp_ssh_menu_find_word(irkeyb_key);
        }

      } else
      if (irkeyb_key == 0xd) {
        if (cur_menu_id == MENU_COMMAND) {
          psp_ssh_menu_command();
        } else 
        if (cur_menu_id == MENU_WORD) {
          psp_ssh_menu_word();
        }
        end_menu  = 1;
      }
      irkeyb_key = PSP_IRKEYB_EMPTY;
    }
# endif

    if (danzeff_mode) {

      danzeff_key = danzeff_readInput(c);

      if (danzeff_key > DANZEFF_START) {
        /* Disable utf8 */
        danzeff_key &= 0x7f;
        if (danzeff_key > ' ') {
          if (cur_menu_id == MENU_COMMAND) {
            psp_ssh_menu_find_command(danzeff_key);
          } else
          if (cur_menu_id == MENU_WORD) {
            psp_ssh_menu_find_word(danzeff_key);
          }
        } else
        if ((danzeff_key == DANZEFF_ENTER) ||
            (danzeff_key == DANZEFF_DOWN )) {
          if (cur_menu_id == MENU_COMMAND) {
            psp_ssh_menu_command();
          } else 
          if (cur_menu_id == MENU_WORD) {
            psp_ssh_menu_word();
          }
          end_menu  = 1;

        } else
        if (danzeff_key == DANZEFF_LEFT) { /* Left */
          if (cur_menu_id == MENU_COMMAND) {
            psp_command_go_left();
          } else
          if (cur_menu_id == MENU_WORD) {
            psp_word_go_left();
          }

        } else
        if (danzeff_key == DANZEFF_RIGHT) { /* Right */
          if (cur_menu_id == MENU_COMMAND) {
            psp_command_go_right();
          } else
          if (cur_menu_id == MENU_WORD) {
            psp_word_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))
    {
      switch (cur_menu_id ) 
      {
        case MENU_COMMAND   : psp_ssh_menu_command();
                              end_menu = 1;
        break;
        case MENU_WORD      : psp_ssh_menu_word();
                              end_menu = 1;
        break;
        case MENU_EDIT_COMMAND : psp_ssh_menu_edit_command();
        break;
        case MENU_EDIT_WORD    : psp_ssh_menu_edit_word();
        break;

        case MENU_VISUAL_BELL : PSPSSH.psp_visual_bell = ! PSPSSH.psp_visual_bell;
        break;

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

        case MENU_SAVE_LOG   : psp_ssh_menu_save_log();
                               end_menu = 1;
        break;              

        case MENU_SAVE_CONFIG : psp_ssh_menu_save_config();
        break;              

        case MENU_BACK      : end_menu = 1;
        break;

        case MENU_DISCONNECT: psp_ssh_menu_disconnect();
                              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) {
      if (cur_menu_id == MENU_COMMAND) {
        psp_command_go_right();
      } else
      if (cur_menu_id == MENU_WORD) {
        psp_word_go_right();
      }

    } else
    if(new_pad & PSP_CTRL_LEFT) {
      if (cur_menu_id == MENU_COMMAND) {
        psp_command_go_left();
      } else
      if (cur_menu_id == MENU_WORD) {
        psp_word_go_left();
      }
    } else
    if(new_pad & PSP_CTRL_RTRIGGER) {
      if (cur_menu_id == MENU_COMMAND) {
        psp_command_go_fast_right();
      } else 
      if (cur_menu_id == MENU_WORD) {
        psp_word_go_fast_right();
      }

    } else
    if(new_pad & PSP_CTRL_LTRIGGER) {
      if (cur_menu_id == MENU_COMMAND) {
        psp_command_go_fast_left();
      } else 
      if (cur_menu_id == MENU_WORD) {
        psp_word_go_fast_left();
      }
    }
  }
 
  psp_kbd_wait_no_button();

  return 0;
}

