/*==========================================================
  File:  gui.c
  Author:  _pragma

  Description:  Sets up the layout of the game graphical
                user interface and handles callbacks for
                elements.
  ==========================================================*/


#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <SDL/SDL.h>

#include "texture.h"
#include "console.h"
#include "cvar.h"
#include "game.h"
#include "gui_elements.h"
#include "gui.h"
#include "util.h"
#include "sdl.h"

gui_element_t *gui_element_list = 0;
int gui_last_state = 0gui_state = 0;

extern int game_state;

#define ELEMENT_INIT(c, type, text, x1, y1, x2, y2, texture) \
  if(!((c) = gui_create_element( (type), (text), (x1), (y1), (x2), (y2), (texture)) )) \
    return false

void gui_init(void)
{
  gui_switch_state(GUI_STATE_MAINMENU);
}

int gui_init_startup(void)
{
  gui_element_t *element;
  extern cvar_t *cvar_scr_width, *cvar_scr_height;
  int width = cvar_scr_width ? atoi(cvar_scr_width->value) : SCREEN_WIDTH;
  int height = cvar_scr_height ? atoi(cvar_scr_height->value) : SCREEN_HEIGHT;

  ELEMENT_INIT(elementGUI_BUTTON0
               (width/2) - 65110, (width/2) + 65200
               "data/textures/gui/gui_newgame.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_newgame_on_click;

  ELEMENT_INIT(elementGUI_BUTTON0
               (width/2) - 65210, (width/2) + 65300
               "data/textures/gui/gui_options.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_options_on_click;

  ELEMENT_INIT(elementGUI_BUTTON0
               (width/2) - 65310, (width/2) + 65400
               "data/textures/gui/gui_quitgame.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_exitgame_on_click;

  return true;
}

int gui_init_ingamemenu(void)
{
  gui_element_t *element;
  extern cvar_t *cvar_scr_width, *cvar_scr_height;
  int width = cvar_scr_width ? atoi(cvar_scr_width->value) : SCREEN_WIDTH;
  int height = cvar_scr_height ? atoi(cvar_scr_height->value) : SCREEN_HEIGHT;

  ELEMENT_INIT(elementGUI_BUTTON0
               (width/2) - 85110, (width/2) + 85200
               "data/textures/gui/gui_resumegame.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_resumegame_on_click;

  ELEMENT_INIT(elementGUI_BUTTON0
               (width/2) - 75210, (width/2) + 75300
               "data/textures/gui/gui_newgame.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_newgame_on_click;

  ELEMENT_INIT(elementGUI_BUTTON0
               (width/2) - 75310, (width/2) + 75400
               "data/textures/gui/gui_quitgame.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_exitgame_on_click;

  return true;
}

int gui_init_options(void)
{
  gui_element_t *element;
  extern cvar_t *cvar_scr_width, *cvar_scr_height;
  int width = cvar_scr_width ? atoi(cvar_scr_width->value) : SCREEN_WIDTH;
  int height = cvar_scr_height ? atoi(cvar_scr_height->value) : SCREEN_HEIGHT;

  ELEMENT_INIT(elementGUI_BUTTON0
               0height-50100height
               "data/textures/gui/gui_back.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_backbutton_on_click;

  ELEMENT_INIT(elementGUI_BUTTON0
                        width/2-65175width/2+65245
                        "data/textures/gui/gui_config.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_config_on_click;  

  return true;
}

int gui_init_config(void)
{
  gui_element_t *element;
  extern cvar_t *cvar_scr_width, *cvar_scr_height;
  int width = cvar_scr_width ? atoi(cvar_scr_width->value) : SCREEN_WIDTH;
  int height = cvar_scr_height ? atoi(cvar_scr_height->value) : SCREEN_HEIGHT;


  ELEMENT_INIT(elementGUI_BUTTON0
               0height-50100height
               "data/textures/gui/gui_back.tga");
  gui_add_element_to_list(element, &gui_element_list);
  element->on_click = gui_backbutton_on_click;

  return true;
}

void gui_backbutton_on_click(float xfloat yint button)
{
  gui_switch_state(gui_last_state);
}

void gui_options_on_click(float xfloat yint button)
{
  gui_switch_state(GUI_STATE_OPTIONS);
  gui_last_state = GUI_STATE_MAINMENU;
}

void gui_config_on_click(float xfloat yint button)
{
  gui_switch_state(GUI_STATE_CONFIG);
  gui_last_state = GUI_STATE_OPTIONS;
}

void gui_newgame_on_click(float xfloat yint button)
{
  extern int game_init;

  game_state = GAME_STATE_PLAYING;
  SDL_ShowCursor(0);
  SDL_WM_GrabInput(SDL_GRAB_ON);
  gui_switch_state(GUI_STATE_INGAME);
  game_init = true;
}

void gui_resumegame_on_click(float xfloat yint button)
{
  CON_addCommand("gamemenu");
}

void gui_exitgame_on_click(float xfloat yint button)
{
  // todo: add 'are you sure?  y/n' box

  gui_destroy_all_elements(gui_element_list);
  gui_element_list = 0;
  exit(1);
}

void gui_switch_state(int state)
{
  gui_state = state;

  gui_destroy_all_elements(gui_element_list);
  gui_element_list = 0;

  switch(state)
  {
    case GUI_STATE_MAINMENU:
      gui_init_startup();
      break;
    case GUI_STATE_OPTIONS:
      gui_init_options();
      break;
    case GUI_STATE_CONFIG:
      gui_init_config();
      break;
    case GUI_STATE_INGAME:
      gui_init_ingamemenu();
      break;
    default:
      CON_printf("Warning: unknown gui_state %d"state);
  }
}