#pragma once
#include "squirrel.h"
#include "sqstdaux.h"
#include "sqstdblob.h"
#include "sqstdio.h"
#include "sqstdmath.h"
#include "sqstdstring.h"
#include "sqstdsystem.h"
#include <iostream>
#include <map>

typedef void *(*__G_GameWorld)();
static SQInteger Get_GameWorld(HSQUIRRELVM v)
{
    void *World = ((__G_GameWorld)0x080DA3A7)();
    sq_pushuserpointer(v, World);
    return 1;
}
typedef int (*__GetSessionByUid)(void *World, int Uid);
static SQInteger GameWorld_GetSessionByUid(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQInteger Uid;
    sq_getinteger(v, 3, &Uid);

    SQInteger Session = ((__GetSessionByUid)0x086C4C24)((void *)P, Uid);
    sq_pushinteger(v, Session);
    return 1;
}
typedef void *(*__GetUserBySession)(void *World, int Session);
static SQInteger GameWorld_GetUserBySession(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQInteger Session;
    sq_getinteger(v, 3, &Session);

    void *User = ((__GetUserBySession)0x086C4B9C)((void *)P, Session);
    sq_pushuserpointer(v, User);
    return 1;
}
typedef void *(*__GetUserByUid)(void *World, int Uid);
static SQInteger GameWorld_GetUserByUid(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQInteger Uid;
    sq_getinteger(v, 3, &Uid);

    void *User = ((__GetUserByUid)0x086C4D40)((void *)P, Uid);
    sq_pushuserpointer(v, User);
    return 1;
}
typedef void *(*__GetUserByName)(void *World, char *Name);
static SQInteger GameWorld_GetUserByName(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    const SQChar *Name;
    sq_getstring(v, 3, &Name);

    void *User = ((__GetUserByName)0x086C9464)((void *)P, (char *)Name);
    sq_pushuserpointer(v, User);
    return 1;
}
typedef int (*__GetUserCount)(void *World);
static SQInteger GameWorld_GetUserCount(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);

    SQInteger Count = ((__GetUserCount)0x086C4550)((void *)P);
    sq_pushinteger(v, Count);
    return 1;
}
typedef void (*__SendAll)(void *World, void *Pack);
static SQInteger GameWorld_SendAll(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQUserPointer Pck;
    sq_getuserpointer(v, 3, &Pck);

    ((__SendAll)0x086C8C14)((void *)P, (void *)Pck);
    return 0;
}
typedef void (*__SendPartyInfoToAll)(void *World, void *CParty, int code);
static SQInteger GameWorld_SendPartyInfoToAll(HSQUIRRELVM v)
{
    SQUserPointer World;
    sq_getuserpointer(v, 2, &World);
    SQUserPointer CParty;
    sq_getuserpointer(v, 3, &CParty);

    ((__SendPartyInfoToAll)0x086C878A)((void *)World, (void *)CParty, 0);
    return 0;
}
// typedef std::map<int, std::string> (*__GetUserIdList)(&std::vector<unsigned short> a1, int a2);
// static SQInteger GetUserIdList(HSQUIRRELVM v)
// {
//     SQInteger a1, a2;
//     sq_getinteger(v, 2, &a1);
//     sq_getinteger(v, 3, &a2);
//     std::vector<unsigned short> Qs;
//     std::map<int, std::string> Ret = ((__GetUserIdList)0x086C305E)(&Qs, a2);

//     std::cout << Qs.size() << std::endl;
//     std::cout << Ret.size() << std::endl;
//     return 0;
// }
typedef void *(*__WorldMoveArea)(void *World, void *User, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11);
static SQInteger GameWorld_MoveArea(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQUserPointer Player;
    sq_getuserpointer(v, 3, &Player);
    SQInteger TownIndex, AreaIndex, Xpos, Ypos;
    sq_getinteger(v, 4, &TownIndex);
    sq_getinteger(v, 5, &AreaIndex);
    sq_getinteger(v, 6, &Xpos);
    sq_getinteger(v, 7, &Ypos);

    ((__WorldMoveArea)0x86C5A84)((void *)P, (void *)Player, TownIndex, AreaIndex, Xpos, Ypos, 0, 0, 0, 0, 0);
    return 0;
}
typedef void *(*__SendDungeonInOut)(void *World, void *User, int a3, bool a4);
static SQInteger GameWorld_SendDungeonInOut(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQUserPointer Player;
    sq_getuserpointer(v, 3, &Player);
    SQInteger Index;
    SQBool Model;
    sq_getinteger(v, 4, &Index);
    sq_getbool(v, 5, &Model);

    ((__SendDungeonInOut)0x86C8FC8)((void *)P, (void *)Player, Index, Model);
    return 0;
}

static SQInteger register_World_func(HSQUIRRELVM v, SQFUNCTION f, const char *fname)
{
    sq_pushroottable(v);
    sq_pushstring(v, fname, -1);
    sq_newclosure(v, f, 0); // create a new function
    sq_newslot(v, -3, SQFalse);
    sq_pop(v, 1); // pops the root table
}

static void RegisterWorld(HSQUIRRELVM v)
{
    // 获取游戏世界
    register_World_func(v, Get_GameWorld, _SC("Sq_Get_GameWorld"));
    // 根据UID获取Session
    register_World_func(v, GameWorld_GetSessionByUid, _SC("Sq_GameWorld_GetSessionByUid"));
    // 根据Session获取玩家
    register_World_func(v, GameWorld_GetUserBySession, _SC("Sq_GameWorld_GetUserBySession"));
    // 根据UID获取玩家
    register_World_func(v, GameWorld_GetUserByUid, _SC("Sq_GameWorld_GetUserByUid"));
    // 根据名字获取玩家
    register_World_func(v, GameWorld_GetUserByName, _SC("Sq_GameWorld_GetUserByName"));
    // 获取玩家数量
    register_World_func(v, GameWorld_GetUserCount, _SC("Sq_GameWorld_GetUserCount"));
    // 给所有玩家发包
    register_World_func(v, GameWorld_SendAll, _SC("Sq_GameWorld_SendAll"));
    // 指定玩家移动
    register_World_func(v, GameWorld_MoveArea, _SC("Sq_GameWorld_MoveArea"));
    // 给所有玩家发送队伍包
    register_World_func(v, GameWorld_SendPartyInfoToAll, _SC("Sq_GameWorld_SendPartyInfoToAll"));
    // 副本开启或关闭消息
    register_World_func(v, GameWorld_SendDungeonInOut, _SC("Sq_GameWorld_SendDungeonInOut"));

    // register_World_func(v, GetUserIdList, _SC("Sq_GetUserIdList"));
}