/* bzflag
 * Copyright (c) 1993-2017 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/* interface header */
#include "AudioMenu.h"

/* system implementation headers */
#include <string>
#include <vector>

/* common implementation headers */
#include "TextUtils.h"
#include "FontManager.h"

/* local implementation headers */
#include "StateDatabase.h"
#include "MainMenu.h"
#include "sound.h"
#include "HUDuiControl.h"
#include "HUDuiLabel.h"
#include "HUDuiList.h"
#include "HUDui.h"

AudioMenu::AudioMenu()
{
  // add controls
  std::vector<HUDuiControl*>& listHUD = getControls();

  HUDuiLabel* label = new HUDuiLabel;
  label->setFontFace(MainMenu::getFontFace());
  label->setString("Audio Settings");
  listHUD.push_back(label);

  HUDuiList* option = new HUDuiList;

  option = new HUDuiList;
  std::vector<std::string>* options;

  // Sound Volume
  option = new HUDuiList;
  option->setFontFace(MainMenu::getFontFace());
  option->setLabel("Sound Volume:");
  option->setCallback(callback, "s");
  options = &option->getList();
  if (isSoundOpen()) {
    options->push_back(std::string("Off"));
    option->createSlider(10);
  } else {
    options->push_back(std::string("Unavailable"));
  }
  option->update();
  listHUD.push_back(option);

/* Right now only SDL_Media has a setDriver function.
   Disable driver selection for others as it gets saved in config
   and can screw things up if you switch from non-SDL to SDL build.
   If more platforms get setDriver functions, they can be added. */

#ifdef HAVE_SDL
  // Driver
  std::string currentDriver = BZDB.get("audioDriver");
  driver = new HUDuiTypeIn;
  driver->setFontFace(MainMenu::getFontFace());
  driver->setLabel("Driver:");
  driver->setMaxLength(10);
  driver->setString(currentDriver);
  listHUD.push_back(driver);

  // Device
  std::string currentDevice = BZDB.get("audioDevice");
  device = new HUDuiTypeIn;
  device->setFontFace(MainMenu::getFontFace());
  device->setLabel("Device:");
  device->setMaxLength(10);
  device->setString(currentDevice);
  listHUD.push_back(device);
#else
  driver = NULL;
  device = NULL;
#endif // HAVE_SDL

  // Remotes Sounds
  option = new HUDuiList;
  option->setFontFace(MainMenu::getFontFace());
  option->setLabel("Remote Sounds:");
  option->setCallback(callback, "r");
  options = &option->getList();
  options->push_back(std::string("Off"));
  options->push_back(std::string("On"));
  option->update();
  listHUD.push_back(option);

  initNavigation(listHUD, 1, listHUD.size() - 1);
}

AudioMenu::~AudioMenu()
{
}

void			AudioMenu::execute()
{
  HUDuiControl* _focus = HUDui::getFocus();
  if (_focus == driver) {
    BZDB.set("audioDriver", driver->getString().c_str());
  } else if (_focus == device) {
    BZDB.set("audioDevice", device->getString().c_str());
  }
}

void			AudioMenu::resize(int _width, int _height)
{
  HUDDialog::resize(_width, _height);
  int i;

  // use a big font for title, smaller font for the rest
  const float titleFontSize = (float)_height / 15.0f;
  const float fontSize = (float)_height / 45.0f;
  FontManager &fm = FontManager::instance();
  int fontFace = MainMenu::getFontFace();

  // reposition title
  std::vector<HUDuiControl*>& listHUD = getControls();
  HUDuiLabel* title = (HUDuiLabel*)listHUD[0];
  title->setFontSize(titleFontSize);
  const float titleWidth = fm.getStrLength(fontFace, titleFontSize, title->getString());
  const float titleHeight = fm.getStrHeight(fontFace, titleFontSize, " ");
  float x = 0.5f * ((float)_width - titleWidth);
  float y = (float)_height - titleHeight;
  title->setPosition(x, y);

  // reposition options
  x = 0.5f * ((float)_width);
  y -= 0.6f * titleHeight;
  const float h = fm.getStrHeight(fontFace, fontSize, " ");
  const int count = listHUD.size();
  for (i = 1; i < count; i++) {
    listHUD[i]->setFontSize(fontSize);
    listHUD[i]->setPosition(x, y);
    y -= 1.0f * h;
  }

  i = 1;
  // sound
  ((HUDuiList*)listHUD[i++])->setIndex(getSoundVolume());
#ifdef HAVE_SDL
  i++; // driver
  i++; // device
#endif // HAVE_SDL
  ((HUDuiList*)listHUD[i++])->setIndex(BZDB.isTrue("remoteSounds") ? 1 : 0);
}

void			AudioMenu::callback(HUDuiControl* w, const void* data) {
  HUDuiList* list = (HUDuiList*)w;
  switch (((const char*)data)[0]) {
    case 's':
      BZDB.set("volume", TextUtils::format("%d", list->getIndex()));
      setSoundVolume(list->getIndex());
      break;
    case 'r':
      BZDB.setBool("remoteSounds", (list->getIndex() == 0) ? false : true);
      break;
  }
}


// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8
