package extend.mj;

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

import com.game.Global;

import extend.mj.player.rule.*;
import extend.mj.player.state.EXPlayerDisCardTipState;
import extend.mj.player.state.EXPlayerDrawTipState;
import extend.mj.player.state.EXPlayerKongWinState;
import extend.mj.player.state.EXPlayerTipState;
import extend.mj.tip.IRuleBase;

/**
 * �����������
 * 
 * 
 *
 *         2017��9��19�� PlayerRuleManager.java
 */
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						CHOW_PONG_DISCARD_RULE	= 7;

	public static final int						KONG_DRAW_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.KONG_DRAW_RULE, (EXPlayerTipState) Global.getState(EXPlayerDrawTipState.class));
		tipMap.put(PlayerRuleManager.OTHER_DISCARD_RULE, (EXPlayerTipState) Global.getState(EXPlayerDisCardTipState.class));
		tipMap.put(PlayerRuleManager.KONG_HU_RULE, (EXPlayerKongWinState) Global.getState(EXPlayerKongWinState.class));
		tipMap.put(PlayerRuleManager.CHOW_PONG_DISCARD_RULE, (EXPlayerDrawTipState) Global.getState(EXPlayerDrawTipState.class));

		List<IRuleBase> drawRuleList = new ArrayList<IRuleBase>();
		drawRuleList.add(new RuleSelfKong());
		//drawRuleList.add(new RulePongKong());
		drawRuleList.add(new RuleSelfWin());
		ruleMap.put(PlayerRuleManager.DRAW_RULE, drawRuleList);

		List<IRuleBase> kongDrawRuleList = new ArrayList<IRuleBase>();
		kongDrawRuleList.add(new RuleSelfKong());
		//kongDrawRuleList.add(new RulePongKong());
		kongDrawRuleList.add(new RuleOtherKongSelfWin());
		ruleMap.put(PlayerRuleManager.KONG_DRAW_RULE, kongDrawRuleList);

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

		List<IRuleBase> konghuList = new ArrayList<IRuleBase>();
		ruleMap.put(PlayerRuleManager.KONG_HU_RULE, konghuList);

		List<IRuleBase> cpDiscardRuleList = new ArrayList<IRuleBase>();
		cpDiscardRuleList.add(new RuleSelfKong());
		//cpDiscardRuleList.add(new RulePongKong());
		ruleMap.put(PlayerRuleManager.CHOW_PONG_DISCARD_RULE, cpDiscardRuleList);

	}

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

	public boolean condition(int type, EXPlayer player, boolean tonextState) {
		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;
	}

}
