package extend.zp;

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

import com.game.Global;

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


public class PlayerRuleManager {
	public static final int						DEAL_RULE		= 1;
	public static final int						KAN_RULE		= 2;
	public static final int						TI_RULE			= 3;
	public static final int						DRAW_RULE		= 4;
	public static final int						OTHER_DRAW_RULE	= 5;
	public static final int						DRAW_PAO_RULE	= 6;
	public static final int						DISCARD_RULE	= 7;
	public static final int						HU_RULE			= 9;
	public static final int						OTHER_HU_RULE	= 11;
	public static final int 					WAI_LIU_RULE = 12;
	public static final int 					DEAL_LIU_RULE = 13;
	public static final int 					HU_LIU_RULE = 14;

	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.HU_RULE, (EXPlayerTipState) Global.getState(EXPlayerDrawTipState.class));
		tipMap.put(PlayerRuleManager.WAI_LIU_RULE, (EXPlayerTipState) Global.getState(EXPlayerDrawTipState.class));
		tipMap.put(PlayerRuleManager.DEAL_LIU_RULE, (EXPlayerTipState) Global.getState(EXPlayerDrawTipState.class));
		tipMap.put(PlayerRuleManager.TI_RULE, (EXPlayerTipState) Global.getState(EXPlayerDrawTipState.class));
		tipMap.put(PlayerRuleManager.HU_LIU_RULE, (EXPlayerTipState) Global.getState(EXPlayerDrawTipState.class));

		tipMap.put(PlayerRuleManager.OTHER_DRAW_RULE, (EXPlayerTipState) Global.getState(EXPlayerDisCardTipState.class));
		tipMap.put(PlayerRuleManager.DISCARD_RULE, (EXPlayerTipState) Global.getState(EXPlayerDisCardTipState.class));
		tipMap.put(PlayerRuleManager.OTHER_HU_RULE, (EXPlayerTipState) Global.getState(EXPlayerDisCardTipState.class));

		List<IRuleBase> dealList = new ArrayList<IRuleBase>();
		dealList.add(new RuleDealTi());
		dealList.add(new RuleDealKan());
		ruleMap.put(PlayerRuleManager.DEAL_RULE, dealList);

		List<IRuleBase> dealKanList = new ArrayList<IRuleBase>();
		dealKanList.add(new RuleKan());
		ruleMap.put(PlayerRuleManager.KAN_RULE, dealKanList);

		List<IRuleBase> paoList = new ArrayList<IRuleBase>();
		//paoList.add(new RuleTi());
		paoList.add(new RuleWei());
		ruleMap.put(PlayerRuleManager.TI_RULE, paoList);

		List<IRuleBase> drawRuleList = new ArrayList<IRuleBase>();
		drawRuleList.add(new RuleWin());
		drawRuleList.add(new RuleDrawChi());
		drawRuleList.add(new RuleLiu());
		ruleMap.put(PlayerRuleManager.DRAW_RULE, drawRuleList);

		List<IRuleBase> otherDrawRuleList = new ArrayList<IRuleBase>();
		otherDrawRuleList.add(new RuleWin());
		otherDrawRuleList.add(new RuleDrawPong());
		otherDrawRuleList.add(new RuleDrawChi());
		ruleMap.put(PlayerRuleManager.OTHER_DRAW_RULE, otherDrawRuleList);

		List<IRuleBase> drawPaoList = new ArrayList<IRuleBase>();
		//drawPaoList.add(new RuleDrawPao());
		ruleMap.put(PlayerRuleManager.DRAW_PAO_RULE, drawPaoList);

		List<IRuleBase> otherDiscardList = new ArrayList<IRuleBase>();
		//otherDiscardList.add(new RuleDiscardPao());
		otherDiscardList.add(new RulePong());
		otherDiscardList.add(new RuleChi());
		ruleMap.put(PlayerRuleManager.DISCARD_RULE, otherDiscardList);

		List<IRuleBase> huList = new ArrayList<IRuleBase>();
		huList.add(new RuleWin());
		ruleMap.put(PlayerRuleManager.HU_RULE, huList);

		List<IRuleBase> wailiuList = new ArrayList<IRuleBase>();
		wailiuList.add(new RuleWin());
		wailiuList.add(new RuleWeiLiu());
		ruleMap.put(PlayerRuleManager.WAI_LIU_RULE, wailiuList);

		List<IRuleBase> dealliuList = new ArrayList<IRuleBase>();
		dealliuList.add(new RuleDealLiu());
		ruleMap.put(PlayerRuleManager.DEAL_LIU_RULE, dealliuList);

		List<IRuleBase> huliuList = new ArrayList<IRuleBase>();
		huliuList.add(new RuleWin());
		huliuList.add(new RuleDealLiu());
		ruleMap.put(PlayerRuleManager.HU_LIU_RULE, huliuList);


		List<IRuleBase> otherHuList = new ArrayList<IRuleBase>();
		otherHuList.add(new RuleWin());
		ruleMap.put(PlayerRuleManager.OTHER_HU_RULE, otherHuList);
	}

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

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

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

		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;
	}
}
