import { gameMethod } from "../../../common/gameMethod";
import { KindItem } from "../../../common/Xys";
import { BaseItemType } from "../../../data/const/TypeConst";
import GameDataCenter from "../../../data/GameDataCenter";
import ResSprite from "../../../frameWork/compment/ResSprite";
import FguiLoadMgr from "../../../frameWork/fgui/FguiLoadMgr";
import ClickAuEffect from "../../../utils/ClickAuEffect";

export interface ItemIconParam {
	showNum?: boolean;
	type?: BaseItemType;
}

export default class ItemIcon extends fgui.GLabel {

	item: KindItem;
	itemIcon: fgui.GLoader;
	itemNum: fgui.GTextField;
	isextra: fgui.Controller;
	extra: fgui.GComponent;
	itemType: string; // 道具类型
	type: BaseItemType = BaseItemType.empty;
	itemIconResSprite: ResSprite;

	protected onConstruct(): void {
		this.itemIcon = this.getChild("icon").asLoader;
		this.itemNum = this.getChild("title").asTextField;
		this.extra = this.getChild("Extra").asCom;
		this.isextra = this.getController("isExtra");
		this.itemIconResSprite = this.itemIcon.node.addComponent(ResSprite);
	}

	protected onEnable(): void {
		this.onClick(this.onClickItem, this);
	}

	protected onDisable(): void {
		this.offClick(this.onClickItem, this);
	}

	setData(item: KindItem, param?: ItemIconParam) {
		if (gameMethod.isEmpty(item)) {
			this.itemIcon.url = '';
			this.itemIconResSprite.unsetSpriteFrame();
			this.itemNum.text = '';
			return;
		}
		this.item = item;
		this.type = param?.type ?? BaseItemType.empty;
		let confBase = GameDataCenter.item.getItemCfgBase(item);
		this.itemType = confBase.type;
		if (confBase == null) {
			console.error("没有找到配置信息:", confBase);
			this.itemIcon.url = '';
			return;
		}
		this.itemIconResSprite.unsetSpriteFrame();
		FguiLoadMgr.loadItemIcon(this.itemIcon, item, () => { }, 1);
		if (param?.showNum) {
			this.itemNum.text = "x" + item?.[2];
		}
		else {
			this.itemNum.text = "";
		}
	}

	@ClickAuEffect()
	onClickItem(evt: Event) {
		// 阻止事件继续传播
		evt.stopPropagation();

		if (this.type == BaseItemType.detail) {
			this.showItemDetail();
		}
	}

	showItemDetail() {
		// 显示道具详情
	}
}
