import { KindItem } from "../../../common/Xys";
import { ItemEvent } from "../../../data/const/EventConst";
import GameDataCenter from "../../../data/GameDataCenter";
import FguiLoadMgr from "../../../frameWork/fgui/FguiLoadMgr";
import FguiMgr from "../../../frameWork/fgui/FguiMgr";
import EventMng from "../../../manager/EventMng";
import ClickAuEffect from "../../../utils/ClickAuEffect";
import GameMath from "../../../utils/GameMath";

export interface ItemCountExtraParam {
	showAddBtn?: boolean; //是否显示加按钮
	fullNumberShow?: boolean; //是否显示完整道具拥有数量
	isBuyItem?: boolean; //是否点击从集市购买道具
	notEnoughRed?: boolean; //道具不足是否显示红色字
	isPreCost?: boolean; //是否是预扣除类道具
	FixedCount?: number | string;// 显示固定数量，不用GameDataCenter.item.getItemCount
}

export default class ItemCount {
	item: KindItem;

	node: fairygui.GButton;

	icon: fairygui.GLoader;
	AddBtn: fairygui.GButton;

	CostCount: number = 0;

	callBack: Function;

	fullNumberShow: boolean;

	isBuyItem: boolean;

	redText: boolean;

	isPreCost: boolean;

	FixedCount: number | string;

	constructor(com: fairygui.GButton, kinditem?: KindItem, costCount: number = 0, param?: ItemCountExtraParam) {
		this.node = com;
		this.AddBtn = this.node.getChild("AddBtn") as fairygui.GButton;
		this.icon = this.node.getChild("icon") as fairygui.GLoader;
		if (kinditem) this.setData(kinditem, costCount, param);
		this.AddBtn.visible = param && param.showAddBtn == false ? false : true;
	}

	setData(kindItem: KindItem, costCount: number = 0, param?: ItemCountExtraParam, iconUrl?: string) {
		if (kindItem) {
			this.CostCount = costCount;
			this.fullNumberShow = param?.fullNumberShow ? true : false;
			this.isBuyItem = param?.isBuyItem ? true : false;
			this.redText = param?.notEnoughRed ? true : false;
			this.isPreCost = param?.isPreCost ?? false;
			this.FixedCount = param?.FixedCount;
			this.item = kindItem;
			if (!iconUrl) {
				FguiLoadMgr.loadItemIcon(this.icon, this.item, null, 0.4);
			} else {
				if (iconUrl.indexOf('ui://') >= 0) {
					this.icon.url = iconUrl;
				}else{
					//加载bundle资源
					let bundleName: string = iconUrl.split("|")[0];
					let urlStr: string = iconUrl.split("|")[1];
					GameDataCenter.loading.loadSpriteFormBundle(this.icon?.node, bundleName, urlStr);
				}
			}
			this.showItem();
			this.node.visible = true;
		} else {
			this.item = null;
			this.node.visible = false;
		}
	}

	showItem() {
		if (this.item == null) return;
		if (this.FixedCount) {
			this.node.title = this.FixedCount + "";// 显示传入的固定数量
			if (this.redText) {
				let itemCount = GameDataCenter.item.getItemCount(this.item);
				if (Number(this.FixedCount) > itemCount) {
					this.node.getChild("title").asTextField.color = cc.color().fromHEX("#E45849");
				} else {
					this.node.getChild("title").asTextField.color = cc.color().fromHEX("#FFFBDE");
				}
			}
		} else {
			let cost: string = this.CostCount ? "/" + this.CostCount : "";
			let itemCount = this.isPreCost ? GameDataCenter.item.getItemPreCostCount(this.item) : GameDataCenter.item.getItemCount(this.item);
			let num = this.fullNumberShow ? itemCount : GameMath.showNum(itemCount);
			this.node.title = num + cost;
			if (this.redText && itemCount < this.CostCount) {
				this.node.getChild("title").asTextField.color = cc.color().fromHEX("#E45849");
			} else {
				this.node.getChild("title").asTextField.color = cc.color().fromHEX("#FFFBDE");
			}
		}
	}

	showDetail() {
		// FguiMgr.Instance.openUI(ItemDetailViewView, ViewZorder.POP, null, this.item);
	}

	/**设置点击回调 如果有设置，就不会弹出详情
	 */
	setCallBack(callBack: Function) {
		this.callBack = callBack;
	}

	onEnable() {
		this.node.onClick(this.onClick, this);
		EventMng.on(ItemEvent.UP_ITEM, this.showItem, this);
		this.showItem();
	}

	onDisable() {
		this.node.offClick(this.onClick, this);
		EventMng.off(ItemEvent.UP_ITEM, this.showItem, this);
	}

	@ClickAuEffect()
	onClick() {
		if (this.callBack) {
			this.callBack();
		} else {
			if (this.isBuyItem) {
				// GameDataCenter.shop.openBuyItemView(this.item);
			} else {
				this.showDetail();
			}
		}
	}
}