import { _decorator, instantiate, Label, Node, Prefab, tween, UIOpacity } from 'cc';
import { BaseView } from '../../../framework/layer/BaseView';
import { EquipChooseItem } from './EquipChooseItem';
import { StringUtil } from '../../../framework/util/StringUtil';
import List from '../../../framework/list/List';
import { EquipManager } from '../../manager/EquipManager';
import { Framework } from '../../../framework/Framework';
import { Equip } from '../../data/EquipData';
import { CommonItem } from '../common/CommonItem';
import { AttrAddTypeEnum, AttrConf } from '../../common/InterfaceAddEnum';
import { GameEvent } from '../../data/GameEvent';
const { ccclass, property } = _decorator;

@ccclass('EquipChoose')
export class EquipChoose extends BaseView {
	@property({ type: Label, tooltip: "关闭提示" })
	closeTips: Label = null;

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

	@property({ type: Node, tooltip: "穿戴装备节点" })
	wearNode: Node = null;

	@property({ type: Label, tooltip: "列表空提示" })
	equipListNoneTx: Label = null;

	@property({ type: List, tooltip: "列表" })
	equipList: List = null;

	@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: Label, tooltip: "穿戴文字" })
	wearTx: Label = null;

	private _curRace: number = 0;
	private _curSlot: number = 0;
	private _equipsData: any[] = [];
	private _curEquip: Equip = null;

	start() {

	}
	protected onLoad() {
		super.onLoad();
		this.closeTips.string = StringUtil.getLanguageData('点击空白关闭');
		this.titieTx.string = StringUtil.getLanguageData('装备选择');
		this.wearTx.string = StringUtil.getLanguageData('当前穿戴');
		this.fightTitie.string = StringUtil.getLanguageData('战力') + ': ';
		this.equipListNoneTx.string = StringUtil.getLanguageData('暂无可用装备');
		this.closeTips.node.getComponent(UIOpacity).opacity = 0;
		this.wearNode.active = false;

		Framework.event.addEvent(GameEvent.EquipWearChange, () => {
			Framework.layer.close(this);
		}, this);
	}

	protected onDestroy() {

	}

	//UI开打时会调用,如果有初始化代码应该放到此函数
	onOpen(data) {
		tween(this.closeTips.node.getComponent(UIOpacity))
			.to(1, { opacity: 255 })
			.to(1.2, { opacity: 10 })
			.union()
			.repeatForever()
			.start()

		this._curRace = data.race;
		this._curSlot = data.slot;
		let eData = EquipManager.getEquipRaceSlotGroup(data.race)[data.slot];
		if (data.equip) {
			this._curEquip = data.equip;
			this.wearNode.active = true;
			this.updateWear();
			this._equipsData = eData.filter((element) => element.id !== data.equip.id);
		}else{
			this._equipsData = eData;
		}
		this.equipListNoneTx.node.active = this._equipsData.length == 0;
		this.equipList.numItems = this._equipsData.length;
	}

	//UI关闭时会调用,该函数在onDestroy前调用
	onClose() {

	}

	//框架管理UI层级时会调用,可根据UI情况修改
	onShow() {
		super.onShow();
	}

	//框架管理UI层级时会调用,可根据UI情况修改
	onHide() {
		super.onHide();
	}

	//UI事件处理
	private onTouchButton(event: Event) {
		//Framework.audio.playEffect(AudioID.Click);
		let target: any = event.target;
		if (target.name == 'mask') {
			Framework.layer.close(this);
		}
	}

	updateWear() {

		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(this._curEquip);
		})

		this.nameTx.string = `${StringUtil.getLanguageData('装备名称')}: ${StringUtil.getLanguageData(this._curEquip.conf['Name'])}`;

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

	onEventList(item, idx) {
		item.getComponent(EquipChooseItem).refreshItem(this._equipsData[idx], this._curEquip);
	}

}