package extend.pk;

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


import com.game.Global;
import com.game.data.Player;
import com.game.data.Room;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;

import extend.pk.uitl.CardConfig;
import extend.pk.uitl.CardUtil;

public class EXRoom extends Room {
	public RoomCard card;

	/**上次打牌的座位*/
	public int lastDiscardSeat; 

	/**打出牌的集合 */
	public CardGroup discard;

	public EXPlayer win;
	
	public int firstCard = 0;
	
	public Map<Integer,CardConfig> card_config_map;
	
	public int piaoCount = 0;

	public List<List<CardObj>> supCards;
	public List<CardObj> targetCards;
	public int adminSeat = 0;

	public int chooseover = 0;

	public EXRoom(String roomid, Map<String, String> redis_room_map) {
		super(roomid, redis_room_map);
		
		card = new RoomCard(this,this.config.getInt(Config.ROOM_CONFIG_CARDNUM) == 1 ? 15 : 16,maxPlayers);
		card_config_map = new HashMap<Integer,CardConfig>();
		card_config_map.put(CardConfig.DAN.type, CardConfig.DAN);
		card_config_map.put(CardConfig.DUIZI.type, CardConfig.DUIZI);
		card_config_map.put(CardConfig.LIANDUI.type, CardConfig.LIANDUI);
		card_config_map.put(CardConfig.SHUNZI.type, CardConfig.SHUNZI);
		card_config_map.put(CardConfig.ZHA.type, CardConfig.ZHA);
		card_config_map.put(CardConfig.FEIJI.type, CardConfig.FEIJI);
		card_config_map.put(CardConfig.SAN_2.type, CardConfig.SAN_2);

		boolean sidai_2 = this.config.getBoolean(Config.ROOM_CONFIG_SIDAI_2);
		boolean sidai_3 = this.config.getBoolean(Config.ROOM_CONFIG_SIDAI_3);
		if(sidai_2) {
			card_config_map.put(CardConfig.SI_2.type, CardConfig.SI_2);
		}
		if(sidai_3) {
			card_config_map.put(CardConfig.SI_3.type, CardConfig.SI_3);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_XIPAI_SCORE))
		{
			this.config.putInt(Config.ROOM_CONFIG_XIPAI_SCORE, 0);
			this.xi_pai_score = 1;
		}
		else {
			this.xi_pai_score = this.config.getInt(Config.ROOM_CONFIG_XIPAI_SCORE);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_ANCHOU_SCORE))
		{
			this.config.putFloat(Config.ROOM_CONFIG_ANCHOU_SCORE, 0);
			this.an_chou_score = 1;
		}else {
			this.an_chou_score = this.config.getInt(Config.ROOM_CONFIG_ANCHOU_SCORE);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_XIPAI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_XIPAI, false);
		}
		
		this.isEntrust = true;

		this.supCards = new ArrayList<>();
		this.targetCards = new ArrayList<>();
		this.adminSeat = 0;
		this.chooseover = 0;
	}

	public void clear() {
		super.clear();
		
	}
	
	public void clearEx() {
		this.activeSeat = 0;
		this.discard = null;
		this.lastDiscardSeat = 0;
		this.win = null;
		this.firstCard = 0;
	}

	public ITObject getReloadInfo(Player player) {
		ITObject data = super.getReloadInfo(player);
		EXPlayer p = (EXPlayer) player;
		
		data.putTArray("hand_card", CardUtil.toTArray(p.cardInhand));

		data.putTArray("handCards", CardUtil.toTArray(p.cardInhand));
		data.putTArray("outCards", CardUtil.toTArray(p.outCards));

		data.putInt("open", p.open);
		
		data.putInt("last_outcard_seat", this.lastDiscardSeat);
		
		// 游戏结束
		if(this.isplaying == false) {
			
			if(this.win != null) {
				data.putInt("winseat", this.win.seat);
			}
			else
			{
				data.putInt("winseat", 0);
			}
			if(this.card.getCount() > 0) {
				data.putTArray("remaincards", CardUtil.toTArray(this.card.cardList));
			}
		}
		
		return data;
	}

	/**
	 *
	 * */
	private int reMultipleScore(int score) {
		if (this.hpData != null) {
			if (this.hpData.containsKey("times")) {
				return Math.round(score / this.hpData.getInt("times"));
			}
		}
		return score;
	}

	public void addScore(Player destPlayer, Player fromPlayer, int score, int type) {

		////
		if(multipleScore(score) > fromPlayer.hp.cur_hp) {
			score = reMultipleScore((int)fromPlayer.hp.cur_hp);
		//	fromPlayer.hp.cur_hp -= multipleScore(score); //不能在这里扣
		}
		//

		destPlayer.score.round_log.put(type, destPlayer.score.round_log.get(type) + score);
		fromPlayer.score.round_log.put(type, fromPlayer.score.round_log.get(type) - score);
		destPlayer.score.round_score += score;
		destPlayer.score.total_score += score;
		fromPlayer.score.round_score -= score;
		fromPlayer.score.total_score -= score;
	}
	
	/**
	 * 变更炸弹积分
	 * @param destPlayer
	 */
	public void addAllBombScore(EXPlayer destPlayer) {
		
		destPlayer.settleLog.add(Config.SETTLE_BOMB_COUNT);
		destPlayer.boomnum++;
		
		TArray seatData=TArray.newInstance();
		for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
			EXPlayer fromPlayer = (EXPlayer)entry.getValue();
			ITObject seatListData = TObject.newInstance();
			if(fromPlayer != destPlayer) {
				this.addScore(destPlayer, fromPlayer, 10, EXScore.ZHA);
				seatListData.putInt("bom_score", -10);
				seatListData.putInt("aid", fromPlayer.playerid);
				seatData.addTObject(seatListData);
			}else{
				seatListData.putInt("bom_score", 20);
				seatListData.putInt("aid", destPlayer.playerid);
				seatData.addTObject(seatListData);
			}
		}
		TObject allData=TObject.newInstance();
		allData.putTArray("gold_list", seatData);
		this.broadCastToClient(0, Config.GAME_EVT_UPDATE_BOM_SCORE, allData);
	}
	
	/**
	 * 变更游戏积分
	 * @param destPlayer
	 */
	public void addAllScore(Player destPlayer) {
		
		EXPlayer dp = (EXPlayer)destPlayer;
		
		boolean bSpring = false;
		for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
			
			EXPlayer fromPlayer = (EXPlayer)entry.getValue();
			if(fromPlayer != destPlayer) {
				
				if(fromPlayer.cardInhand.size()>1) {
					
					int size =fromPlayer.cardInhand.size();
					int score = size;
					
					if(size == (this.config.getInt(Config.ROOM_CONFIG_CARDNUM) == 1 ? 15 : 16)) {
						fromPlayer.chuntian = true;
						score = size * 2;
						bSpring = true;
					}
					
					if(fromPlayer.isHaveRedTen || dp.isHaveRedTen) {
						score = score * 2;
					}
					
					if(dp.piao > 0) {
						score += dp.piao;
					}
					
					if(fromPlayer.piao > 0) {
						score += fromPlayer.piao;
					}
					
					this.addScore(destPlayer, fromPlayer, score, EXScore.WIN);
				}
			}
		}
		
		// 设置玩家的当初最大分数
		for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
			EXPlayer player = (EXPlayer)entry.getValue();
			
			if(destPlayer == player) {
				player.settleLog.add(Config.SETTLE_WIN_COUNT);
			}
			
			if(player.score.round_score > player.settleLog.get(Config.SETTLE_MAX_SCORE)) {
				player.settleLog.put(Config.SETTLE_MAX_SCORE, player.score.round_score);
			}
		}
		// 增加玩家春天的个数
		if(bSpring) {
			dp.settleLog.add(Config.SETTLE_CHUNTIAN_COUNT);
		}
		
		if(totalType()) {
			
			if( config.containsKey(Config.ROOM_CONFIG_DANIAO) && config.getInt(Config.ROOM_CONFIG_DANIAO) > 0){
				
				int daniao = this.config.getInt(Config.ROOM_CONFIG_DANIAO);
				if(daniao == 1) 			daniao = 10;
				else if(daniao == 2) 	daniao = 20;
				else if(daniao == 3) 	daniao = 50;
				
				int maxTotalScore = 0;
				int maxTotalCount = 0;
				
				for (Entry<Integer, Player> entry : this.playerMapBySeat.entrySet()) {
					
					Player player = entry.getValue();
					
					if(player.score.total_score > maxTotalScore) {
						
						maxTotalScore = player.score.total_score;
						maxTotalCount = 1;
					}
					else if(player.score.total_score == maxTotalScore && maxTotalScore > 0) {
						
						maxTotalCount++;
					}
				}
				
				// 如果存在大赢家
				if(maxTotalScore > 0 && maxTotalCount > 0) {
					
					for (Entry<Integer, Player> entry : this.playerMapBySeat.entrySet()) {
						
						EXPlayer player = (EXPlayer)entry.getValue();
						
						if(player.score.total_score == maxTotalScore) {
			
							player.score.total_score += daniao;
							player.daniaoScore = daniao;
						}
						else {
							
							player.score.total_score -= daniao * maxTotalCount;
							player.daniaoScore = -daniao * maxTotalCount;
						}
					}
				}
			}
		}
	}
	
	@Override
	protected void roomResult() {
		EXMainServer.gameCtr.roomResult(this);
	}
	

	@Override
	public void saveMilitaryTotal(boolean dissmiss) {
		super.saveMilitaryTotal(dissmiss);
		EXMainServer.gameCtr.roomTotalResult(this, dissmiss);
	}
	
	@Override
	public void endGame() {
		addAllScore(win);

		this.chooseover = 0;

		EXPlayBack pb = (EXPlayBack)this.playBackData;
		
		
		ITObject pbResult = TObject.newInstance();
		
		ITArray info = new TArray();
		for (Entry<Integer, Player> entry : playerMapByPlaying.entrySet()) {
			EXPlayer player=(EXPlayer)entry.getValue();
			ITObject obj = TObject.newInstance();
			obj.putInt("seat", player.seat);
			obj.putInt("score", player.score.round_score);
			obj.putBoolean("entrust", player.entrust);
			info.addTObject(obj);
		}

		pbResult.putTArray("result", info);
		Global.logger.error("pbResult:"+pbResult);
		pb.addResultCommand(win.seat, pbResult);
		
		pb.addNewRoundCommand();
		super.endGame();
	}
}
