import { _decorator, Label, Node, Sprite, SpriteFrame } from 'cc';
import { ResKeeper } from '../../../framework/res/ResKeeper';
import { ItemEnum } from '../../common/InterfaceAddEnum';
import { Framework } from '../../../framework/Framework';
import { ViewID } from '../../../framework/config/LayerConf';
const { ccclass, property } = _decorator;

@ccclass('CommonItem')
export class CommonItem extends ResKeeper {
	@property({ type: Sprite, tooltip: "背景" })
	farmeBg: Sprite = null;

	@property({ type: Label, tooltip: "数量" })
	numTx: Label = null;

	@property({ type: Sprite, tooltip: "图标" })
	icoImg: Sprite = null;

	@property({ type: Node, tooltip: "碎片标记" })
	pieceIco: Node = null;

	@property({ type: Node, tooltip: "阵营标记" })
	raceIco: Node = null;

	@property({ type: Node, tooltip: "星级节点" })
	starNode: Node = null;

	@property({ type: [Node], tooltip: "星星" })
	stars: Node[] = [];

	@property({ type: [SpriteFrame], tooltip: "星星" })
	gradeBgs: SpriteFrame[] = [];

	@property({ type: Node, tooltip: "选中效果" })
	selectBg: Node = null;

	@property({ type: [SpriteFrame], tooltip: "背景图" })
	bgFrame: SpriteFrame[] = [];

	private isSelect = false;
	private isNumShow = true;
	private isClickEnable = true;
	private data = null;
	private clickFunc = null;

	protected onLoad() {
		this.selectBg.active = false;
		this.pieceIco.active = false;
		this.raceIco.active = false;
		this.starNode.active = false;
	}

	protected onDestroy() {
		//如果该组件有事件自行取消注释
		//Framework.event.removeEvent(this);
		super.onDestroy();
	}

	//如果使用了池中的节点,在该函数内归还,该函数会在onDestroy前调用
	onClose() {

	}

	//UI事件处理
	private onTouchButton(event: Event) {
		//Framework.audio.playEffect(AudioID.Click);
		let target: any = event.target;
	}

	refreshItem(data) {
		this.data = data;
		if (data.clickFunc) {
			this.clickFunc = data.clickFunc;
		}
		if (this.data.type == ItemEnum.material) {
			this.setMaterial();
		} else if (this.data.type == ItemEnum.equip) {
			this.setEquip();
		} else if (this.data.type == ItemEnum.role) {
			this.setRole();
		}else if (this.data.type == ItemEnum.user) {
			this.setUser();
		}
		this.numTx.node.active = this.isNumShow;
	}

	setMaterial() {
		this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
		this.load('common', `texture/icon/item/item_1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
			this.icoImg.spriteFrame = res;
		})
		this.numTx.string = this.data.count;
	}

	setEquip() {
		this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
		this.load('common', `texture/icon/equips/equip1_${this.data.conf.Slot}1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
			this.icoImg.spriteFrame = res;
		})
		this.numTx.string = this.data.count;
	}

	setRole() {
		this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
		this.load('common', `texture/icon/hero/head_${this.data.conf.Id}/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
			this.icoImg.spriteFrame = res;
		})
		this.numTx.string = this.data.count;
		this.raceIco.active = true;
		this.load('common', `texture/image/race_ball_${this.data.conf.Race}/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
			this.raceIco.getComponent(Sprite).spriteFrame = res;
		})
	}

	setUser() {
		// this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
		// this.load('common', `texture/icon/item/item_1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
		// 	this.icoImg.spriteFrame = res;
		// })
		// this.numTx.string = this.data.count;
	}

	setSelectEnable(state: boolean) {
		this.selectBg.active = state;
	}

	setClickEnable(state: boolean) {
		this.isClickEnable = state;
	}

	setNumShow(state: boolean) {
		this.isNumShow = state;
	}

	setGray(state: boolean) {
		this.farmeBg.grayscale = state;
		this.icoImg.grayscale = state;
		this.raceIco.getComponent(Sprite).grayscale = state;
	}

	setClick(callFunc) {
		this.clickFunc = callFunc;
	}

	onClick() {
		this.isSelect = !this.isSelect;
		if (this.clickFunc) {
			this.clickFunc(this.data, this.isSelect);
		} else {
			if (this.isClickEnable) {
				Framework.layer.open(ViewID.ItemMsg, null, this.data);
			}
		}
	}
}