import { _decorator, Component, director, Node, SceneAsset, Sprite, Tween, tween } from 'cc';
import { InitData } from './InitData';
const { ccclass, property } = _decorator;

@ccclass('Loading')
export class Loading extends Component {

    @property(Sprite)
    private progressBar: Sprite = null;
    @property(Node)
    private startBtn: Node = null;
    @property(Node)
    private progress: Node = null;
    fakeProgress: number;
    // 将 fakeProgress 存储在一个对象中，便于 Tween 动画操作
    private progressData = { fakeProgress: 0 };
    // 用于标记场景是否加载完成
    private sceneLoaded: boolean = false;



    start() {
        this.preloadScene('main')
    }

    update(deltaTime: number) {

    }

    async startGame() {

        this.startBtn.active = false;
        this.progress.active = true;
        InitData.ins.initUserData();

    }

    preloadScene(sceneName: string): void {
        // 初始化假的进度条为0
        this.progressData.fakeProgress = 0;
        this.sceneLoaded = false; // 重置场景加载标志

        // 预加载场景
        director.preloadScene(sceneName, this.onProgress.bind(this), (err, sceneAsset: SceneAsset) => {
            if (err) {
                console.error(`Failed to load scene: ${sceneName}`, err);
                return;
            }
            // 场景加载完成，标记为已加载
            this.sceneLoaded = true;

        });
    }

    private onProgress(completedCount: number, totalCount: number): void {

    }
    private updateProgressBar(): void {
        // 更新进度条显示
        if (this.progressBar && this.progressBar.fillRange < 1) {
            this.progressBar.fillRange = this.progressData.fakeProgress;
        }
    }

    private completeLoading(sceneName): void {
        // 当Tween动画完成并且场景加载完毕后，将进度条快速填充到100%
        Tween.stopAllByTarget(this.progressData);
        tween(this.progressData)
            .to(1, { fakeProgress: 1 }, {
                onUpdate: () => {
                    this.updateProgressBar(); // 更新进度条显示
                },
                onComplete: () => {
                    // 最终完成，切换场景
                    this.scheduleOnce(() => {
                        director.loadScene(sceneName, (err) => {
                            if (err) {
                                console.error(`Failed to switch to scene: main`, err);
                            } else {
                                console.log(`Scene main loaded successfully!`);
                            }
                        });
                    }, 0.1); // 延迟切换场景
                }
            })
            .start();
    }

    public updateProgress(targetProgress: number) {
        console.log('更新loading进度条');

        Tween.stopAllByTarget(this.progressData);
        tween(this.progressData)
            .to(3, { fakeProgress: targetProgress }, {
                onUpdate: () => {
                    this.updateProgressBar();
                }
            })
            .start();
    }

    public enterGame(sceneName: string) {
        this.completeLoading(sceneName);
    }

}

