package extend.mj.player.rule;

import com.game.Global;
import com.game.Util;
import com.game.data.Player;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import extend.mj.*;
import extend.mj.player.rulestate.PRSelfWinState;
import extend.mj.player.state.EXPlayerPiaoNiaoTipState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;
import extend.mj.uitl.WinCard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

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

	@Override
	public boolean condition(EXPlayer player) {
		// TODO Auto-generated method stub
		ITObject config = player.room.config;
		EXRoom room = player.getRoom();
		int laizi = 0;
		HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();

		boolean flag = false;

		WinCard win = new WinCard(player.cardInhand, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false);
		if (win.checkWin(map, player, room, true)) {
			flag = true;
		}

		if (flag) {
			ITArray opcard = TArray.newInstance();
			opcard.addInt(player.drawCard);
			Tip tip = new Tip(player.drawCard,opcard, RuleWeight.SELF_WIN,this, RuleWeight.TYPE_WIN);
			tip.winMap = map;
			tip.self = true;
			player.tipMgr.addTip(tip);

			ArrayList<Integer> tempCardList = new ArrayList<Integer>();
			tempCardList.addAll(player.cardInhand);
			WinCard win2 = new WinCard(tempCardList, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false);
			int cheng_num = 0;
			for (Map.Entry<Integer, Player> entry : room.playerMapById.entrySet()) {
				EXPlayer tempPlayer = (EXPlayer)entry.getValue();
				cheng_num += tempPlayer.cheng_num;
			}

			//WinCard.putWinCardType(map, WinCardType.YING_MO, 1,WinCardType.YING_MO_SCORE, true, "自摸");
			if (player.seat==room.bankerSeat){
				WinCard.putWinCardType(map, WinCardType.ZHUANGJIA, 1,WinCardType.BASE_HU_SCORE, false, "庄家");
			}else{
				WinCard.putWinCardType(map, WinCardType.XIANJIA, 1,WinCardType.BASE_HU_SCORE, false, "闲家");
			}
			return true;
		}
		player.is_cheng = false;
		return false;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		// TODO Auto-generated method stub
		player.winCard = tip.card;
		player.drawCard = tip.card;
		player.winMap = tip.winMap;
		EXRoom room = player.getRoom();

		Global.logger.info("self win player:" + player.playerid + " di fen:" + player.di_score + " hu fen:" + player.hu_score);

		int dahu_score = 1;
		int jia_score = 0;
		for (Map.Entry<Integer, WinCardType> entry : player.winMap.entrySet()) {
			WinCardType cardType = entry.getValue();
			Global.logger.info("player:" + player.playerid + " hu:" + cardType.toString());
			if (cardType.IsDaHu())
			{
				dahu_score *= cardType.score;
			}
			else
			{
				jia_score +=  cardType.score;
			}
		}

		if (dahu_score > 0)
		{
			player.hu_score *= dahu_score;
		}
		Global.logger.info("self win player:" + player.playerid +"jia_score:"+jia_score+"dahu_score:"+dahu_score );
		player.hu_score += jia_score;
		Global.logger.info("self win player:" + player.playerid +"hu_score:"+player.hu_score);
		player.stateMachine.changeState(Global.getState(PRSelfWinState.class));
	}

}
