#include "Scene/Scene_Loading_UI.h"
#include "Scene/Scene_MainUi.h"
#include "Scene/Scene_Test.h"
#include "Scene_Loading_UI.h"
#include "EngineFrame/Component/Sprite.h"
#include "EngineFrame/Component/Text.h"
#include "Global/Global_Game.h"
#include "Asset/Asset_Script.h"

void Scene_Loading_UI::Enter()
{
    // 加载OGG文件
    music = Mix_LoadMUS("Music/characterSelectStage.ogg");
    if (music)
    {
        Mix_PlayMusic(music, 1);
    }

    RefPtr<Actor> actor = new Actor;
    AddChild(actor);

    RefPtr<Sprite> BackGroundSp = new Sprite("ImagePacks2/Loading1.png");
    actor->AddComponent(BackGroundSp);
    RefPtr<Sprite> BackGround2Sp = new Sprite("ImagePacks2/Loading0.png");
    BackGround2Sp->SetPos(Vec2{0, 686});
    actor->AddComponent(BackGround2Sp);
    RefPtr<Sprite> LoadCircleSp = new Sprite("ImagePacks2/Loading2.png");
    LoadCircleSp->SetName("LoadCircle");
    LoadCircleSp->SetPos(Vec2{1280 - 60, 686 - 60});
    LoadCircleSp->SetBlendMode(LINEARDODGE);
    LoadCircleSp->SetAnchor(Vec2{0.5f, 0.5f});
    actor->AddComponent(LoadCircleSp);

    actor->SetCallbackOnUpdate([LoadCircleSp](float deltaTime) mutable
                               { 
                                float angle = LoadCircleSp->GetRotation();
                                LoadCircleSp->SetRotation(angle + 180.0f * deltaTime); });
}

void Scene_Loading_UI::Update(float deltaTime)
{
    Scene::Update(deltaTime);
    if (Global_Game::GetInstance().InitFlag)
    {
        // 设定游戏层场景
        RefPtr<Scene_Test> scene = new Scene_Test;
        Game::GetInstance().ChangeScene(scene);
        // 设定UI层场景
        RefPtr<Scene_MainUi> sceneUI = new Scene_MainUi;
        Game::GetInstance().ChangeUIScene(sceneUI);
    }
}

void Scene_Loading_UI::Exit()
{
    if (music)
    {
        Mix_FreeMusic(music);
        music = nullptr;
    }
}
