import { _decorator, Node } from 'cc';
import { ResKeeper } from '../../../framework/res/ResKeeper';
import { LoginMgr } from '../../common/LoginManager';
import { Framework } from '../../../framework/Framework';
import { ViewID } from '../../../framework/config/LayerConf';
import { AudioID } from '../../../framework/config/AudioConf';
import { UserData } from '../../data/UserData';
const { ccclass, property } = _decorator;

@ccclass('ServerItem')
export class ServerItem extends ResKeeper {
	private ServetData = null
	private server_list = null;
	protected onLoad() {

	}
	init(data, ServetData) {
		this.ServetData = ServetData;
		if (this.ServetData) {
			this.server_list = data;
			this.Label.name.string = this.server_list.name;
			// this.Label.index.string = this.server_list.id
		}
	}
	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;
		let _gateway_data = {
			openid: this.ServetData.openid,    //上面获取到的openid
			openkey: this.ServetData.openkey,    //上面获取到的openkey
			sid: this.server_list.id,        // 选择的服务器列表信息中的id
		}
		UserData.save_locally('choose_server', this.server_list);
		LoginMgr.getGateway(_gateway_data, this.server_list.host, () => {
			if (UserData['mark'].first_login && UserData['mark'].first_login == 1) {

					Framework.layer.open(ViewID.MainUI, () => {
						Framework.layer.close(ViewID.LoginUI);
						Framework.layer.close(ViewID.ServerList);
						Framework.layer.close(ViewID.LoadingUI);
					});

			} else {
				Framework.layer.open(ViewID.MainUI, () => {
					Framework.layer.close(ViewID.LoginUI);
					Framework.layer.close(ViewID.ServerList);
					Framework.layer.close(ViewID.LoadingUI);
				});
			}
		})
	}
}