/*
 * Copyright (C) 2006-2025 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/Music.h"
#include "solarus/audio/MusicSystem.h"
#include "solarus/core/CurrentQuest.h"
#include "solarus/core/MainLoop.h"
#include "solarus/core/ResourceProvider.h"
#include "solarus/lua/LuaBind.h"
#include "solarus/lua/LuaContext.h"
#include "solarus/lua/LuaTools.h"

namespace Solarus {

/**
 * \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_music(lua_State* l, int index) {
  return is_userdata(l, index, music_module_name);
}

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

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

/**
 * \brief Implementation of sol.music.create().
 * \param l The Lua context that is calling this function.
 * \return The created music object
 */
static MusicPtr create(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);
  }

  const std::string& music_id = LuaTools::check_string(l, 2);
  MusicPtr music = Music::create(music_id);
  music->set_audio_context_id(audio_context_id);
  
  return music;
}

/**
 * \brief Implementation of music:play().
 * \param l The Lua context that is calling this function.
 * \return Number of values to return to Lua.
 */
static void play(lua_State* l) {
  auto music = LuaContext::check_music(l, 1);
  auto audio_context_id = music->get_audio_context_id();
  auto audio_context = AudioContext::get_context(audio_context_id);
  audio_context->play_music(music);
}

/**
 * \brief Implementation of music:stop().
 * \param l The Lua context that is calling this function.
 * \return Number of values to return to Lua.
 */
static void stop(lua_State* l) {
  auto music = LuaContext::check_music(l, 1);
  auto audio_context_id = music->get_audio_context_id();
  auto audio_context = AudioContext::get_context(audio_context_id);
  audio_context->stop_music(music);
}

/**
 * \brief Implementation of music:is_paused().
 * \param l The Lua context that is calling this function.
 * \return Number of values to return to Lua.
 */
static bool is_paused(lua_State* l) {
  auto music = LuaContext::check_music(l, 1);
  return music->is_paused();
}

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

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

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

/**
 * \brief Implementation of music:get_channel_volume).
 * \param l The Lua context that is calling this function.
 * \return The channel volume
 */
static int get_channel_volume(lua_State* l) {
  auto music = LuaContext::check_music(l, 1);
  int channel_index = LuaTools::check_int(l, 2);
  return music->get_channel_volume(channel_index);
}

/**
 * \brief Implementation of music:set_channel_volume().
 * \param l The Lua context that is calling this function.
 */
static void set_channel_volume(lua_State* l) {
  auto music = LuaContext::check_music(l, 1);
  int channel_index = LuaTools::check_int(l, 2);
  int pan = LuaTools::check_int(l, 3);
  music->set_channel_volume(channel_index, pan);
}

/**
 * \brief Implementation of music:get_channel_pan().
 * \param l The Lua context that is calling this function.
 * \return The channel pan
 */
static int get_channel_pan(lua_State* l) {
  auto music = LuaContext::check_music(l, 1);
  int channel_index = LuaTools::check_int(l, 2);
  return music->get_channel_pan(channel_index);
}

/**
 * \brief Implementation of music:set_channel_pan().
 * \param l The Lua context that is calling this function.
 */
static void set_channel_pan(lua_State* l) {
  auto music = LuaContext::check_music(l, 1);
  int channel_index = LuaTools::check_int(l, 2);
  int pan = LuaTools::check_int(l, 3);
  music->set_channel_pan(channel_index, pan);
}

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

void LuaContext::register_music_module() {
  // Functions of sol.music.
  const std::vector<luaL_Reg> functions = {
    { "create", LUA_TO_C_BIND(create) },
  };

  // Methods of the music type.
  const std::vector<luaL_Reg> methods = {
    { "play", LUA_TO_C_BIND(play) },
    { "stop", LUA_TO_C_BIND(stop) },
    { "is_paused", LUA_TO_C_BIND(is_paused) },
    { "set_paused", LUA_TO_C_BIND(set_paused) },
    { "get_volume", LUA_TO_C_BIND(get_volume) },
    { "set_volume", LUA_TO_C_BIND(set_volume) },
    { "get_channel_volume", LUA_TO_C_BIND(get_channel_volume) },
    { "set_channel_volume", LUA_TO_C_BIND(set_channel_volume) },
    { "get_channel_pan", LUA_TO_C_BIND(get_channel_pan) },
    { "set_channel_pan", LUA_TO_C_BIND(set_channel_pan) },
  };

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

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

}