package com.mgr.group.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.data.bean.AccountBean;
import com.data.cache.AccountCache;
import com.mgr.group.Global;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.StringUtil;
import com.taurus.permanent.TPServer;
import com.taurus.permanent.data.Session;

/**
 * 长连接用户
 * 
 */
public class User {
	public int uid;
	public String session_key;
	/**
	 * 合伙人等级
	 */
	public int partnerLev;
	/**
	 * 职位等级
	 */
	public int lev;
	
	public volatile Group 				group = null;
	/**
	 * 是否正在链接
	 */
	public volatile boolean	isConnect		= false;
	/**
	 * mpnet session
	 */
	public volatile Session	sender			= null;
	
	public long hp =0;
	public int mail_tip = 0;
	public int ban =0;

	public int queueid = 0;

	public int seeid = 0;

	public String gm_key;
	
	/**最后邀请时间*/
	public long last_invitation_time;
	/**拒绝邀请时间*/
	public long invitation_refuse_time;
	public int last_invitation_id;
	
	/**最后刷新时间*/
	public long last_refresh_time;
	
	public boolean updateOffline;
	
	private List<User> onlineList = new ArrayList<>();
	
	public ITArray getOnlineList(){
		if((System.currentTimeMillis() - last_refresh_time)>=2000) {
			last_refresh_time = System.currentTimeMillis();
			onlineList.clear();
			Collection<User> tem = group.userMap.values();
			for (User user : tem) {
				if (user!=this&&user.isConnect&&user.ban==0) {
					String room = Redis.use("group1_db0").hget(user.session_key, "room");
					if(StringUtil.isEmpty(room)) {
						onlineList.add(user);
					}
				}
			}
		}
		Collections.shuffle(onlineList);
		int size = Math.min(onlineList.size(), 9);
		ITArray arr = TArray.newInstance();
		for(int i=0;i<size;i++) {
			User u = onlineList.get(i);
			AccountBean acc = AccountCache.getAccount(u.session_key);
			ITObject obj = TObject.newInstance();
			obj.putInt("uid", acc.id);
			obj.putUtfString("nick", acc.nick);
			obj.putUtfString("portrait", acc.portrait);
			arr.addTObject(obj);
		}
		return arr;
		
	}
	
	/** 
	 * 设置玩家session
	 * @param sender
	 */
	public void setSender(Session sender) {
		// 服务器从崩溃中重启
		if (sender == null) {
			this.isConnect = false;
			return;
		}
		if (this.isConnect && this.sender != null&&this.sender!=sender) {
			Global.sessionMgr.delSession(this.sender);
			TPServer.me().getController().disconnect(this.sender);
		}
		// 已经连接
		this.sender = sender;
		Global.sessionMgr.putUser(sender, this);
		this.isConnect = true;
		this.updateOffline = false;
	}
	
	/**
	 * 返回结果
	 * @param params
	 * @param gid
	 * @param error
	 */
	public void response(ITObject params, int gid, int error) {
		if (!this.isConnect)
			return;
		if (error == 0) {
			TPServer.me().getController().sendResponse(gid, error, uid, params, this.sender);
		} else {
			TPServer.me().getController().sendResponse(gid, error, uid, null, this.sender);
		}
	}
}
