import { _decorator, Label, Node, Sprite, SpriteFrame } from 'cc';
import { ResKeeper } from '../../../framework/res/ResKeeper';
import { Framework } from '../../../framework/Framework';
import { GameEvent } from '../../data/GameEvent';
import { StringUtil } from '../../../framework/util/StringUtil';
import { AccountData } from '../../data/AccountData';

const { ccclass, property } = _decorator;

@ccclass('ServerItem')
export class ServerItem extends ResKeeper {
	@property({ type: Label, tooltip: "名字" })
	nameTx: Label = null;

	@property({ type: Sprite, tooltip: "状态" })
	stateIco: Sprite = null;

	@property({ type: Node, tooltip: "当前背景" })
	nowBg: Node = null;
	@property({ type: Label, tooltip: "当前文字" })
	nowTx: Label = null;

	@property({ type: Label, tooltip: "角色信息" })
	userMsg: Label = null;

	@property({ type: [SpriteFrame], tooltip: "状态图" })
	stateFrame: SpriteFrame[] = [];
	
	private servetData = null

	protected onLoad() {

	}

	init(data) {
		this.servetData = data;
		this.nameTx.string = this.servetData.name;
		this.stateIco.spriteFrame = this.stateFrame[this.servetData.status];
		this.nowTx.string = StringUtil.getLanguageData('当前');
		let selectSid = AccountData.getSid();
		this.nowBg.active = (this.servetData.sid == selectSid);
		let str = "";
		if(this.servetData.role_name){
			str = `[${this.servetData.role_name}]${StringUtil.getLanguageData(`${this.servetData.lv}级`)}`
		}
		this.userMsg.string = str;
	}

	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;
		AccountData.setSid(this.servetData.sid,false);
		Framework.event.fireEvent(GameEvent.SelectServer);
	}
}