import { gameMethod } from "../../common/gameMethod";
import { KindItem } from "../../common/Xys";
import { ConstItem, ItemKind, UserSex } from "../../data/const/TypeConst";
import GameDataCenter from "../../data/GameDataCenter";
import AssetsBundleMgr from "../../utils/AssetsBundleMgr";
import { I18n } from "../../utils/I18nUtil";
import ResGComponent from "../compment/ResGComponent";
import ResSprite from "../compment/ResSprite";

/**
 * 动态加载资源
 */
export default class FguiLoadMgr {
	//动态加载道具图标
	static loadItemIcon(obj: fgui.GLoader | fgui.GButton | fgui.GLabel, kindItem: KindItem | ConstItem, callback: Function = (success: boolean) => { }, scale?: number) {
		if (!kindItem) {
			let node = obj.node;
			let itemResSprite = node.getComponent(ResSprite);
			itemResSprite?.unsetSpriteFrame();

			callback && callback(false);
			return;
		}

		let iconUrl = GameDataCenter.item.getItemCfgBase(kindItem)?.iconUrl;
		if (gameMethod.isEmpty(iconUrl)) {
			if (CC_PREVIEW) console.error("道具Url未配置===>>>", kindItem);
			return;
		}
		let kind = kindItem?.[0];
		if (kind == ItemKind.money) {
			let node = obj.node;
			let itemBase = GameDataCenter.item.getItemCfgBase(kindItem);
			let itemResSprite = node.getComponent(ResSprite) ?? node.addComponent(ResSprite);
			itemResSprite.setSpriteFrame("ItemIcon", itemBase.icon, () => {
				if (callback) {
					callback(true);
				}
			});
			let nodeScaleX = scale ?? obj.scaleX;
			let nodeScaleY = scale ?? obj.scaleY;
			node.setScale(nodeScaleX, nodeScaleY);
			return;
		}

		// 因有些图标不在common包里 所以需要加载
		let newUrls = iconUrl.split("/");
		this.loadPackage(newUrls[2], newUrls[2], (error) => {
			if (error) {
				if (callback) callback(false);
				return;
			}

			let url = iconUrl;
			if (obj instanceof fgui.GLoader) {
				obj.url = url;
			} else {
				obj.icon = url;
			}

			if (scale) obj.asCom.setScale(scale, scale);

			if (callback) {
				callback(true);
			}
		});
	}

	//动态加载package包里的图片资源
	static loadSpriteFormPackage(obj: fgui.GLoader | fairygui.GLabel | fairygui.GButton, bundleName: string, pkgName: string, url: string, callback: Function = (success: boolean) => { }, scale?: number, xFlip: boolean = false, yFlip: boolean = false) {
		this.loadPackage(bundleName, pkgName, (error) => {
			if (error || !obj?.node || !obj.node.isValid) {
				if (callback) callback(false);
				return;
			}
			if (obj instanceof fgui.GLoader) {
				obj.url = url;
			} else {
				obj.icon = url;
			}
			if (scale) {
				obj.setScale(scale, scale);
			}
			if (xFlip || yFlip) {
				obj.setScale(obj.scaleX * (xFlip ? -1 : 1), obj.scaleY * (yFlip ? -1 : 1));
			}
			if (callback) {
				callback(true);
			}
		});
	}


	private static loadFguiPkg(bundle, pkgName, callback?: Function) {
		let p = fgui.UIPackage.getByName(pkgName);
		if (p) {
			if (callback) callback(null, p);
		} else {
			fgui.UIPackage.loadPackage(bundle, pkgName, (error, pkg) => {
				if (error) {
					if (callback) callback(error, null);
					return;
				}
				if (callback) {
					callback(null, pkg);
				}
			});
		}
	}


	/** 同步加载package */
	static async loadPackageAsync(bundleName: string, pkgName: string): Promise<void> {
		return new Promise<void>((resolve, reject) => {
			AssetsBundleMgr.loadBundle(bundleName, (error, bundle: cc.AssetManager.Bundle) => {
				if (error) {
					reject(error);
					return;
				}
				FguiLoadMgr.loadFguiPkg(bundle, pkgName, (error, pkg) => {
					if (error) {
						reject(error);
						return;
					}
					resolve();
				});
			});
		});
	}

	static loadPackage(bundleName: string, pkgName: string, onComplete: (error: any, pkg: fgui.UIPackage) => void): void {
		AssetsBundleMgr.loadBundle(bundleName, (error, bundle: cc.AssetManager.Bundle) => {
			if (error) {
				onComplete(error, null);
				return;
			}
			FguiLoadMgr.loadFguiPkg(bundle, pkgName, (error, pkg) => {
				onComplete(error, pkg);
			});
		});
	}


	/** 加载fgui里的图片 */
	static loadPackageFrameAsset(node: cc.Node, packageName: string, iconUrl: string) {
		this.loadPackage(packageName, packageName, (error, pkg) => {
			if (error) {
				return;
			}
			let asset = pkg.getItemAssetByName(iconUrl);
			if (asset) {
				let frame = asset as cc.SpriteFrame;
				node.getComponent(cc.Sprite).spriteFrame = frame;
			}
		});
	}

	/** 设置多语言文本 */
	static setI18nTextLabel(gLabel: fgui.GTextField | fgui.GRichTextField | fgui.GLabel | fgui.GButton, key: string, ...args: any[]) {
		let str = I18n.getI18nText(key, ...args);
		this.setLabel(gLabel, str);
	}

	static setLabel(gLabel: fgui.GTextField | fgui.GRichTextField | fgui.GLabel | fgui.GButton, str: string) {
		// gLabel.node.getComponent(cc.Label).cacheMode = cc.Label.CacheMode.CHAR;
		if (gLabel instanceof fgui.GTextField || gLabel instanceof fgui.GRichTextField) {
			gLabel.text = str;
		} else if (gLabel instanceof fgui.GLabel || gLabel instanceof fgui.GButton) {
			gLabel.title = str;
		}
	}

	/** 动态加载组件 */
	static LoadComp(parent: fairygui.GComponent, bundleName: string, pkgName: string, resName: string, userClass: new () => fgui.GObject, onComplete: (comp: fairygui.GObject) => void) {
		let resGComp = parent.node.getComponent(ResGComponent);
		if (!resGComp) resGComp = parent.node.addComponent(ResGComponent);
		resGComp.LoadComp(bundleName, pkgName, resName, userClass, (comp: fairygui.GObject) => {
			onComplete(comp);
		});
	}

}
