#include <stdio.h>
#include "allegro.h"
#include "Config.h"
#include "gameConstants.h"

#include "ControlConfig.h"
#include "GfxSoundConfig.h"
#include "StartGameConfig.h"
#include "GameSettings.h"
#include "Loader.h"
#include "tasmMath.h"

ControlConfig * Config :: cc = NULL;
GfxSoundConfig * Config :: gsc = NULL;
StartGameConfig * Config :: sgc = NULL;

int Config :: configBgColorR = 192;
int Config :: configBgColorB = 192;
int Config :: configBgColorG = 192;

int Config :: configFgColorR = 0;
int Config :: configFgColorB = 0;
int Config :: configFgColorG = 0;

int Config :: mainMsg = mainMenuQuit;

DIALOG Config :: mainMenuDialog [] = 
  {
    { d_clear_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
    { my_button_proc, 100, 20, 100, 40, 0, 0, 0, D_EXIT, 0, 0, (char *)"Start", NULL, (void *)startGame},
    { my_button_proc, 10, 90, 80, 20, 0, 0, 0, D_EXIT, 0, 0, (char *)"Controls", NULL, (void *)startControl},
    { my_button_proc, 210, 90, 80, 20, 0, 0, 0, D_EXIT, 0, 0, (char *)"Vid/Aud", NULL, (void *)startGFX},
    { my_button_proc, 100, 70, 100, 40, 0, 0, 0, D_EXIT, 0, 0, (char *)"Quit", NULL, (void *)quitGame},
    { d_text_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (char *)"TASM demo v034...by Chad Serrant", NULL, NULL },
    { d_textbox_proc, 0, 130, 300, 100, 0, 0, 0, 0, 0, 0, (char *)"ESC = exit, TAB = pause, F10 = restart round.\n\nWASD = move, Mouse Buttons & Spacebar = attacks.\n\nAttacks use up your juice. Hit enemies and bump them into each other to get juice back.", NULL, NULL },
    { d_yield_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL},
    { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL  }
  };

void Config :: ConfigResetMenu ()
{
  set_dialog_color (mainMenuDialog, 
		    makecol (configFgColorR, configFgColorB, configFgColorG),
		    makecol (configBgColorR, configBgColorB, configBgColorG));

  centre_dialog (mainMenuDialog);
  dialog_message (mainMenuDialog, MSG_DRAW, 0, NULL);
}

Config :: Config ()
{

}

//controls all other config menus
//tells main when the game is ready
//sets up settings
//saves settings
//recolors all menus when graphicsConfig is done

int Config :: initialize (GameSettings * gset, InputObj * iO, MusicSound * ms, Loader * lder)
{
  //initalizes settings found in other loaders (or defaults if they have none.)
  //return 0 if successful.
  //return -1 if failure.

  //set up the subconfig menus
  cc = new ControlConfig (iO, gset);
  gsc = new GfxSoundConfig (gset, ms);
  sgc = new StartGameConfig (gset, lder);

  //load graphics settings
  gs = gset;

  //set graphics settings
  if (initGfx () != 0)
    return -1;

  if (initControls () != 0)
    return -1;

  return 0;
}

int Config :: configLoop ()
{
  //start config menu here

  int tempStub = 0;

  ConfigResetMenu ();

  while (tempStub < 1)
    {
      //so that stray keys held down from before don't screw this up...
      clear_keybuf ();
      do_dialog(mainMenuDialog, 1);

      if (key[KEY_ESC])
	mainMsg = mainMenuQuit;

      tempStub ++;
    }

  return mainMsg;
}

int Config :: initGfx ()
{
  //initializes graphics settings, retrieved from GameSettings
  //returns 0 on sucess.

  set_color_depth (gs -> getColorDepth ());

  if (set_gfx_mode(gs -> getGfxDriver(), gs -> getResX (), gs -> getResY (), 0, 0) != 0) {
    if (set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
      return 1;
    }}
  return 0;
}

int Config :: initControls ()
{
  //inits controls, grabbed from gameSettings
  //returns 0 on sucess.

  //insert defaults here!!!
  return 0;
}

//general functions
int Config :: my_button_proc (int msg, DIALOG *d, int c)
{
   int ret = d_button_proc (msg, d, c);
   if (ret == D_CLOSE && d->dp3)
      return ((int (*)(void))d->dp3)();
   return ret;
}

Config :: ~Config ()
{
}

//mainMenu
int Config :: quitGame(void)
{
  //tells TASM to quit.
  mainMsg = mainMenuQuit;
  return D_CLOSE;
}

int Config :: startGame(void)
{
  sgc -> go (8, &mainMsg);

  if (mainMsg == mainMenuStopMode)
    return D_CLOSE;

  ConfigResetMenu ();
  show_mouse (screen);

  return D_O_K;
}

int Config :: startControl(void)
{
  cc -> go (1);

  ConfigResetMenu ();
  show_mouse (screen);

  return D_O_K;
}

int Config :: startGFX(void)
{
  gsc -> go (1);

  ConfigResetMenu ();
  show_mouse (screen);
  return D_O_K;
}
