package extend.zp;

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

import com.game.Global;

import extend.zp.player.rule.RuleChow;
import extend.zp.player.rule.RulePong;
import extend.zp.player.rule.RuleWin;
import extend.zp.player.state.EXPlayerDisCardTipState;
import extend.zp.player.state.EXPlayerTipState;
import extend.zp.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						OTHER_DRAW_RULE 			= 4;
	public static final int						OTHER_DISCARD21_RULE 	= 5;
	
	public static final int						WIN_RULE	= 6;
	

	public static final int						ACTION_OTHER_RULE	= 7;



	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(EXPlayerDisCardTipState.class));
		tipMap.put(PlayerRuleManager.OTHER_DISCARD_RULE, (EXPlayerTipState) Global.getState(EXPlayerDisCardTipState.class));
		tipMap.put(PlayerRuleManager.WIN_RULE, (EXPlayerTipState) Global.getState(EXPlayerDisCardTipState.class));
		
		
		List<IRuleBase> drawRuleList = new ArrayList<IRuleBase>();
		drawRuleList.add(new RuleWin());
		drawRuleList.add(new RulePong());
		drawRuleList.add(new RuleChow());
		ruleMap.put(PlayerRuleManager.DRAW_RULE, drawRuleList);

		List<IRuleBase> otherDiscardList = new ArrayList<IRuleBase>();
		otherDiscardList.add(new RulePong());
		otherDiscardList.add(new RuleChow());
		ruleMap.put(PlayerRuleManager.OTHER_DISCARD_RULE, otherDiscardList);
		
		List<IRuleBase> asList = new ArrayList<IRuleBase>();
		asList.add(new RuleWin());
		ruleMap.put(PlayerRuleManager.WIN_RULE, asList);
	}


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

}
