/*Troy Rosin*/

#include <SDL2/SDL.h>
#include <game.h>
#include <main.h>
#include <overworld.h>
#include <host.h>
#include <mydebug.h>


void handle_input_mainmenu(SDL_Event *e);
void handle_input_battle(SDL_Event *e);
void handle_input_overworld(SDL_Event *e);
void handle_input_host(SDL_Event *e);

void mainmenu_selection_state()
{
  switch (selected_index)
  {
    case START_GAME:          
      state = START; 
      break;
    case HOST_GAME:
      state = HOST;
      break;
    case JOIN_GAME:
      state = JOIN;
      break;
    case OPTIONS_MENU:
      state = OPTIONS;
      break;
    case EXIT_GAME:
      running = 0;
      break;
  }
}

void handle_event(SDL_Event *e){
  while(SDL_PollEvent(e))
  {
    switch(state)
    {
      case TITLE:
        if(e->type == SDL_KEYDOWN)
        { 
          handle_input_mainmenu(e);
        }
        if(e->type == SDL_MOUSEBUTTONDOWN)
        {
          mainmenu_selection_state();
        }
        break;
      case BATTLE:
        switch (e->type)
        {
          case SDL_KEYDOWN:
            handle_input_battle(e);
            break;
          case SDL_MOUSEBUTTONDOWN:
            dragging = GRAB_NEW;
            break;
          case SDL_MOUSEBUTTONUP:
            dragging = LETGO;
        } 
        break;
      case OVERWORLD:
        switch(e->type)
        {
          case SDL_KEYDOWN:
            handle_input_overworld(e);
            break;
        }
        break;
      case HOST:
        handle_input_host(e);      
        break;
      default:
        if(e->type == SDL_KEYDOWN && e->key.keysym.sym == SDLK_ESCAPE)
        {
          running = 0;
        } 
    }
  return;
  }
}

void handle_input_mainmenu(SDL_Event *e)
{
  switch(e->key.keysym.sym){
    case SDLK_ESCAPE:
      running = 0;
      break;
    case SDLK_UP:
    case SDLK_w:
      if (selected_index) { selected_index--; }
      else { selected_index = MAIN_SELECTIONS - 1; }
      selected_index %= MAIN_SELECTIONS;
      break;
    case SDLK_DOWN:
    case SDLK_s:
      selected_index++;
      selected_index %= MAIN_SELECTIONS;
      break;
    case SDLK_RETURN:
      mainmenu_selection_state();
      break; 
  }
  return;
}


void handle_input_battle(SDL_Event *e)
{
  switch(e->key.keysym.sym)
  {
    case SDLK_ESCAPE:
      running = 0;
      break;
  }
  return;
}

void handle_input_overworld(SDL_Event *e)
{
  PLAYER *plr = get_this_player();
  switch(e->key.keysym.sym)
  {
    case SDLK_ESCAPE:
      running = 0;
      break;
    case SDLK_UP:
    case SDLK_w:
      plr->pos.y -= 1;
      break;
    case SDLK_DOWN:
    case SDLK_s:
      plr->pos.y += 1;
      break;
    case SDLK_RIGHT:
    case SDLK_d:
      plr->pos.x += 1;
      break;
    case SDLK_LEFT:
    case SDLK_a:
      plr->pos.x -= 1;
      break;
  }
  return;
}


void handle_input_host(SDL_Event *e)
{
  int oldlen, inputlen;
  char tempname[MAX_NAMESZ], *nameptr;
  nameptr = get_this_name(0);
  strcpy(tempname, nameptr);
  oldlen = strlen(tempname);
  inputlen = strlen(e->text.text);
      
  switch(e->type)
  {
    case SDL_KEYUP:
      break;
    case SDL_KEYDOWN:
      switch(e->key.keysym.sym)
      {
        case SDLK_ESCAPE:
          running = 0;
          break;
        case SDLK_BACKSPACE:
          PRINT_M(got backspace)
          if(oldlen == 0){ break; }
          tempname[oldlen - 1] = '\0';
          strcpy(nameptr, tempname);
          break;
      }
      break;
    case SDL_TEXTINPUT:
      PRINT_M(got text input event)
      if(oldlen + inputlen > MAX_NAMESZ -1){ break; }
      strcat(tempname, e->text.text);
      strcpy(nameptr, tempname);
      break;
    
    default:
      printf("got event type: %u\n", e->type);
  }
  return;
}
