import { _decorator, Component, director, EventGamepad, Node } from 'cc';
import { resMgr } from '../Frames/ResourcesMgr';
import { dataMgr } from '../Frames/DataManager';
import { LoadingUI } from './UI/LoadingUI';
import { localDt } from '../Frames/LocalDt';
import { GameInfo } from '../GameInfo';
import { UIMgr } from '../Frames/UIManager';
const { ccclass, property } = _decorator;

@ccclass('MyApp')
export class MyApp extends Component {
    private static hasOpenedStartUI = false;
    loadingUI: Node | null = null;
    protected onLoad(): void {
        this.loadingUI = this.node.getChildByName("LoadingUI");
    }
    protected async start() {
        if (MyApp.hasOpenedStartUI) return;
        const loadingUI = this.loadingUI?.getComponent(LoadingUI)
        if (!this.loadingUI.active) {
            this.loadingUI.active = true;
        }
        await dataMgr.loadDataDir("Data", (finish, total) => {
            loadingUI?.updateProgress(finish / total);
            loadingUI?.updateWhichRes(1);
        });
        await resMgr.loadAllRes("Res", (finish, total) => {
            loadingUI?.updateProgress(finish / total);
            loadingUI?.updateWhichRes(2);
        });
        await resMgr.loadAllRes("UI", (finish, total) => {
            loadingUI?.updateProgress(finish / total);
            loadingUI?.updateWhichRes(3);
        });
        this.initData();
        this.loadingUI.active = false;
        if (!MyApp.hasOpenedStartUI) {
            UIMgr.openUI("Start");
            MyApp.hasOpenedStartUI = true;
        }
    }

    private initData() {
        if (!localDt.getData("Gold")) {
            localDt.saveData("Gold", 8888);
        }
        if (!localDt.getData("Diamond")) {
            localDt.saveData("Diamond", 200);
        }
        if (!localDt.getData("curLv")) {
            localDt.saveData("curLv", 1);
        }
        if (!localDt.getData("availableCardSlot")) {
            localDt.saveData("availableCardSlot", 4);
        }
    }
}