#include "allegro.h"
#include "gameConstants.h"
#include "GameSettings.h"
#include "StartGameConfig.h"
#include "InputObj.h"
#include "MusicSound.h"

GameSettings :: GameSettings ()
{
  resX = 640;
  resY = 480;
  colorDepth = 16;
  gfxDriver = GFX_AUTODETECT_WINDOWED;
}

int GameSettings :: init (InputObj * iO, MusicSound * ms, StartGameConfig * sgc)
{
  //grab stuff from files
  //returns 0 on sucess.

  int res = 0;

  if (loadGraphicsSettings () != 0)
    return res;

  if (loadControls (iO) != 0)
    return res;

  if (loadVolume (ms) != 0)
    return res;

  return res;
}

int GameSettings :: loadGraphicsSettings ()
{
  //loads graphics settings from files.
  //returns 0 on sucess.

  set_config_file ("data/gameSettings");

  resX = get_config_int ("Gfx", "ResX", 640);
  resY = get_config_int ("Gfx", "ResY", 480);
  colorDepth = get_config_int ("Gfx", "Depth", 24);
  gfxDriver = get_config_int ("Gfx", "Driver", GFX_AUTODETECT_WINDOWED);

  return 0;
}

int GameSettings :: saveGraphicsSettings ()
{
  //loads graphics settings from files.
  //returns 0 on sucess.

  set_config_file ("data/gameSettings");
  set_config_int ("Gfx", "ResX", resX);
  set_config_int ("Gfx", "ResY", resY);
  set_config_int ("Gfx", "Depth", colorDepth);
  set_config_int ("Gfx", "Driver", gfxDriver);
  return 0;
}

int GameSettings :: loadControls (InputObj * iO)
{
  set_config_file ("data/gameSettings");

  iO -> setKeyMapping (keyUp, get_config_int ("Controls", "Up", keyBindUp));

  iO -> setKeyMapping (keyDown, get_config_int ("Controls", "Down", keyBindDown));
  iO -> setKeyMapping (keyLeft, get_config_int ("Controls", "Left", keyBindLeft));
  iO -> setKeyMapping (keyRight, get_config_int ("Controls", "Right", keyBindRight));
  iO -> setKeyMapping (keyHyper, get_config_int ("Controls", "Hyper", keyBindHyper));
  iO -> setKeyMapping (keyRestart, get_config_int ("Controls", "Restart", keyBindRestart));
  iO -> setKeyMapping (keyPause, get_config_int ("Controls", "Pause", keyBindPause));
  iO -> setKeyMapping (keyMenu, get_config_int ("Controls", "Menu", keyBindMenu));
  iO -> setKeyMapping (keyTaunt, get_config_int ("Controls", "Taunt", keyBindTaunt));
  iO -> setMouseControlThreshold (get_config_int ("Controls", "MouseThreshold", 30));
  return 0;
}

int GameSettings :: saveControls (InputObj * iO)
{
  vector <int> * vi = iO -> getKeyMappings ();

  set_config_file ("data/gameSettings");
  set_config_int ("Controls", "Up", vi -> at (keyUp));
  set_config_int ("Controls", "Down", vi -> at (keyDown));
  set_config_int ("Controls", "Left", vi -> at (keyLeft));
  set_config_int ("Controls", "Right", vi -> at (keyRight));
  set_config_int ("Controls", "Hyper", vi -> at (keyHyper));
  set_config_int ("Controls", "Restart", vi -> at (keyRestart));
  set_config_int ("Controls", "Pause", vi -> at (keyPause));
  set_config_int ("Controls", "Menu", vi -> at (keyMenu));
  set_config_int ("Controls", "Taunt", vi -> at (keyTaunt));

  set_config_int ("Controls", "MouseThreshold", iO -> getMouseControlThreshold ());
  return 0;
}

int GameSettings :: loadVolume (MusicSound * ms)
{
  //loads graphics settings from files.
  //returns 0 on sucess.

  set_config_file ("data/gameSettings");
  ms -> setVolumeGlobal (get_config_int ("Sound", "Global", 128));
  ms -> setVolumeMusic (get_config_int ("Sound", "Music", 128));
  ms -> setVolumeSfx (get_config_int ("Sound", "Sfx", 128));
  return 0;
}

int GameSettings :: saveVolume (MusicSound * ms)
{
  //loads graphics settings from files.
  //returns 0 on sucess.

  set_config_file ("data/gameSettings");
  set_config_int ("Sound", "Global", ms -> getVolumeGlobal ());
  set_config_int ("Sound", "Music", ms -> getVolumeMusic ());
  set_config_int ("Sound", "Sfx", ms -> getVolumeSfx ());

  return 0;
}

int GameSettings :: getColorDepth ()
{ return colorDepth;}

int GameSettings :: getResX ()
{ return resX;}

int GameSettings :: getResY ()
{ return resY;}

int GameSettings :: getGfxDriver ()
{ return gfxDriver;}

void GameSettings :: setGfxSettings (int depth, int driver, int resx, int resy)
{ colorDepth = depth; gfxDriver = driver; resX = resx; resY = resy; }

GameSettings :: ~GameSettings ()
{
}

int GameSettings :: loadStartSettings ()
{
  set_config_file ("data/gameSettings");
  StartGameConfig::settingsIndexChar = get_config_int ("StartGame", "pc", 0);
  StartGameConfig::settingsIndexCamp = get_config_int ("StartGame", "camp", 0);
  return 0;
}

int GameSettings :: saveStartSettings ()
{
  set_config_file ("data/gameSettings");
  set_config_int ("StartGame", "pc", StartGameConfig::settingsIndexChar);
  set_config_int ("StartGame", "camp", StartGameConfig::settingsIndexCamp);
  return 0;
}
