import { _decorator, Label, Node } from 'cc';
import { BaseView } from '../../../framework/layer/BaseView';
import List from '../../../framework/list/List';
import { StringUtil } from '../../../framework/util/StringUtil';
import { HeroListItem, heroListItemData } from './HeroListItem';
import { RoleManager } from '../../manager/RoleManager';
import { EquipManager } from '../../manager/EquipManager';
import { Framework } from '../../../framework/Framework';
import { GameEvent } from '../../data/GameEvent';
const { ccclass, property } = _decorator;

@ccclass('HeroList')
export class HeroList extends BaseView {
	@property({ type: Label, tooltip: "标题" })
	titleTx: Label = null;

	@property({ type: List, tooltip: "滑动容器" })
	sv: List = null;

	private _herosData: Array<heroListItemData> = [];
	protected onLoad() {
		super.onLoad();
		this.titleTx.string = StringUtil.getLanguageData('英雄列表');

		let herodata = RoleManager.getRolesRaceGroup();
		let equipData = EquipManager.getEquipWearRaceGroup();
		for (let index = 0; index < 4; index++) {
			let t: heroListItemData = { race: index + 1, heros: herodata[index + 1], equips: equipData[index + 1] }
			this._herosData.push(t);
		}
		this.sv.numItems = this._herosData.length;

		Framework.event.addEvent(GameEvent.EquipWearChange, (race) => {
			let herodata = RoleManager.getRolesRaceGroup();
			let equipData = EquipManager.getEquipWearRaceGroup();
			let t: heroListItemData = { race: race, heros: herodata[race], equips: equipData[race] }
			// this._herosData[race - 1] = t;
			let item = this.sv.content.getChildByName('race_' + race);
			if(item){
				item.getComponent(HeroListItem).refreshItem(t);
			}
		}, this);
	}

	protected onDestroy() {

	}

	//UI开打时会调用,如果有初始化代码应该放到此函数
	onOpen() {

	}

	//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 == 'close_btn') {
			Framework.layer.close(this)
		}

	}

	onEventList(item, idx) {
		item.name = 'race_' + this._herosData[idx].race;
		item.getComponent(HeroListItem).refreshItem(this._herosData[idx]);
	}
}