#pragma once

//创建按钮
static SQInteger Create_Button(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SquirrelButtonPtr Button = new SquirrelButton();

    //获取对象的唯一ID
    uint64_t UUID = Button->GetObjectID();

    //将按钮对象存入Map管理
    ActorPtrMapObject[UUID] = Button;

    sq_pushinteger(v, UUID);

    return 1;
}

//设置按钮状态纹理
static SQInteger Button_Load_Texture(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger ActorUUID;
    sq_getinteger(v, 2, &ActorUUID);

    const SQChar* Path;
    sq_getstring(v, 3, &Path);

    SQInteger Frame;
    sq_getinteger(v, 4, &Frame);

    if (ActorPtrMapObject.count(ActorUUID)) {
        SquirrelButtonPtr Button = dynamic_cast<SquirrelButton*>(ActorPtrMapObject[ActorUUID].Get());

        TexturePtr t = SquirrelClassEx::GetTexturePtrByImg((char*)Path, Frame);
        Button->SetFrame(t);
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//初始化按钮
static SQInteger Button_Init(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger ActorUUID;
    sq_getinteger(v, 2, &ActorUUID);

    if (ActorPtrMapObject.count(ActorUUID)) {
        SquirrelButtonPtr Button = dynamic_cast<SquirrelButton*>(ActorPtrMapObject[ActorUUID].Get());
        Button->Init();
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}


//判断按钮是否被点击
static SQInteger Button_IsPress(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger ActorUUID;
    sq_getinteger(v, 2, &ActorUUID);

    SQInteger Type;
    sq_getinteger(v, 3, &Type);

    if (ActorPtrMapObject.count(ActorUUID)) {
        SquirrelButtonPtr Button = dynamic_cast<SquirrelButton*>(ActorPtrMapObject[ActorUUID].Get());
        sq_pushbool(v, Button->IsPress(Type));
    }
    else {
        sq_pushnull(v);
    }

    return 1;
}

//判断按钮是否悬停
static SQInteger Button_IsHover(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger ActorUUID;
    sq_getinteger(v, 2, &ActorUUID);

    if (ActorPtrMapObject.count(ActorUUID)) {
        SquirrelButtonPtr Button = dynamic_cast<SquirrelButton*>(ActorPtrMapObject[ActorUUID].Get());
        sq_pushbool(v, Button->IsHover());
    }
    else {
        sq_pushnull(v);
    }

    return 1;
}

//移除自己的Z轴序列
static SQInteger Button_RemoveZorder(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger ActorUUID;
    sq_getinteger(v, 2, &ActorUUID);

    if (ActorPtrMapObject.count(ActorUUID)) {
        SquirrelButtonPtr Button = dynamic_cast<SquirrelButton*>(ActorPtrMapObject[ActorUUID].Get());
        Button->RemoveZorder();
        sq_pushbool(v, true);
    }
    else {
        sq_pushnull(v);
    }

    return 1;
}