package extend.mj.player.rule;

import java.util.HashMap;

import com.game.Global;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;

import extend.mj.Config;
import extend.mj.EXPlayer;
import extend.mj.EXRoom;
import extend.mj.OpCard;
import extend.mj.OpenKong;
import extend.mj.RuleWeight;
import extend.mj.WinCardType;
import extend.mj.player.rulestate.PROtherWinState;
import extend.mj.player.rulestate.PRPongOpenKongState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;
import extend.mj.uitl.Paixing;

/**
 * ���Ƽ��
 * 
 */
public class RuleOtherWin implements IRuleBase {

	@Override
	public boolean condition(EXPlayer player) {
		EXRoom room = player.getRoom();
		boolean zimo = room.config.getBoolean(Config.ROOM_CONFIG_ZIMO);
		if(!zimo&&player.louhu)return false;
		if(room.activeSeat == player.seat)return false;
		boolean no_jiang = room.config.getBoolean(Config.ROOM_CONFIG_NO_JIANG);
		
		EXPlayer activePlayer = (EXPlayer)room.playerMapBySeat.get(room.activeSeat);
		boolean  qiang_kong = activePlayer.stateMachine.curState instanceof PRPongOpenKongState;
		OpenKong ok =  room.activeKong;
		boolean result = false;
		ITArray opcard = TArray.newInstance();
		HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
		if (ok != null) {
			for (int card : ok.drawList) {
				if (Paixing.checkWin(map, player.opCardList, player.cardInhand, card,true, room.difen_score)) {
					Paixing.putWinCardType(map, WinCardType.GANGSHANGPAO, 1,WinCardType.WIN_TYPE_SCORE);
					map.remove(WinCardType.XIAOHU);
					result = true;
					opcard.addInt(card);
				}
			}
		} else {
			boolean jiang = true;
			if(no_jiang&&(qiang_kong ||room.activeHaidi)) {
				jiang = false;
			}
			result = Paixing.checkWin(map, player.opCardList, player.cardInhand, room.activeCard,jiang, room.difen_score);
			opcard.addInt(room.activeCard);
		}
		if (result) {
			if (room.activeHaidi) {
				map.remove(WinCardType.XIAOHU);
				map.put(WinCardType.HAIDIPAO, new WinCardType(WinCardType.HAIDIPAO, 1,WinCardType.WIN_TYPE_SCORE));
			}
			
			boolean menqing = room.config.getBoolean("menqing");
			if(menqing) {
				boolean m_r = true;
				for (OpCard oc : player.opCardList) {
					if(oc.type !=RuleWeight.TYPE_SELF_KONG) {
						m_r = false;
						break;
					}
				}
				if(m_r) {
					map.remove(WinCardType.XIAOHU);
					map.put(WinCardType.TYPE_MENQING, new WinCardType(WinCardType.TYPE_MENQING, 1,WinCardType.WIN_TYPE_SCORE));
				}
			}
			
			if(qiang_kong) {
				map.remove(WinCardType.XIAOHU);
				map.put(WinCardType.QIANG_GANG_HU, new WinCardType(WinCardType.QIANG_GANG_HU, 1,WinCardType.WIN_TYPE_SCORE));
			}
			
			if(player.tianting) {
				map.remove(WinCardType.XIAOHU);
				map.put(WinCardType.TYPE_TIANTINGHU, new WinCardType(WinCardType.TYPE_TIANTINGHU, 1,WinCardType.WIN_TYPE_SCORE));
			}
			
			if(zimo&&map.containsKey(WinCardType.XIAOHU))return false;

			Tip tip = new Tip(opcard.getInt(0),opcard, RuleWeight.WIN,this, RuleWeight.TYPE_WIN);
			tip.group_type = RuleWeight.GROUO_WIN;
			tip.winMap = map;
			player.tipMgr.addTip(tip);
		}

		return result;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		player.getRoom().activeCard = tip.card;
		player.winMap = tip.winMap;
		player.stateMachine.changeState(Global.getState(PROtherWinState.class));
	}

}
