import { _decorator, instantiate, Label, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { ResKeeper } from '../../../framework/res/ResKeeper';
import { StringUtil } from '../../../framework/util/StringUtil';
import { EquipManager } from '../../manager/EquipManager';
import { Framework } from '../../../framework/Framework';
import { AttrAddTypeEnum, AttrEnum } from '../../common/InterfaceAddEnum';
import { CommonItem } from '../common/CommonItem';
const { ccclass, property } = _decorator;

@ccclass('EquipChooseItem')
export class EquipChooseItem extends ResKeeper {
	@property({ type: Node, tooltip: "装备节点" })
	itemNode: Node = null;

	@property({ type: Label, tooltip: "装备名" })
	nameTx: Label = null;

	@property({ type: Label, tooltip: "战力标题" })
	fightTitie: Label = null;

	@property({ type: Label, tooltip: "战力文字" })
	fightTx: Label = null;

	@property({ type: Sprite, tooltip: "穿戴按钮图" })
	wearBtnSp: Sprite = null;

	@property({ type: Label, tooltip: "穿戴按钮文字" })
	wearBtnTx: Label = null;

	private data = {};

	private wearEquip = null;
	protected onLoad() {
		this.fightTitie.string = StringUtil.getLanguageData('战力') + ': ';
	}

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

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

	}

	refreshItem(data, wearEquip) {
		this.data = data;
		this.wearEquip = wearEquip;
		if (wearEquip) {
			this.wearBtnTx.string = StringUtil.getLanguageData('替换');
			this.load('common', `texture/button/blue_btn_1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
				this.wearBtnSp.spriteFrame = res;
			})
		} else {
			this.wearBtnTx.string = StringUtil.getLanguageData('装备');
			this.load('common', `texture/button/yellow_btn_1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
				this.wearBtnSp.spriteFrame = res;
			})
		}

		this.load('common', `prefab/CommonItem`, Prefab, (pre: Prefab) => {
			let item = instantiate(pre);
			this.itemNode.addChild(item);
			item.getComponent(CommonItem).setClickEnable(false);
			item.getComponent(CommonItem).setNumShow(false);
			item.getComponent(CommonItem).refreshItem(data);
		})
		
		this.nameTx.string = `${StringUtil.getLanguageData('装备名称')}: ${StringUtil.getLanguageData(data.conf['Name'])}`;

		let attrStr = 0;
		for (let index = 1; index <= 2; index++) {
			let Stat = data.conf['Stat' + index];
			if (Stat != 0) {
				let attr = Stat.split(':');
				let attrConf = AttrEnum[attr[0]]
				let value = attrConf.type == AttrAddTypeEnum.reality ? Number(attr[1]) :
					100 * Number(attr[1])
				attrStr = attrStr + value;
			}
		}
		this.fightTx.string = String(attrStr);
	}

	//UI事件处理
	private onTouchButton(event: Event) {
		//Framework.audio.playEffect(AudioID.Click);
		let target: any = event.target;
		if (target.name == 'wear_btn') {
			let args = { eid: this.data['id'], slot: this.data['conf']['Slot'], race: this.data['conf']['Race'] };
			EquipManager.sendWearMsg(args, () => {
				Framework.tips.setTips(StringUtil.getLanguageData('穿戴成功!'));
			})
			// if (this.wearEquip) {

			// }else{

			// }
		}
	}
}