/*
 * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
 *
 * Solarus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Solarus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include "solarus/audio/Sound.h"
#include "solarus/core/CurrentQuest.h"
#include "solarus/core/MainLoop.h"
#include "solarus/core/ResourceProvider.h"
#include "solarus/core/Savegame.h"
#include "solarus/lua/LuaBind.h"
#include "solarus/lua/LuaContext.h"
#include "solarus/lua/LuaTools.h"

namespace Solarus {

/**
 * Name of the Lua table representing the sound module.
 */
const std::string LuaContext::sound_module_name = "sol.sound";

/**
 * \brief Returns whether a value is a userdata of type sound.
 * \param l A Lua context.
 * \param index An index in the stack.
 * \return true if the value at this index is a sound.
 */
bool LuaContext::is_sound(lua_State* l, int index) {
  return is_userdata(l, index, sound_module_name);
}

/**
 * \brief Checks that the userdata at the specified index of the stack is a
 * sound and returns it.
 * \param l A Lua context.
 * \param index An index in the stack.
 * \return The sound.
 */
SoundPtr LuaContext::check_sound(lua_State* l, int index) {
  return std::static_pointer_cast<Sound>(check_userdata(
      l, index, sound_module_name
  ));
}

/**
 * \brief Pushes a sound userdata onto the stack.
 * \param l A Lua context.
 * \param sound A sound.
 */
void LuaContext::push_sound(lua_State* l, Sound& sound) {
  push_userdata(l, sound);
}

/**
 * \brief Implementation of sol.sound.create().
 * \param l The Lua context that is calling this function.
 * \return Number of values to return to Lua.
 */

static SoundPtr create(LuaContext& lua_context) {
  lua_State* l = lua_context.get_internal_state();

  bool use_default_audio_context = true;
  auto audio_context_id = AudioContext::Identifier::MAIN;

  if (CurrentQuest::is_format_at_least({ 2, 1 })) {
    // Check the presence of a context passed as first argument
    if (LuaContext::is_main(l, 1)) {
      use_default_audio_context = false;
      audio_context_id = AudioContext::Identifier::MAIN;
    } else if (LuaContext::is_game(l, 1)) {
      use_default_audio_context = false;
      audio_context_id = AudioContext::Identifier::GAME;
    } else if (LuaContext::is_map(l, 1)) {
      use_default_audio_context = false;
      audio_context_id = AudioContext::Identifier::MAP;
    }
  }
  
  if (use_default_audio_context) {
    // Push the main context in the first argument
    LuaContext::push_main(l);
    lua_insert(l, 1);
  }

  const std::string& sound_id = LuaTools::check_string(l, 2);
  const bool language_specific = LuaTools::opt_boolean(l, 3, false);
  SoundBuffer& sound_buffer = lua_context.get_main_loop().get_resource_provider().get_sound(sound_id, language_specific);
  SoundPtr sound = Sound::create(sound_buffer);
  sound->set_audio_context_id(audio_context_id);

  return sound;
}

/**
 * \brief Implementation of sol.sound.stop_all().
 * \param l The Lua context that is calling this function.
 */
static void stop_all(lua_State* l) {
  bool use_default_audio_context = true;
  auto audio_context_id = AudioContext::Identifier::MAIN;

  if (CurrentQuest::is_format_at_least({ 2, 1 })) {
    // Check the presence of a context passed as first argument
    if (LuaContext::is_main(l, 1)) {
      use_default_audio_context = false;
      audio_context_id = AudioContext::Identifier::MAIN;
    } else if (LuaContext::is_game(l, 1)) {
      use_default_audio_context = false;
      audio_context_id = AudioContext::Identifier::GAME;
    } else if (LuaContext::is_map(l, 1)) {
      use_default_audio_context = false;
      audio_context_id = AudioContext::Identifier::MAP;
    }
  }
  
  if (use_default_audio_context) {
    // Push the main context in the first argument
    LuaContext::push_main(l);
    lua_insert(l, 1);
  }

  auto audio_context = AudioContext::get_context(audio_context_id);
  audio_context->stop_all_sounds();
}

/**
 * \brief Implementation of sound:play().
 * \param l The Lua context that is calling this function.
 */
static void play(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  auto audio_context_id = sound->get_audio_context_id();
  auto audio_context = AudioContext::get_context(audio_context_id);
  audio_context->play_sound(sound);
}

/**
 * \brief Implementation of sound:stop().
 * \param l The Lua context that is calling this function.
 */
static void stop(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  auto audio_context_id = sound->get_audio_context_id();
  auto audio_context = AudioContext::get_context(audio_context_id);
  audio_context->stop_sound(sound);
}

/**
 * \brief Implementation of sound:is_playing().
 * \param l The Lua context that is calling this function.
 * \return true if the sound is playing
 */
static bool is_playing(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  return sound->is_playing();
}

/**
 * \brief Implementation of sound:is_paused().
 * \param l The Lua context that is calling this function.
 * \return true if the sound is paused by script
 */
static bool is_paused(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  return sound->is_paused_by_script();
}

/**
 * \brief Implementation of sound:set_paused().
 * \param l The Lua context that is calling this function.
 */
static void set_paused(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  bool paused = LuaTools::opt_boolean(l, 2, true);
  sound->set_paused_by_script(paused);
}

/**
 * \brief Implementation of sound:is_looped().
 * \param l The Lua context that is calling this function.
 * \return true if the sound is looped
 */
static bool is_looped(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  return sound->is_looped();
}

/**
 * \brief Implementation of sound:set_looped().
 * \param l The Lua context that is calling this function.
 */
static void set_looped(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  bool looped = LuaTools::opt_boolean(l, 2, true);
  sound->set_looped(looped);
}

/**
 * \brief Implementation of sound:get_volume().
 * \param l The Lua context that is calling this function.
 * \return The volume
 */
static int get_volume(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  return sound->get_volume();
}

/**
 * \brief Implementation of sound:set_volume().
 * \param l The Lua context that is calling this function.
 */
static void set_volume(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  const int volume = LuaTools::check_int(l, 2);
  sound->set_volume(volume);
}

/**
 * \brief Implementation of sound:get_pan().
 * \param l The Lua context that is calling this function.
 * \return The pan
 */
static int get_pan(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  const float pan = sound->get_pan();
  return pan;
}

/**
 * \brief Implementation of sound:set_pan().
 * \param l The Lua context that is calling this function.
 */
static void set_pan(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  const float pan = LuaTools::check_number(l, 2);
  sound->set_pan(pan);
}

/**
 * \brief Implementation of sound:get_pitch().
 * \param l The Lua context that is calling this function.
 * \return The pitch
 */
static float get_pitch(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  const float pitch = sound->get_pitch();
  return pitch;
}

/**
 * \brief Implementation of sound:set_pitch().
 * \param l The Lua context that is calling this function.
 */
static void set_pitch(lua_State* l) {
  auto sound = LuaContext::check_sound(l, 1);
  const float pitch = LuaTools::check_number(l, 2);
  sound->set_pitch(pitch);
}

/**
 * \brief Initializes the sound features provided to Lua.
 */
void LuaContext::register_sound_module() {

  if (!CurrentQuest::is_format_at_least({ 2, 0 })) {
    return;
  }

  // Functions of sol.sound.
  const std::vector<luaL_Reg> functions = {
    { "create", LUA_TO_C_BIND(create) },
    { "stop_all", LUA_TO_C_BIND(stop_all) },
  };

  // Methods of the sound type.
  const std::vector<luaL_Reg> methods = {
    { "play", LUA_TO_C_BIND(play) },
    { "stop", LUA_TO_C_BIND(stop) },
    { "is_playing", LUA_TO_C_BIND(is_playing) },
    { "is_paused", LUA_TO_C_BIND(is_paused) },
    { "set_paused", LUA_TO_C_BIND(set_paused) },
    { "is_looped", LUA_TO_C_BIND(is_looped) },
    { "set_looped", LUA_TO_C_BIND(set_looped) },
    { "get_volume", LUA_TO_C_BIND(get_volume) },
    { "set_volume", LUA_TO_C_BIND(set_volume) },
    { "get_pan", LUA_TO_C_BIND(get_pan) },
    { "set_pan", LUA_TO_C_BIND(set_pan) },
    { "get_pitch", LUA_TO_C_BIND(get_pitch) },
    { "set_pitch", LUA_TO_C_BIND(set_pitch) },
  };

  const std::vector<luaL_Reg> metamethods = {
    { "__gc", userdata_meta_gc },
    { "__newindex", userdata_meta_newindex_as_table },
    { "__index", userdata_meta_index_as_table },
  };

  register_type(sound_module_name, functions, methods, metamethods);
}

}
