package com.backfire.room;

import java.util.ArrayList;

import android.os.Handler;
import android.os.Message;

import com.backfire.arithmetic.Card;
import com.backfire.arithmetic.CardAssist;
import com.backfire.room.data.HandlerMsg;
import com.backfire.room.data.Player;
import com.backfire.room.data.RoomInfo;
import com.backfire.room.data.State;
import com.backfire.room.data.State.DepositS;
import com.backfire.room.data.State.GiveUpLandlordS;

/**   
 * @Title: MsgDispatcher.java
 * @Description: 消息分发器
 * @author Potter
 * @date 2013年10月24日 下午4:53:36
 * @version V2.0   
 */
public class MsgDispatcher extends Handler{
	
	RoomActivity mContext=null;
	public MsgDispatcher(RoomActivity mContext){
		this.mContext=mContext;
	}
	
	@Override
	public void handleMessage(Message msg) {
		int cmd=msg.what;
		if(cmd<=0)return;
		boolean isHandle=true;
		switch (cmd) {
		case HandlerMsg.MSG_READY:
			mContext.getRoom().getSocketSender().readyReq();
			break;
		case HandlerMsg.MSG_LEAVE:
		case HandlerMsg.MSG_CENTER_LEAVE:
		case HandlerMsg.MSG_SETTLE_ACCOUNT_EXIT:
			mContext.getRoom().getSocketSender().logoutReq();
			break;
		case HandlerMsg.MSG_ONE:
			RoomInfo.getInstance().setCurrRatio(1);
			mContext.getRoom().getSocketSender().qiangdizhuReq(GiveUpLandlordS.GIVEUP_N);
			break;
		case HandlerMsg.MSG_TWO:
			RoomInfo.getInstance().setCurrRatio(2);
			mContext.getRoom().getSocketSender().qiangdizhuReq(GiveUpLandlordS.GIVEUP_N);
			break;
		case HandlerMsg.MSG_THREE:
			RoomInfo.getInstance().setCurrRatio(3);
			mContext.getRoom().getSocketSender().qiangdizhuReq(GiveUpLandlordS.GIVEUP_N);
			break;
		case HandlerMsg.MSG_GRAB:
			mContext.getRoom().getSocketSender().qiangdizhuReq(GiveUpLandlordS.GIVEUP_N);
			break;
		case HandlerMsg.MSG_NO_GRAB:
			mContext.getRoom().getSocketSender().qiangdizhuReq(GiveUpLandlordS.GIVEUP_Y);
			break;
		case HandlerMsg.MSG_PASS:
		{
			RoomInfo.getInstance().getSelfPlayer().setAction(State.UserAction.PASS);
			ArrayList<Card> cards=mContext.getRoom().getCardBtnManager().getSelectCards();
			if(cards!=null){
				ArrayList<Card> tmpCards = null;
				synchronized (cards) {
					tmpCards=CardAssist.copyList(cards);
				}
				mContext.getRoom().getSocketSender().playActionReq(tmpCards);
			}
		}
			break;
		case HandlerMsg.MSG_RE_SELECT:
			break;
		case HandlerMsg.MSG_PROMPT:
			break;
		case HandlerMsg.MSG_PLAY:
		{
			RoomInfo.getInstance().getSelfPlayer().setAction(State.UserAction.PLAY);
			ArrayList<Card> cards=mContext.getRoom().getCardBtnManager().getSelectCards();
			if(cards!=null){
				ArrayList<Card> tmpCards = null;
				synchronized (cards) {
					tmpCards=CardAssist.copyList(cards);
				}
				mContext.getRoom().getSocketSender().playActionReq(tmpCards);
			}
		}
			break;
		case HandlerMsg.MSG_DISPOSIT:
			mContext.getRoom().getSocketSender().depositReq(DepositS.deposit_Y);
			break;
		case HandlerMsg.MSG_CANCEL_DESPOSITE:
			mContext.getRoom().getSocketSender().depositReq(DepositS.deposit_N);
			break;
		case HandlerMsg.MSG_TOOLS:
			break;
		case HandlerMsg.MSG_CHAT:
			mContext.showChatWindow();
			break;
		case HandlerMsg.MSG_SET:
			mContext.showSetWindow();
			break;
		case HandlerMsg.MSG_ADD_VCOIN:
			mContext.addVcoin();
			break;
		case HandlerMsg.MSG_RECHARGE:
			break;
		case HandlerMsg.MSG_SETTLE_ACCOUNT_REFIGHT:
			mContext.resetGame();
			break;
		default:
			isHandle=false;
			break;
		}
		if(!isHandle){
			RoomInfo roomInfo=RoomInfo.getInstance();
			for(int i=0;i<3;i++){
				Player player = roomInfo.getPlayer(i+1);
				if(player==null)continue;
				int seatId=0;
				int seatIdx=0;
				synchronized (player) {
					seatId=player.getSeatId();
					seatIdx=roomInfo.getSeatIdx(seatId);
				}
				if (seatIdx > HandlerMsg.MSG_INFOS.length || seatIdx < 0){
					continue;
				}
				int tmpMsg=HandlerMsg.MSG_INFOS[seatIdx];
				if(tmpMsg==cmd){
					mContext.showOtherMsg(seatId);
					break;
				}
			}
		}
		super.handleMessage(msg);
	}

}
