#include "ui/ScriptFunctions.hpp"
#include "db/Db.hpp"
#include "glue/CCharacterSelection.hpp"
#include "ui/CSimpleModelFFX.hpp"
#include "ui/Types.hpp"
#include "util/Lua.hpp"
#include "util/Unimplemented.hpp"
#include "glue/CGlueMgr.hpp"

int32_t Script_SetCharSelectModelFrame(lua_State* L) {
    if (!lua_isstring(L, 1)) {
        return luaL_error(L, "Usage: SetCharSelectModelFrame(\"frameName\")");
    }

    auto type = CSimpleModel::GetObjectType();
    auto name = lua_tolstring(L, 1, nullptr);
    auto frame = CScriptObject::GetScriptObjectByName(name, type);

    if (frame) {
        CCharacterSelection::s_modelFrame = static_cast<CSimpleModelFFX*>(frame);
    }

    return 0;
}

int32_t Script_SetCharSelectBackground(lua_State* L) {
    if (!lua_isstring(L, 1)) {
        return luaL_error(L, "Usage: SetCharSelectBackground(\"filename\")");
    }

    auto modelPath = lua_tolstring(L, 1, nullptr);
    CCharacterSelection::SetBackgroundModel(modelPath);

    return 0;
}

int32_t Script_GetCharacterListUpdate(lua_State* L) {
    // TODO: CCharSelectInfo::ClearCharacterModel();
    // TODO: CCharSelectInfo::ClearPetModel();
    CGlueMgr::GetCharacterList();
    return 0;
}

int32_t Script_GetNumCharacters(lua_State* L) {
    lua_pushnumber(L, CCharacterSelection::s_characterList.Count());

    return 1;
}

int32_t Script_GetCharacterInfo(lua_State* L) {
    WHOA_UNIMPLEMENTED(0);
}

int32_t Script_SelectCharacter(lua_State* L) {
    WHOA_UNIMPLEMENTED(0);
}

int32_t Script_DeleteCharacter(lua_State* L) {
    WHOA_UNIMPLEMENTED(0);
}

int32_t Script_RenameCharacter(lua_State* L) {
    WHOA_UNIMPLEMENTED(0);
}

int32_t Script_DeclineCharacter(lua_State* L) {
    WHOA_UNIMPLEMENTED(0);
}

int32_t Script_UpdateSelectionCustomizationScene(lua_State* L) {
    CCharacterSelection::RenderPrep();

    return 0;
}

int32_t Script_GetCharacterSelectFacing(lua_State* L) {
    // Radian to Degree
    lua_pushnumber(L, CCharacterSelection::s_charFacing * 57.29578f);
    return 1;
}

int32_t Script_SetCharacterSelectFacing(lua_State* L) {
    if (!lua_isnumber(L, 1)) {
        luaL_error(L, "Usage: SetCharacterSelectFacing(degrees)");
    }
    // Degree to Radian
    float facing = lua_tonumber(L, 1) * 0.017453292;
    CCharacterSelection::SetCharFacing(facing);
    return 1;
}

int32_t Script_GetSelectBackgroundModel(lua_State* L) {
    if (!lua_isnumber(L, 1)) {
        return luaL_error(L, "Usage: GetSelectBackgroundModel(index)");
    }

    auto characterIndex = static_cast<int32_t>(lua_tonumber(L, 1)) - 1;

    // TODO

    ChrRacesRec* racesRec = nullptr;

    if (characterIndex < 0 || characterIndex >= CCharacterSelection::s_characterList.Count()) {
        racesRec = g_chrRacesDB.GetRecord(2);
    } else {
        // TODO
    }

    if (racesRec) {
        lua_pushstring(L, racesRec->m_clientFileString);
    } else {
        lua_pushstring(L, "");
    }

    return 1;
}

FrameScript_Method FrameScript::s_ScriptFunctions_CharSelect[NUM_SCRIPT_FUNCTIONS_CHAR_SELECT] = {
    { "SetCharSelectModelFrame",    &Script_SetCharSelectModelFrame },
    { "SetCharSelectBackground",    &Script_SetCharSelectBackground },
    { "GetCharacterListUpdate",     &Script_GetCharacterListUpdate },
    { "GetNumCharacters",           &Script_GetNumCharacters },
    { "GetCharacterInfo",           &Script_GetCharacterInfo },
    { "SelectCharacter",            &Script_SelectCharacter },
    { "DeleteCharacter",            &Script_DeleteCharacter },
    { "RenameCharacter",            &Script_RenameCharacter },
    { "DeclineCharacter",           &Script_DeclineCharacter },
    { "UpdateSelectionCustomizationScene", &Script_UpdateSelectionCustomizationScene },
    { "GetCharacterSelectFacing",   &Script_GetCharacterSelectFacing },
    { "SetCharacterSelectFacing",   &Script_SetCharacterSelectFacing },
    { "GetSelectBackgroundModel",   &Script_GetSelectBackgroundModel }
};
