import { _decorator, Button, instantiate, Label, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { ResKeeper } from '../../../framework/res/ResKeeper';
import { StringUtil } from '../../../framework/util/StringUtil';
import { Framework } from '../../../framework/Framework';
import { HeroListCard } from './HeroListCard';
import { EquipManager } from '../../manager/EquipManager';
import { ViewID } from '../../../framework/config/LayerConf';
const { ccclass, property } = _decorator;

export interface heroListItemData {
	race: number;
	heros: [];
	equips: {};
}

@ccclass('HeroListItem')
export class HeroListItem extends ResKeeper {
	@property({ type: Label, tooltip: "阵营文字" })
	raceName: Label = null;

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

	@property({ type: [Node], tooltip: "装备" })
	equips: Node[] = [];

	@property({ type: Label, tooltip: "羁绊总星级" })
	fetterStarNum: Label = null;

	@property({ type: Label, tooltip: "羁绊按钮文字" })
	fetterBtnTx: Label = null;

	@property({ type: Node, tooltip: "英雄容器" })
	herosNode: Node = null;

	private data = {};

	protected onLoad() {

	}

	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: heroListItemData) {
		this.data = data;
		this.raceName.string = StringUtil.getLanguageData('阵营类型' + data.race);
		this.load('common', `texture/image/race_ball_${data.race}/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
			this.raceIco.spriteFrame = res;
		})
		this.updateHero();
		this.updateEquip();
	}

	updateHero() {
		this.herosNode.removeAllChildren();
		for (const element of this.data['heros']) {
			this.load('hero', `prefab/HeroListCard`, Prefab, (pre: Prefab) => {
				let heroCard = instantiate(pre);
				heroCard.name = 'hero_' + element.conf.Id;
				this.herosNode.addChild(heroCard);
				heroCard.getComponent(HeroListCard).refreshItem(element);
				heroCard.active = true;
			})
		}
	}

	//更新装备
	updateEquip() {
		let equipsData = EquipManager.getEquipRaceSlotGroup(this.data['race']);
		for (const slot in this.equips) {
			if (Object.prototype.hasOwnProperty.call(this.equips, slot)) {
				const node = this.equips[slot];
				let bg = node.getChildByName('bg').getComponent(Sprite);
				let noneNode = node.getChildByName('none_node');
				let noneAdd = noneNode.getChildByName('add_ico');
				noneAdd.active = equipsData[Number(slot) + 1].length > 0;
				let equipNode = node.getChildByName('equip_node');
				let ico = equipNode.getChildByName('ico').getComponent(Sprite);
				let starNode = equipNode.getChildByName('star_node');
				let starTx = starNode.getChildByName('num').getComponent(Label);
				let levelTx = equipNode.getChildByName('level_tx').getComponent(Label);
				let eData = this.data['equips'][Number(slot) + 1];
				if (eData) {
					noneNode.active = false;
					equipNode.active = true;
					this.load('common', `texture/background/frame_bg_${eData.conf.Quality}/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
						bg.spriteFrame = res;
					})
					this.load('common', `texture/icon/equips/equip1_${eData.conf.Slot}1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
						ico.spriteFrame = res;
					})
					if (eData.conf['Quality'] < 5) {
						starNode.active = false;
					} else {
						starNode.active = true;
						starTx.string = String(eData.star);
					}
					let lvConf = EquipManager.getEquipLevelByExp(eData.exp);
					levelTx.string = 'Lv.' + lvConf.Id;
				} else {
					noneNode.active = true;
					equipNode.active = false;
					this.load('common', `texture/background/frame_bg_0/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
						bg.spriteFrame = res;
					})
				}
			}
		}
	}

	onClickEquip(event: Event, customStr) {
		let eData = this.data['equips'][customStr];
		if (eData) {
			let args = { race: this.data['race'], slot: customStr, equip: eData }
			Framework.layer.open(ViewID.EquipOperate, null, args);
		} else {
			let args = { race: this.data['race'], slot: customStr }
			Framework.layer.open(ViewID.EquipChoose, null, args);
		}

	}
}