#pragma once
#include "squirrel.h"
#include "sqstdaux.h"
#include "sqstdblob.h"
#include "sqstdio.h"
#include "sqstdmath.h"
#include "sqstdstring.h"
#include "sqstdsystem.h"
#include "import.h"
#include "inline_hook.h"
#include "Singleton.h"
#include "./sdk/sdk_class.h"
#include "utils.h"
#include "GameDataManager.h"
#include "PacketBuf.h"
#include "User.h"
#include <iostream>

static SQInteger Packet_New(HSQUIRRELVM v)
{
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)PacketGuard::NewPacketGuard();
    sq_pushuserpointer(v, Pck);
    return 1;
}
static SQInteger Packet_Put_header(HSQUIRRELVM v)
{
    SQUserPointer Buf;
    sq_getuserpointer(v, 2, &Buf);
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)Buf;
    SQInteger Header1;
    sq_getinteger(v, 3, &Header1);
    SQInteger Header2;
    sq_getinteger(v, 4, &Header2);
    Pck->put_header(Header1, Header2);
    return 0;
}
static SQInteger Packet_Put_byte(HSQUIRRELVM v)
{
    SQUserPointer Buf;
    sq_getuserpointer(v, 2, &Buf);
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)Buf;
    SQInteger Byte;
    sq_getinteger(v, 3, &Byte);
    Pck->put_byte(Byte);
    return 0;
}
static SQInteger Packet_Put_short(HSQUIRRELVM v)
{
    SQUserPointer Buf;
    sq_getuserpointer(v, 2, &Buf);
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)Buf;
    SQInteger Short;
    sq_getinteger(v, 3, &Short);
    Pck->put_short(Short);
    return 0;
}
static SQInteger Packet_Put_int(HSQUIRRELVM v)
{
    SQUserPointer Buf;
    sq_getuserpointer(v, 2, &Buf);
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)Buf;
    SQInteger Int;
    sq_getinteger(v, 3, &Int);
    Pck->put_int(Int);
    return 0;
}
static SQInteger Packet_Put_binary(HSQUIRRELVM v)
{
    SQUserPointer Buf;
    sq_getuserpointer(v, 2, &Buf);
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)Buf;
    const SQChar *Str;
    sq_getstring(v, 3, &Str);
    Pck->put_str((char *)Str, strlen(Str));
    return 0;
}
static SQInteger Packet_Put_binaryex(HSQUIRRELVM v)
{
    SQUserPointer Buf;
    sq_getuserpointer(v, 2, &Buf);
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)Buf;
    SQUserPointer Str;
    sq_getuserpointer(v, 3, &Str);
    SQInteger Len;
    sq_getinteger(v, 4, &Len);
    Pck->put_binary((char *)Str, Len);
    return 0;
}
static SQInteger Packet_Finalize(HSQUIRRELVM v)
{
    SQUserPointer Buf;
    sq_getuserpointer(v, 2, &Buf);
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)Buf;
    SQBool B;
    sq_getbool(v, 3, &B);
    Pck->finalize(B);
    return 0;
}
static SQInteger Packet_Send(HSQUIRRELVM v)
{
    SQUserPointer userbuf;
    sq_getuserpointer(v, 2, &userbuf);
    CUser *user = (CUser *)userbuf;
    SQUserPointer Buf;
    sq_getuserpointer(v, 3, &Buf);
    user->Send((PacketGuard *)Buf);
    return 0;
}
static SQInteger Packet_Delete(HSQUIRRELVM v)
{
    SQUserPointer Buf;
    sq_getuserpointer(v, 2, &Buf);
    InterfacePacketBuf *Pck = (InterfacePacketBuf *)Buf;
    InterfacePacketBuf::DelPacketBuf(Pck);
    return 0;
}

static SQInteger register_Packet_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 RegisterPacket(HSQUIRRELVM v)
{
    register_Packet_func(v, Packet_New, _SC("Sq_Packet_New"));
    register_Packet_func(v, Packet_Put_header, _SC("Sq_Packet_Put_header"));
    register_Packet_func(v, Packet_Put_byte, _SC("Sq_Packet_Put_byte"));
    register_Packet_func(v, Packet_Put_short, _SC("Sq_Packet_Put_short"));
    register_Packet_func(v, Packet_Put_int, _SC("Sq_Packet_Put_int"));
    register_Packet_func(v, Packet_Put_binary, _SC("Sq_Packet_Put_binary"));
    register_Packet_func(v, Packet_Put_binaryex, _SC("Sq_Packet_Put_binaryex"));
    register_Packet_func(v, Packet_Finalize, _SC("Sq_Packet_Finalize"));
    register_Packet_func(v, Packet_Send, _SC("Sq_Packet_Send"));
    register_Packet_func(v, Packet_Delete, _SC("Sq_Packet_Delete"));
}