#include "Scene_Test.h"
#include <memory>
#include "EngineCore/Game.h"
#include "EngineFrame/Component/AnimationManager.h"
#include "EngineFrame/Component/Text.h"
#include "EngineFrame/Component/Canvas.h"
#include "Global/Global_Game.h"
Scene_Test::Scene_Test()
{
}

Scene_Test::~Scene_Test()
{
    SDL_Log("Scene_Test::我的ID是%d --- 我被释放了!", this->MyId);
}

void Scene_Test::Enter()
{
    map = new GameMap;
    map->LoadMap("map/cataclysm/town/elvengard/new_d_elvengard_l.map");
    map->Enter(this);

    RefPtr<CharacterObject> obj = new CharacterObject();
    obj->SetPosition({1000, 300, 0});
    obj->Construction(0);

    map->AddObject(obj);

    RefPtr<MonsterObject> monster = new MonsterObject();
    monster->SetPosition({1200, 301, 0});
    monster->Construction(1);
    monster->SetDirection(1);
    map->AddObject(monster);

    _camera = new GameMapCamera;
    _camera->SetFromActor(obj.Get());

    return;

    // // SDL_Log("Scene_Test::进入测试场景!");
    RefPtr<Actor> actor = new Actor;
    AddChild(actor);

    // RefPtr<Sprite> sprite = new Sprite("ImagePacks2/test_white_background.png");

    RefPtr<Sprite> chr = new Sprite("sprite/interface2/hud/hud.img", 0);
    chr->SetPos(Vec2{200, 100});
    // chr->SetAlpha(0.5);
    // chr->SetShadow();
    // actor->AddComponent(chr);

    RefPtr<Canvas> canvas = new Canvas(VecSize{250, 720});
    actor->AddChild(canvas);
    canvas->DrawImg("sprite/interface2/hud/hud.img", 0, Vec2{0, 0});

    // canvas->AddChild(chr);

    // RefPtr<AnimationManager> Am = new AnimationManager;
    // RefPtr<Animation> ani4 = new Animation("map/cataclysm/town/hendonmyre/animation/object/gateall_02.ani");
    // Am->AddAnimation(ani4);

    // // Am->SetPos(Vec2{200, 100});
    // actor->AddChild(Am);

    // // sprite2->UnsetClipRect();

    // RefPtr<Actor> actor2 = new Actor;
    // actor->AddChild(actor2);

    // RefPtr<Animation> ani4 = new Animation("map/cataclysm/town/hendonmyre/animation/object/gateall_02.ani");
    // // ani4->SetScale(Vec2{-1.0, 1.0});
    // actor->AddChild(ani4);

    // RefPtr<Animation> ani3 = new Animation("map/cataclysm/town/elvengard/animation/obj/serialight01_right.ani");
    // actor->AddChild(ani3);

    // RefPtr<Animation> ani4 = new Animation("map/cataclysm/town/hendonmyre/animation/object/gateall_02.ani");
    // ani4->SetPos(VecPos{300, 500});
    // actor->AddChild(ani4);

    // for (size_t i = 0; i < 400; i++)
    // {
    //     RefPtr<Actor> actor = new Actor;
    //     AddChild(actor);

    //     // RefPtr<Animation> ani3 = new Animation("common/commoneffect/animation/priestslowheal1.ani");
    //     // actor->AddComponent(ani3);
    //     // ani3->SetRenderZOrder(1000);

    //     // RefPtr<Animation> ani = new Animation("common/anton/main.ani");
    //     // actor->AddComponent(ani);
    //     // ani->SetRenderZOrder(500);

    //     RefPtr<Sprite> sprite = new Sprite("sprite/item/avatar/swordman/0sm_acap.img", 0);
    //     actor->AddComponent(sprite);
    //     // RefPtr<Animation> ani2 = new Animation("common/anton/face/0/0.ani");
    //     // actor->AddComponent(ani2);
    //     // ani2->SetRenderZOrder(1000);
    // }
}

void Scene_Test::HandleEvents(SDL_Event *e)
{
    Scene::HandleEvents(e);
}

void Scene_Test::Update(float deltaTime)
{
    Scene::Update(deltaTime);
    // 摄像机中有检测宿主坐标 所以要在场景的update后调用以便读取到本帧最新的坐标
    if (_camera)
        _camera->Update(deltaTime);
    if (map)
        map->Update(deltaTime);
}

void Scene_Test::Render()
{
    Scene::Render();
}

void Scene_Test::Exit()
{
    SDL_Log("Scene_Test::退出测试场景!当前引用计数%d", this->GetRefCount());
}

RefPtr<BaseNode> Scene_Test::GetCamera()
{
    return this->_camera;
}
