import Config from "../../Config";
import FguiMgr from "../../frameWork/fgui/FguiMgr";
import EventMng from "../../manager/EventMng";
import AssetsBundleMgr from "../../utils/AssetsBundleMgr";
import { LoadEvent } from "../const/EventConst";
import { InGame } from "../const/TypeConst";
import IDataModel from "../../frameWork/model/IDataModel";
import ResSprite from "../../frameWork/compment/ResSprite";
import FguiLoadMgr from "../../frameWork/fgui/FguiLoadMgr";
import { RedUtil } from "../../frameWork/fgui/RedUtil";
import UserHead from "../../logic/fgui/Common/UserHead";
import ItemIcon from "../../logic/fgui/Common/ItemIcon";

export default class LoadingModel extends IDataModel {
    constructor() {
        super('loading');
    }

    // 两个变量加载完成后,即可进入主游戏场景
    loginSucc: boolean = false // 登录成功
    jsonLoadSucc: boolean = false // 配置表加载成功
    mainAssetsLoadSucc: boolean = false // 主要资源加载成功
    isLoadEnter: boolean = false //进游戏加载资源

    // 预加载列表
    preLoadList: { bundle: string, packageName: string, spriteList?: string[], resPath?: string }[] = [
        // { bundle: "Home", packageName: "Home", spriteList: ["Home_atlas0"] },
    ]

    // 加载主场景
    loadMainAssets(callback?: Function) {
        if (Config.inGame == InGame.home) {
            return
        }
        EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.1, "res")
        //fgui优先加载common的组件
        FguiLoadMgr.loadPackage("Common", "Common", (error, pkg) => {
            if (error) {
                console.error(error)
            }
            if (callback) {
                callback()
            }
            // //拓展基础类
            // fgui.UIObjectFactory.setExtension("ui://Common/ItemBase", ItemBase);
            fgui.UIObjectFactory.setExtension("ui://Common/UserHead", UserHead);
            // fgui.UIObjectFactory.setExtension("ui://Common/JingJieCom", JingJieCom);
            // fgui.UIObjectFactory.setExtension("ui://Common/TopItemCount", TopItemCount);
            // fgui.UIObjectFactory.setExtension("ui://Common/RedDot", RedDot);
            fgui.UIObjectFactory.setExtension("ui://Common/ItemIcon", ItemIcon);
            // fgui.UIObjectFactory.setExtension("ui://Common/HdTimeBenBtn", HdTimeBenBtn);
            // fgui.UIObjectFactory.setExtension("ui://Common/ChengHaoCom", ChengHaoCom);
            // fgui.UIObjectFactory.setExtension("ui://Common/GrowthGiftEntrBtn", GrowthGiftEntrBtn);
            // fgui.UIObjectFactory.setExtension("ui://GameHome/HdItemBtn", HdItem);

            this.preLoadBundle(0)
        })
    }

    private preLoadBundle(index: number) {
        if (index >= this.preLoadList.length) {
            EventMng.emit(LoadEvent.LOAD_PROGRESS, 1, "res")
            this.mainAssetsLoadSucc = true
            this.checkEnterGame()
            return
        }
        let bundle = this.preLoadList[index].bundle
        let packageName = this.preLoadList[index].packageName
        let spriteList = this.preLoadList[index].spriteList
        let resPath = this.preLoadList[index].resPath
        AssetsBundleMgr.loadBundle(bundle, (err, bundle) => {
            if (err) {
                console.error(err)
                return
            }
            if (packageName) {
                fgui.UIPackage.loadPackage(bundle, packageName, (error, pkg) => {
                    if (error) {
                        console.error(error)
                    }
                    //加载下一个
                    this.preLoadBundle(index + 1)
                });
                for (let i = 0; i < spriteList.length; i++) {
                    bundle.preload(spriteList[i], cc.SpriteFrame, (err, assert) => {
                        if (err) {
                            console.error(err)
                        }
                    })
                }
            } else {
                bundle.preload(resPath, cc.Asset, (err, assert) => {
                    if (err) {
                        console.error(err)
                    }
                    //加载下一个
                    this.preLoadBundle(index + 1)
                })
            }

            EventMng.emit(LoadEvent.LOAD_PROGRESS, (index + 1) / this.preLoadList.length, "res")
        })
    }

    // 资源或登录成功,检测是否能够进入游戏
    checkEnterGame() {
        if (this.mainAssetsLoadSucc && this.loginSucc && this.jsonLoadSucc) {
            //初始化红点模块
            RedUtil.initRedUtil()
            FguiMgr.Instance.CloseAllUI()
            cc.director.loadScene("game")
        }
    }

    //动态添加图标精灵
    loadSpriteFormBundle(target: cc.Node, bundleName: string, spriteName: string, cb?: (asset: cc.SpriteFrame) => void, scale?: number) {
        let targetSprite = target.getComponent(ResSprite)
        if (!targetSprite) {
            targetSprite = target.addComponent(ResSprite)
        }
        if (scale) {
            target.setScale(scale, scale);
        }
        targetSprite.setSpriteFrame(bundleName, spriteName, cb);
    }
}