package com.group;

import com.data.bean.AccountBean;
import com.data.bean.GroupBean;
import com.data.bean.GroupMemberBean;
import com.data.bean.GroupPlayBean;
import com.data.cache.AccountCache;
import com.data.cache.GroupCache;
import com.data.util.ErrorCode;
import com.group.service.GroupRoomService;
import com.taurus.core.entity.ITObject;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.routes.Action;
import com.taurus.core.routes.IController;
import com.taurus.core.routes.Interceptor;
import com.taurus.core.util.StringUtil;
import com.taurus.web.Controller;
import com.taurus.web.WebException;

public class WebInterceptor implements Interceptor{
	/**
	 * 验证session
	 */
	public static final int V_SESSION = 1;
	/**
	 * 验证圈子
	 */
	public static final int V_GROUP = 2;
	/**
	 * 验证圈主
	 */
	public static final int V_GROUP_OWNER = 4;
	/**
	 * 验证合伙人
	 */
	public static final int V_GROUP_PARTNER= 8;
	/**
	 * 验证玩法
	 */
	public static final int V_GROUP_PLAY= 16;
	/**
	 * 验证管理员
	 */
	public static final int V_GROUP_MGR= 32;
	/**
	 * 验证管理员或者合伙人
	 */
	public static final int V_GROUP_MGR_OR_PARTNER= 64;
	/**
	 * 检测玩家在不在房间
	 */
	public static final int V_GROUP_CHECK_ROOM= 128;

	/**
	 * 验证圈主或合伙人
	 */
	public static final int V_GROUP_OWNER_OR_MGR_OR_PARTNER = 256;
	
	@Override
	public void intercept(Action action, IController controller, Object... args) throws Exception {
		Controller ctr = (Controller) controller;
		int validate = action.getActionKeyObj().validate();
		String session = ctr.getSession();
		String token = ctr.getTokens();
		if (StringUtil.isNotEmpty(token) && StringUtil.isNotEmpty(session))
		{
			String token_session = Redis.use("group1_db0").hget(token, "user");
			if (StringUtil.isEmpty(token_session))
			{
				throw new WebException(ErrorCode._NO_SESSION);
			}
			else {
				if (!token_session.equals(session))
				{
					throw new WebException(ErrorCode._NO_SESSION);
				}
			}
		}
		else {
			throw new WebException(ErrorCode._NO_SESSION);
		}

		if ((validate&V_SESSION)!=0 ) {
			if (!Redis.use("group1_db0").exists(session)) {
				throw new WebException(ErrorCode._NO_SESSION);
			}
			String opt = Redis.use("group1_db0").hget(session, "opt");
//			background  del  sign
			if (!StringUtil.isEmpty(opt) && Integer.parseInt(opt) == 1) {
				throw new WebException(ErrorCode._NO_SESSION);
			}
		}
		
		if((validate&V_GROUP)!=0) {
			ITObject reqData = ctr.getParams();
			if(!reqData.containsKey("id")) {
				throw new WebException(ErrorCode.GROUP_NO_EXIST);
			}
			int groupId = reqData.getInt("id");
			GroupBean gb = GroupCache.getGroup(groupId);
			if (gb == null) {
				throw new WebException(ErrorCode.GROUP_NO_EXIST);
			}
			if (gb.stop == 1) {
				throw new WebException(ErrorCode.GROUP_STOP_SERVICE);
			}
			AccountBean acc = AccountCache.getAccount(session);
			if((validate&V_GROUP_CHECK_ROOM)!=0) {
				boolean checkRoom = GroupRoomService.checkRoom(groupId, acc.id);
				if(checkRoom) {
					throw new WebException(ErrorCode.GROUP_EXIST_ROOMS);
				}
			}
			boolean v_o_p = (validate&V_GROUP_OWNER_OR_MGR_OR_PARTNER)!=0;
			if (v_o_p)
			{
				if(gb.owner != acc.id) {
					GroupMemberBean gmb = GroupCache.getMember(groupId, acc.id);
					if (gmb==null) {
						throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
					}

					if (gmb.lev >= 3)
					{
						if(gmb.partnerLev<=0) {
							throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
						}
					}
				}
			}
			else if((validate&V_GROUP_OWNER)!=0) {
				if(gb.owner != acc.id) {
					throw new WebException(ErrorCode.GROUP_NOT_OWNER);
				}
			}else {
				GroupMemberBean gmb = GroupCache.getMember(groupId, acc.id);
				if (gmb==null) {
					throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
				}
				int count = 0;
				boolean v_m_p = (validate&V_GROUP_MGR_OR_PARTNER)!=0;

				if((validate&V_GROUP_PARTNER)!=0 || v_m_p) {
					if(gmb.partnerLev<=0) {
						if(v_m_p) {
							count ++;
						}else {
							throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
						}
					}
				}
				
				if((validate&V_GROUP_MGR)!=0 || v_m_p) {
					if(gmb.lev == 3) {
						if(v_m_p) {
							count ++;
						}else {
							throw new WebException(ErrorCode.GROUP_MGR_EXIST);	
						}
					}
				}
				
				if(v_m_p && count==0) {
					throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
				}
			}
			
			
			if((validate&V_GROUP_PLAY)!=0) {
				int pid = reqData.getInt("pid");
				GroupPlayBean gpb = GroupCache.getPlay(groupId, pid);
				if (gpb==null) {
					throw new WebException(ErrorCode.GROUP_PLAY_EXIST);
				}
			}

			
		}
		
		
		
	}

	

}
