package extend.mj;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.game.Global;

import extend.mj.player.rule.RuleChow;
import extend.mj.player.rule.RuleHaidi;
import extend.mj.player.rule.RuleOtherKong;
import extend.mj.player.rule.RuleOtherOpenKong;
import extend.mj.player.rule.RuleOtherWin;
import extend.mj.player.rule.RulePong;
import extend.mj.player.rule.RulePongKong;
import extend.mj.player.rule.RulePongOpenKong;
import extend.mj.player.rule.RuleQSWin;
import extend.mj.player.rule.RuleSelfKong;
import extend.mj.player.rule.RuleSelfOpenKong;
import extend.mj.player.rule.RuleSelfWin;
import extend.mj.player.rule.RuleZTWin;
import extend.mj.player.state.EXPlayerDisCardTipState;
import extend.mj.player.state.EXPlayerDrawTipState;
import extend.mj.player.state.EXPlayerHaidiTipState;
import extend.mj.player.state.EXPlayerKongWinState;
import extend.mj.player.state.EXPlayerQSWinTipState;
import extend.mj.player.state.EXPlayerTipState;
import extend.mj.player.state.EXPlayerZTWinTipState;
import extend.mj.tip.IRuleBase;

/**
 * 
 */
public class PlayerRuleManager {


	public static final int						DRAW_RULE				= 1;

	public static final int						OTHER_DISCARD_RULE		= 2;

	public static final int						KONG_HU_RULE			= 3;

	public static final int						QS_HU_RULE				= 4;

	public static final int						OPENKONG_RULE				= 5;

	public static final int						HAIDI_RULE				= 6;

	public static final int						CHOW_PONG_DISCARD_RULE	= 7;

	public static final int						ZT_HU_RULE				= 8;


	public HashMap<Integer, List<IRuleBase>>	ruleMap					= null;
	public HashMap<Integer, EXPlayerTipState>	tipMap					= null;

	public PlayerRuleManager() {
		ruleMap = new HashMap<Integer, List<IRuleBase>>();

		tipMap = new HashMap<Integer, EXPlayerTipState>();
		tipMap.put(PlayerRuleManager.DRAW_RULE, (EXPlayerTipState) Global.getState(EXPlayerDrawTipState.class));
		tipMap.put(PlayerRuleManager.OTHER_DISCARD_RULE, (EXPlayerTipState) Global.getState(EXPlayerDisCardTipState.class));
		tipMap.put(PlayerRuleManager.KONG_HU_RULE, (EXPlayerTipState) Global.getState(EXPlayerKongWinState.class));
		tipMap.put(PlayerRuleManager.CHOW_PONG_DISCARD_RULE, (EXPlayerTipState) Global.getState(EXPlayerDrawTipState.class));
		tipMap.put(PlayerRuleManager.QS_HU_RULE, (EXPlayerTipState) Global.getState(EXPlayerQSWinTipState.class));
		tipMap.put(PlayerRuleManager.OPENKONG_RULE, (EXPlayerTipState) Global.getState(EXPlayerDisCardTipState.class));
		tipMap.put(PlayerRuleManager.HAIDI_RULE, (EXPlayerTipState) Global.getState(EXPlayerHaidiTipState.class));
		tipMap.put(PlayerRuleManager.ZT_HU_RULE, (EXPlayerTipState) Global.getState(EXPlayerZTWinTipState.class));
		
		List<IRuleBase> drawRuleList = new ArrayList<IRuleBase>();
		drawRuleList.add(new RuleSelfKong());
		drawRuleList.add(new RuleSelfOpenKong());
		drawRuleList.add(new RulePongKong());
		drawRuleList.add(new RulePongOpenKong());
		drawRuleList.add(new RuleSelfWin());
		ruleMap.put(PlayerRuleManager.DRAW_RULE, drawRuleList);

		List<IRuleBase> otherDiscardList = new ArrayList<IRuleBase>();
		otherDiscardList.add(new RuleChow());
		otherDiscardList.add(new RulePong());
		otherDiscardList.add(new RuleOtherKong());
		otherDiscardList.add(new RuleOtherOpenKong());
		otherDiscardList.add(new RuleOtherWin());
		ruleMap.put(PlayerRuleManager.OTHER_DISCARD_RULE, otherDiscardList);

		List<IRuleBase> konghuList = new ArrayList<IRuleBase>();
		konghuList.add(new RuleOtherWin());
		ruleMap.put(PlayerRuleManager.KONG_HU_RULE, konghuList);
		
		List<IRuleBase> qshuList = new ArrayList<IRuleBase>();
		qshuList.add(new RuleQSWin());
		ruleMap.put(PlayerRuleManager.QS_HU_RULE, qshuList);
		
		List<IRuleBase> cpDiscardRuleList = new ArrayList<IRuleBase>();
		cpDiscardRuleList.add(new RuleSelfKong());
		cpDiscardRuleList.add(new RuleSelfOpenKong());
		cpDiscardRuleList.add(new RulePongKong());
		cpDiscardRuleList.add(new RulePongOpenKong());
		ruleMap.put(PlayerRuleManager.CHOW_PONG_DISCARD_RULE, cpDiscardRuleList);
		
		List<IRuleBase> openkongList = new ArrayList<IRuleBase>();
		openkongList.add(new RuleChow());
		openkongList.add(new RulePong());
		openkongList.add(new RuleOtherKong());
		openkongList.add(new RuleOtherOpenKong());
		openkongList.add(new RulePongOpenKong());
		openkongList.add(new RuleOtherWin());
		openkongList.add(new RuleSelfWin());
		ruleMap.put(PlayerRuleManager.OPENKONG_RULE, openkongList);
		
		List<IRuleBase> haidiList = new ArrayList<IRuleBase>();
		haidiList.add(new RuleHaidi());
		ruleMap.put(PlayerRuleManager.HAIDI_RULE, haidiList);
		
		List<IRuleBase> zthuList = new ArrayList<IRuleBase>();
		zthuList.add(new RuleZTWin());
		ruleMap.put(PlayerRuleManager.ZT_HU_RULE, zthuList);
		
	}

	public boolean condition(int type, EXPlayer player) {
		return condition(type, player, true);
	}

	public boolean condition(int type, EXPlayer player, boolean tonextState) {
		player.getRoom().actionWinList.clear();
		List<IRuleBase> ruleList = this.ruleMap.get(type);
		boolean result = false;

		for (IRuleBase rule : ruleList) {
			result = rule.condition(player) || result;
		}

		if (result) {
			player.stateMachine.changeState(this.tipMap.get(type));
		} else {
			if (tonextState)
				player.stateMachine.toNextState();
		}
		return result;
	}

}
