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.PRSelfWinState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;
import extend.mj.uitl.Paixing;

/**
 * �������
 * 
 *
 * 2017��8��30��
 * RuleSelfWin.java
 */
public class RuleSelfWin implements IRuleBase {

	@Override
	public boolean condition(EXPlayer player) {
		EXRoom room = player.getRoom();
		if(room.activeSeat != player.seat)return false;
		boolean no_jiang = room.config.getBoolean(Config.ROOM_CONFIG_NO_JIANG);
		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) {
				//jefe 杠上开花
				if (Paixing.checkWin(map, player.opCardList, player.cardInhand, card,true, room.difen_score)) {
					Paixing.putWinCardType(map, WinCardType.GANGSHANGHUA, 1,WinCardType.WIN_TYPE_SCORE);
					map.remove(WinCardType.XIAOHU);
					result = true;
					opcard.addInt(card);
				}
			}
		} else {
			player.cardInhand.remove((Integer) player.drawCard);
			boolean jiang = true;
			if(no_jiang&&room.activeHaidi) {
				jiang = false;
			}
			result = Paixing.checkWin(map, player.opCardList, player.cardInhand, player.drawCard,jiang, room.difen_score);
			player.cardInhand.add(player.drawCard);
			opcard.addInt(player.drawCard);
		}
		if (result) {
			if (room.activeHaidi) {
				map.remove(WinCardType.XIAOHU);
				map.put(WinCardType.HAIDI, new WinCardType(WinCardType.HAIDI, 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));
			}
			
			boolean tianhu = room.config.getBoolean(Config.ROOM_CONFIG_NATIVE_HU);
			if(tianhu&&room.bankerSeat == player.seat && player.outCardCount ==0) {
				map.remove(WinCardType.XIAOHU);
				map.put(WinCardType.TYPE_TIANHU, new WinCardType(WinCardType.TYPE_TIANHU, 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));
				}
			}
			
			
			Tip tip = new Tip(opcard.getInt(0),opcard, RuleWeight.SELF_WIN,this, RuleWeight.TYPE_WIN);
			tip.group_type = RuleWeight.GROUO_WIN;
			tip.winMap = map;
			tip.self = true;
			player.tipMgr.addTip(tip);
		}

		return result;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		player.winCard = tip.card;
		player.drawCard = tip.card;
		player.winMap = tip.winMap;
		player.stateMachine.changeState(Global.getState(PRSelfWinState.class));

	}

}
