#pragma once
//演员对象智能指针Map
extern std::map<uint64_t, ActorPtr>ActorPtrMapObject;

extern NPK_M* npk;
//创建图像精灵
static SQInteger Create_Image(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }


    const SQChar* gOutPutBuffer;
    sq_getstring(v, 2, &gOutPutBuffer);

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

    //转化img名称
    std::string TextureName = (char*)gOutPutBuffer;

    //构造纹理
    TexturePtr t = SquirrelClassEx::GetTexturePtrByImg(TextureName, gFrame);

    //构造对象
    SpritePtr Png = new Sprite(t);

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

    //将精灵对象存入Map管理
    ActorPtrMapObject[UUID] = Png;

    sq_pushinteger(v, UUID);

    return 1;
}

//创建高级图像精灵
static SQInteger Create_ImageEx(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }


    //赋予了初始纹理
    if (Top == 3) {
        const SQChar* gOutPutBuffer;
        sq_getstring(v, 2, &gOutPutBuffer);

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

        std::string TextureName = (char*)gOutPutBuffer;

        SpriteExPtr Png = new SpriteEx();

        TexturePtr t = SquirrelClassEx::GetTexturePtrByImg(TextureName, gFrame);

        Png->SetFrame(t);

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

        //将高级精灵对象存入Map管理
        ActorPtrMapObject[UUID] = Png;

        sq_pushinteger(v, UUID);

    }
    //空纹理
    else if (Top == 1) {

        SpriteExPtr Png = new SpriteEx();

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

        //将高级精灵对象存入Map管理
        ActorPtrMapObject[UUID] = Png;

        sq_pushinteger(v, UUID);

    }

    return 1;
}


//设置高级图像精灵混合模式
static SQInteger Set_ImageEx_Mode(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 gType;
    sq_getinteger(v, 3, &gType);

    //从全局Map中获取高级精灵角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        SpriteExPtr Png = dynamic_cast<SpriteEx*>(ActorPtrMapObject[ActorUUID].Get());
        Png->SetMode(gType);
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//设置高级图像精灵帧
static SQInteger Set_ImageEx_Frame(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* gOutPutBuffer;
    sq_getstring(v, 3, &gOutPutBuffer);

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

    std::string TextureName = (char*)gOutPutBuffer;

    //从全局Map中获取高级精灵角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        SpriteExPtr Png = dynamic_cast<SpriteEx*>(ActorPtrMapObject[ActorUUID].Get());

        TexturePtr t = SquirrelClassEx::GetTexturePtrByImg(TextureName, gFrame);

        Png->SetFrame(t);

        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}


//设置高级精灵图像透明度
static SQInteger Set_ImageEx_Alpha(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);

    SQFloat Alpha;
    sq_getfloat(v, 3, &Alpha);


    //从全局Map中获取高级精灵角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        SpriteExPtr Png = dynamic_cast<SpriteEx*>(ActorPtrMapObject[ActorUUID].Get());
        Png->SetOpacity(Alpha);
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}



//设置高级精灵图像帧信息
static SQInteger Set_ImageEx_FrameInfo(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);

    std::vector<int> FrameDelay;
    sq_pushnull(v);  // null iterator
    while (SQ_SUCCEEDED(sq_next(v, 3)))
    {
        SQInteger gDelay;
        sq_getinteger(v, -1, &gDelay);
        FrameDelay.push_back(gDelay);
        //这里-1是值，-2是键
        sq_pop(v, 2); //在下一次迭代之前弹出键和值
    }
    sq_pop(v, 1);

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

    //从全局Map中获取高级精灵角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        SpriteExPtr Png = dynamic_cast<SpriteEx*>(ActorPtrMapObject[ActorUUID].Get());
        Png->SetFrameInfo(FrameDelay, Loop);
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//设置高级精灵图像播放动画
static SQInteger Set_ImageEx_PlayAnimotion(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);

    SQBool Type;
    sq_getbool(v, 3, &Type);

    //从全局Map中获取高级精灵角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        SpriteExPtr Png = dynamic_cast<SpriteEx*>(ActorPtrMapObject[ActorUUID].Get());
        Png->SetAnimotionPlay(Type);
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}


//设置高级精灵Ani帧
static SQInteger Set_ImageEx_Animotion_Frame(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 Frame;
    sq_getinteger(v, 3, &Frame);

    //从全局Map中获取高级精灵角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        SpriteExPtr Png = dynamic_cast<SpriteEx*>(ActorPtrMapObject[ActorUUID].Get());
        Png->SetAnimotionFrame(Frame);
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}



//设置高级精灵阴影
static SQInteger Set_ImageEx_Shadow(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 Frame;
    //sq_getinteger(v, 3, &Frame);

    //从全局Map中获取高级精灵角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        SpriteExPtr Png = dynamic_cast<SpriteEx*>(ActorPtrMapObject[ActorUUID].Get());
        Png->SetShadow();
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//设置裁切精灵
static SQInteger Set_Image_CropRect(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);

    SQFloat L;
    sq_getfloat(v, 3, &L);

    SQFloat U;
    sq_getfloat(v, 4, &U);

    SQFloat R;
    sq_getfloat(v, 5, &R);

    SQFloat D;
    sq_getfloat(v, 6, &D);

    //从全局Map中获取高级精灵角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        SpritePtr Png = dynamic_cast<Sprite*>(ActorPtrMapObject[ActorUUID].Get());

        Png->SetCropRect(Rect(Point(L,U), Point(R, D)));
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}