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 int daNiaoCount = 0;

	public boolean isDaniao = true;

	public int daNiaoScore = 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_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/this.hpData.getInt("times");
			}
		}
		return score;
	}



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

		////
		//System.out.println("fromPlayer.hp.cur_hp"+fromPlayer.hp.cur_hp);
		if (multipleScore(score) > fromPlayer.hp.cur_hp) {
			score = reMultipleScore((int) fromPlayer.hp.cur_hp);
			// fromPlayer.hp.cur_hp -= multipleScore(score); //不能在这里扣
		}
	//	System.out.println("score:"+score);
		//
		EXPlayer destEXPlayer = (EXPlayer) destPlayer;
		EXPlayer fromEXPlayer = (EXPlayer) fromPlayer;

		if (type == EXScore.WIN || type == EXScore.ZHA) {
			destEXPlayer.winCardScore += score;
			fromEXPlayer.winCardScore -= 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);
		//if (type!=EXScore.DANIAO){
			destPlayer.score.round_score += score;
			fromPlayer.score.round_score -= score;
	//	}
		destPlayer.score.total_score += score;
		fromPlayer.score.total_score -= score;

//		// 重新设置房间最低分数
//		fromPlayer.room.upper_limit_hp -= score;
//		destPlayer.room.upper_limit_hp += 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;
		//System.out.println("addallscore");
		boolean bSpring = false;
		int score_total = 0;

		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);
					score_total = score * 100;
				}
			}

		}

//		for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
//			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
//			int cur_hp = (int) fromPlayer.hp.cur_hp;
//
//			if (fromPlayer != destPlayer) {
//				cur_hp = cur_hp - score_total;
//			} else {
//				cur_hp = cur_hp + score_total;
//			}
//			// 重置最高输赢分数
//			if (fromPlayer.room.upper_limit_hp == 0 || fromPlayer.room.upper_limit_hp > cur_hp) {
//				fromPlayer.room.upper_limit_hp = cur_hp;
//			}
//			Global.logger.info(fromPlayer.playerid + "当局赢分上限：" + fromPlayer.room.upper_limit_hp);
//
//		}

		// 设置玩家的当初最大分数
		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);
		}

		Global.logger.info("当局赢分上限：" + destPlayer.room.upper_limit_hp);

		if (false) {

			if (config.containsKey(Config.GAME_DANIAO) && config.getInt(Config.GAME_DANIAO) > 0) {

				EXPlayer winNiao = null;

				for (Entry<Integer, Player> entry : dp.getRoom().playerMapById.entrySet()) {
					EXPlayer player = (EXPlayer) entry.getValue();
					if (player.score.total_score > 0) {
						winNiao = player;
					}
				}
				if (winNiao != null) {
					for (Entry<Integer, Player> entry1 : dp.getRoom().playerMapBySeat.entrySet()) {

						EXPlayer lostPlayer = (EXPlayer) entry1.getValue();
						if (winNiao.playerid == lostPlayer.playerid) {
							continue;
						} else {
							Global.logger.info(lostPlayer.playerid + ":lostScore-player.daniaoScore:"
									+ winNiao.getRoom().daNiaoScore);
							dp.getRoom().addScore(winNiao, lostPlayer, winNiao.getRoom().daNiaoScore, EXScore.DANIAO);
							Global.logger.info(lostPlayer.playerid + ":" + lostPlayer.score.total_score);

						}
					}
					Global.logger
							.info(winNiao.playerid + ":addScore-player.daniaoScore:" + winNiao.getRoom().daNiaoScore);

				}

			}
		}
	}

	@Override
	protected void roomResult() {
		//小结算
		EXMainServer.gameCtr.roomResult(this);
	}

	@Override
	public void saveMilitaryTotal(boolean dissmiss) {
		//强行退出
		for (Entry<Integer, Player> entry1 : this.playerMapBySeat.entrySet()) {
			EXPlayer player = (EXPlayer) entry1.getValue();
			//System.out.println("player:"+player.playerid+"|"+player.hp.total_hp);
			//充值打鸟分
			player.score.round_score = 0;

		}

		tuiChuScore();
		boolean settlement = this.redisUpdateHp();
		//for (Entry<Integer, Player> entry1 : this.playerMapBySeat.entrySet()) {
		//	EXPlayer player = (EXPlayer) entry1.getValue();
			//System.out.println("2player:"+player.playerid+"|"+player.hp.total_hp);
	//	}
		super.saveMilitaryTotal(dissmiss);
		//for (Entry<Integer, Player> entry1 : this.playerMapBySeat.entrySet()) {
			//EXPlayer player = (EXPlayer) entry1.getValue();
			//System.out.println("3player:"+player.playerid+"|"+player.hp.total_hp);
		//}
		EXMainServer.gameCtr.roomTotalResult(this, dissmiss);
	}

	public void tuiChuScore(){
		//获取获胜
		if (config.containsKey(Config.GAME_DANIAO) && config.getInt(Config.GAME_DANIAO) > 0) {

			EXPlayer winNiao = null;

			for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
				EXPlayer player = (EXPlayer) entry.getValue();
				if (player.score.total_score > 0) {
					winNiao = player;
				}
			}
			if (winNiao != null) {
				for (Entry<Integer, Player> entry1 : this.playerMapBySeat.entrySet()) {

					EXPlayer lostPlayer = (EXPlayer) entry1.getValue();
					if (winNiao.playerid == lostPlayer.playerid) {
						continue;
					} else {
						Global.logger.info(lostPlayer.playerid + ":lostScore-player.daniaoScore:" + winNiao.getRoom().daNiaoScore);
						this.addScore(winNiao, lostPlayer, this.daNiaoScore, EXScore.DANIAO);
						Global.logger.info(lostPlayer.playerid + ":" + lostPlayer.score.total_score);
					}
				}
				Global.logger
						.info(winNiao.playerid + ":addScore-player.daniaoScore:" + winNiao.getRoom().daNiaoScore);

			}

		}
	}

	@Override
	public void endGame() {
		//System.out.println("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.putInt("daniao", 0);
			obj.putInt("winCardScore", player.winCardScore);
			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();
	}
}
