#include "allegro.h"

#include "StartGameConfig.h"
#include "GameSettings.h"
#include "gameConstants.h"
#include "Loader.h"

#include <vector>
#include <string>
using namespace std;

GameSettings * StartGameConfig :: gs = NULL;
Loader * StartGameConfig :: l = NULL;
int * StartGameConfig :: mainMsg = NULL;
int StartGameConfig :: bgColorR = 160;
int StartGameConfig :: bgColorB = 160;
int StartGameConfig :: bgColorG = 160;

vector <string *> * StartGameConfig :: campNames = new vector <string *> (0);
vector <string *> * StartGameConfig :: campDesc = new vector <string *> (0);

vector <string *> * StartGameConfig :: charNames = new vector <string *> (0);
vector <string *> * StartGameConfig :: charDesc = new vector <string *> (0);

int StartGameConfig :: settingsIndexChar = 0;
int StartGameConfig :: settingsIndexCamp = 0;

DIALOG StartGameConfig :: dlog [] =
  {
    { d_clear_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
    { d_box_proc, 0, 0, 600, 420, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
    { d_ctext_proc, 120, 15, 0, 0, 0, 0, 0, 0, 0, 0, (char *)"Campaign", NULL, NULL },
    { my_list_proc, 10, 30, 220, 260, 0, 0, 0, 0, 0, 0, (void *)getCampNames, NULL,  (void *)buttonChooseCamp},
    { d_textbox_proc, 10, 300, 280, 100, 0, 0, 0, 0, 0, 0, (char *)"Intentionally Left Blank.", NULL, NULL },

    { d_ctext_proc, 480, 15, 0, 0, 0, 0, 0, 0, 0, 0, (char *)"Character", NULL, NULL },
    { my_list_proc, 370, 30, 220, 260, 0, 0, 0, 0, 0, 0, (void *)getCharNames, NULL,  (void *)buttonChooseChar},
    { d_textbox_proc, 310, 300, 280, 100, 0, 0, 0, 0, 0, 0, (char *)"Intentionally Left Blank.", NULL, NULL },

    { my_button_proc, 250, 40, 100, 140, 0, 0, 0, D_EXIT, 0, 0, (char *)"Start", NULL, (void *)startGame},
    { my_button_proc, 250, 250, 100, 40, 0, 0, 0, D_EXIT, 0, 0, (char *)"Back to main", NULL, (void *)toMain},

    { 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  }
  };

StartGameConfig :: StartGameConfig (GameSettings * gset, Loader * lder)
{
  gs = gset;
  l = lder;
}

void StartGameConfig :: go (int index, int * mm)
{
  mainMsg = mm;

  campNames -> clear ();
  campDesc -> clear ();
  charNames -> clear ();
  charDesc -> clear ();

  //set indecies here
  l -> loadCharAndCamp (this);
  gs -> loadStartSettings ();

  dlog[3].d1 = settingsIndexCamp;
  chooseCamp (false);

  dlog[6].d1 = settingsIndexChar;
  chooseChar (false);

  redrawMenu ();
  show_mouse (screen);
  do_dialog (dlog, index);
}

StartGameConfig :: ~StartGameConfig ()
{
}

void StartGameConfig :: redrawMenu ()
{
  set_dialog_color (dlog, 
		    makecol (0, 0, 0),
		    makecol (bgColorR, bgColorB, bgColorG));

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

int StartGameConfig :: 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;
}

int StartGameConfig :: my_list_proc (int msg, DIALOG * d, int c)
{
   int ret = d_list_proc (msg, d, c);
   if ((msg == MSG_CLICK || msg == MSG_CHAR) && d->dp3)
     return ((int (*)(void))d->dp3)();
   return ret;
}

int StartGameConfig :: startGame(void)
{
  //tells TASM to switch modes
  *mainMsg = mainMenuStopMode;
  gs -> saveStartSettings ();
  return D_CLOSE;
}

int StartGameConfig :: toMain ()
{
  //tell system to save settings.
  *mainMsg = mainMenuContinue;
  gs -> saveStartSettings ();
  return D_CLOSE;
}

//helper functions for the list boxes boxes
char * StartGameConfig :: getCharNames (int index, int *list_size)
{
  if (index < 0)
    {*list_size = charNames -> size (); return NULL;}

  if (charNames -> size () <= 0)
    {*list_size = 0; return NULL;}

  if (index >= (int) charNames -> size ())
    {
      *list_size = charNames -> size (); return NULL;
    }

  return (char *)charNames -> at (index) -> c_str ();
}

char * StartGameConfig :: getCampNames (int index, int *list_size)
{
  if (index < 0)
    {*list_size = campNames -> size (); return NULL;}

  if (campNames -> size () <= 0)
    {*list_size = 0; return NULL;}

  if (index >= (int) campNames -> size ())
    {
      *list_size = campNames -> size (); return NULL;
    }

  return (char *)campNames -> at (index) -> c_str ();
}

void StartGameConfig :: addCampName (string name, string desc)
{
  campNames -> push_back (new string (name));
  campDesc -> push_back (new string (desc));
}

void StartGameConfig :: addCharName (string name, string desc)
{
  charNames -> push_back (new string (name));
  charDesc -> push_back (new string (desc));
}

void StartGameConfig :: buttonChooseCamp (void)
{ chooseCamp (true); }

void StartGameConfig :: buttonChooseChar (void)
{ chooseChar (true); }

void StartGameConfig :: chooseCamp (bool drawIt)
{
  int index = dlog[3].d1;

  if (index < 0 || index >= (int)campDesc -> size ())
    {dlog[3].d1 = 0; settingsIndexCamp = 0; index = 0;}

  settingsIndexCamp = index;

  //updates the descriptor.
  dlog[4].dp = (void *)campDesc -> at (index) -> c_str ();
  //redraw
  if (drawIt)
    dialog_message (&(dlog[4]), MSG_DRAW, 0, NULL);
}

void StartGameConfig :: chooseChar (bool drawIt)
{
  int index = dlog[6].d1;

  if (index < 0 || index >= (int)charDesc -> size ())
    {dlog[6].d1 = 0; settingsIndexChar = 0; index = 0;}
  settingsIndexChar = index;

  //updates the descriptor.
  dlog[7].dp = (void *)charDesc -> at (index) -> c_str ();
  //redraw
  if (drawIt)
    dialog_message (&(dlog[7]), MSG_DRAW, 0, NULL);
}

string StartGameConfig :: getCampName ()
{
  return *(campNames -> at (dlog[3].d1));
}

string StartGameConfig :: getCharName ()
{
  return *(charNames -> at (dlog[6].d1));
}
