#pragma once

//演员对象智能指针Map
extern std::map<uint64_t, ActorPtr>ActorPtrMapObject;
//淡入淡出动画智能指针Map
std::map<uint32_t, FadeTransitionPtr>FadeTransitionPtrMapObject;
//场景管理Map
std::map<std::string, uint64_t>SquirrelStagePtrMapObject;

//重载场景
static SQInteger Reset_Scene(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    //获取传入场景
    SQInteger gscene;
    sq_getinteger(v, 2, &gscene);

    //获取传入场景
    if (ActorPtrMapObject.count(gscene)) {
        SquirrelStagePtr scene = dynamic_cast<SquirrelStage*>(ActorPtrMapObject[gscene].Get());
        //移除场景所有子对象
        scene->RemoveAllChildren();
        //重新构造
        scene->PushBaseUi();
        //重新调用进入场景函数
        scene->OnEnter();

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

    //ActorPtr c = scene->GetChild("Scene_Default_Layer");
    //c->RemoveAllChildren();

    //ActorList ChildList = scene->GetAllChildren();

    //for (auto& child : ChildList)
    //{
    //    if (!child->IsName("Scene_Mouse_UI_Layer")) {
    //        child->RemoveAllChildren();
    //    }
    //}

    return 1;
}


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

    //获取传入名字
    const SQChar* StageName;
    sq_getstring(v, 2, &StageName);

    //此操作会New出一个字符串需要手动销毁
    char* gStageName = SquirrelClassEx::SquirrelU2W((char*)StageName);

    std::string RealStageName = gStageName;

    //销毁New出来的字符串
    delete[]gStageName;

    //创建场景
    SquirrelStagePtr scene = new SquirrelStage(RealStageName);

    //获取对象的UUID
    uint32_t UUID = scene->GetObjectID();

    //将场景对象存入Map管理
    ActorPtrMapObject[UUID] = scene;
    SquirrelStagePtrMapObject[RealStageName] = UUID;

    sq_pushinteger(v, UUID);

    return 1;
}

//根据名字获取场景  如果不存在会自动创建
static SQInteger Get_SceneByName(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    //获取传入名字
    const SQChar* StageName;
    sq_getstring(v, 2, &StageName);

    //此操作会New出一个字符串需要手动销毁
    char* gStageName = SquirrelClassEx::SquirrelU2W((char*)StageName);

    std::string RealStageName = gStageName;

    //销毁New出来的字符串
    delete[]gStageName;

    if (SquirrelStagePtrMapObject.count(RealStageName) && ActorPtrMapObject.count(SquirrelStagePtrMapObject[RealStageName])) {
            sq_pushinteger(v, SquirrelStagePtrMapObject[RealStageName]);
    }
    else {
        //创建场景
        SquirrelStagePtr scene = new SquirrelStage(RealStageName);

        //获取对象的UUID
        uint32_t UUID = scene->GetObjectID();

        //将场景对象存入Map管理
        ActorPtrMapObject[UUID] = scene;
        SquirrelStagePtrMapObject[RealStageName] = UUID;

        sq_pushinteger(v, UUID);
    }

    return 1;
}

//根据名字获取城镇场景  如果不存在会自动创建
static SQInteger Get_TownSceneByName(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    //获取传入名字
    const SQChar* StageName;
    sq_getstring(v, 2, &StageName);

    //此操作会New出一个字符串需要手动销毁
    char* gStageName = SquirrelClassEx::SquirrelU2W((char*)StageName);

    std::string RealStageName = gStageName;

    //销毁New出来的字符串
    delete[]gStageName;

    if (SquirrelStagePtrMapObject.count(RealStageName) && ActorPtrMapObject.count(SquirrelStagePtrMapObject[RealStageName])) {
        sq_pushinteger(v, SquirrelStagePtrMapObject[RealStageName]);
    }
    else {
        //创建场景
        SquirrelTownStagePtr scene = new SquirrelTownStage(RealStageName);

        //获取对象的UUID
        uint32_t UUID = scene->GetObjectID();

        //将场景对象存入Map管理
        ActorPtrMapObject[UUID] = scene;
        SquirrelStagePtrMapObject[RealStageName] = UUID;

        sq_pushinteger(v, UUID);
    }

    return 1;
}

//导演切换场景
static SQInteger Director_EnterStage(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    //获取传入场景
    SQInteger gscene;
    sq_getinteger(v, 2, &gscene);

    //获取传入场景
    if (ActorPtrMapObject.count(gscene)) {
        SquirrelStagePtr scene = dynamic_cast<SquirrelStage*>(ActorPtrMapObject[gscene].Get());
        if (Top == 2) {
            Director::GetInstance().EnterStage(scene);
        }
        else if (Top == 3) {
            SQInteger gTsn;
            sq_getinteger(v, 3, &gTsn);

            if (FadeTransitionPtrMapObject.count(gTsn)) {
                FadeTransitionPtr Tsn = FadeTransitionPtrMapObject[gTsn];
                Director::GetInstance().EnterStage(scene, Tsn);
            }
            else {
                Director::GetInstance().EnterStage(scene);
            }
        }
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//导演切换场景
static SQInteger Director_PushStage(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    //获取传入场景
    SQInteger gscene;
    sq_getinteger(v, 2, &gscene);

    //获取传入场景
    if (ActorPtrMapObject.count(gscene)) {
        SquirrelStagePtr scene = dynamic_cast<SquirrelStage*>(ActorPtrMapObject[gscene].Get());
        if (Top == 2) {
            Director::GetInstance().PushStage(scene);
        }
        else if (Top == 3) {
            SQInteger gTsn;
            sq_getinteger(v, 3, &gTsn);

            if (FadeTransitionPtrMapObject.count(gTsn)) {
                FadeTransitionPtr Tsn = FadeTransitionPtrMapObject[gTsn];
                Director::GetInstance().PushStage(scene, Tsn);
            }
            else {
                Director::GetInstance().PushStage(scene);
            }
        }
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//导演切换场景
static SQInteger Director_PopStage(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    //获取传入场景
    SQInteger gscene;
    sq_getinteger(v, 2, &gscene);

    //获取传入场景
    if (ActorPtrMapObject.count(gscene)) {
        SquirrelStagePtr scene = dynamic_cast<SquirrelStage*>(ActorPtrMapObject[gscene].Get());
        if (Top == 2) {
            Director::GetInstance().PopStage();
        }
        else if (Top == 3) {
            SQInteger gTsn;
            sq_getinteger(v, 3, &gTsn);

            if (FadeTransitionPtrMapObject.count(gTsn)) {
                FadeTransitionPtr Tsn = FadeTransitionPtrMapObject[gTsn];
                Director::GetInstance().PopStage(Tsn);
            }
            else {
                Director::GetInstance().PopStage();
            }
        }
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//导演获取当前场景
static SQInteger Director_GetScene(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    uint32_t scene = Director::GetInstance().GetCurrentStage()->GetObjectID();
    sq_pushinteger(v, scene);

    return 1;
}

//导演退出当前场景并清空场景栈
static SQInteger Director_ClearStages(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    Director::GetInstance().ClearStages();

    return 0;
}

//创建淡入淡出过渡动画
static SQInteger Creat_FadeTransition(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger gDuration;
    SQBool gParallel;
    sq_getinteger(v, 2, &gDuration);
    sq_getbool(v, 3, &gParallel);

    Duration Time = Duration(gDuration);
    FadeTransitionPtr Tsn = new FadeTransition(Time, gParallel);

    //获取对象的UUID
    uint32_t UUID = Tsn->GetObjectID();

    //将高级精灵对象存入Map管理
    FadeTransitionPtrMapObject[UUID] = Tsn;
    
    sq_pushinteger(v, UUID);

    return 1;
}