import { _decorator, Label, tween, UIOpacity } from 'cc';
import { BaseView } from '../../../framework/layer/BaseView';
import { StringUtil } from '../../../framework/util/StringUtil';
import List from '../../../framework/list/List';
import { Framework } from '../../../framework/Framework';
import { Equip } from '../../data/EquipData';
import { RoleData } from '../../data/RoleData';
import { HeroFateItem } from './HeroFateItem';
import { GameEvent } from '../../data/GameEvent';

const { ccclass, property } = _decorator;

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

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

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

	private _fettersList = [];

	start() {

	}
	protected onLoad() {
		super.onLoad();
		this.closeTips.string = StringUtil.getLanguageData('点击空白关闭');
		this.titieTx.string = StringUtil.getLanguageData('英雄羁绊');

		this.closeTips.node.getComponent(UIOpacity).opacity = 0;

		Framework.event.addEvent(GameEvent.FateChange, () => {
			this.updateUI();
		}, 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.updateUI();
			
	}

	//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);
		}
	}

	updateUI() {
		this._fettersList = [];
		let data = RoleData.fate;
		for (const key in data) {
			if (Object.prototype.hasOwnProperty.call(data, key)) {
				const element = data[key];
				this._fettersList.push(element);
			}
		}
		this._fettersList.sort((a, b) => {
            return Number(a.id) > Number(b.id) ? -1 : 1;
        });

		this.itemList.numItems = this._fettersList.length;
	}

	onEventList(item, idx) {
		item.getComponent(HeroFateItem).refreshItem(this._fettersList[idx]);
	}
}