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.MainServer;
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 int curPaiFen;

	public EXPlayer win;
	
	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 List<CardObj> out510KInhand;

	public EXRoom(String roomid, Map<String, String> redis_room_map) {
		super(roomid, redis_room_map);
		out510KInhand = new ArrayList<>();
		card = new RoomCard(this, 23,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.WUSHIK.type, CardConfig.WUSHIK);

		boolean sandai_2 = this.config.getBoolean(Config.KEYI_DAI_2);
		if(sandai_2) {
			card_config_map.put(CardConfig.SAN_2.type, CardConfig.SAN_2);
			card_config_map.put(CardConfig.FEIJI.type, CardConfig.FEIJI);
		}

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

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

		if (!this.config.containsKey(Config.KE_BAO_PAI))
		{
			this.config.putBoolean(Config.KE_BAO_PAI, false);
		}
		if (!this.config.containsKey(Config.QU_DIAO_34))
		{
			this.config.putBoolean(Config.QU_DIAO_34, false);
		}
		if (!this.config.containsKey(Config.XI_YA_SI_YOU_FEN))
		{
			this.config.putBoolean(Config.XI_YA_SI_YOU_FEN, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_XIPAI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_XIPAI, false);
		}
		if (!this.config.containsKey(Config.WU_ZHANG_SUAN_XI))
		{
			this.config.putBoolean(Config.WU_ZHANG_SUAN_XI, false);
		}
		if (!this.config.containsKey(Config.KE_ZUO_LIAN_ZI_2))
		{
			this.config.putBoolean(Config.KE_ZUO_LIAN_ZI_2, false);
		}
		if (!this.config.containsKey(Config.THREE_WANG_SUAN_XI))
		{
			this.config.putBoolean(Config.THREE_WANG_SUAN_XI, false);
		}
		if (!this.config.containsKey(Config.KEYI_DAI_2))
		{
			this.config.putBoolean(Config.KEYI_DAI_2, false);
		}
		if (!this.config.containsKey(Config.dan_sun_zi))
		{
			this.config.putBoolean(Config.dan_sun_zi, false);
		}
		if (!this.config.containsKey(Config.an_bao))
		{
			this.config.putBoolean(Config.an_bao, false);
		}
		if (!this.config.containsKey(Config.xi_zai_shou_you_fen))
		{
			this.config.putBoolean(Config.xi_zai_shou_you_fen, 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.curPaiFen = 0;
		this.out510KInhand.clear();
	}

	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(EXPlayer destPlayer, EXPlayer 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 addAllXiScore(EXPlayer destPlayer, int score) {
		TArray seatData=TArray.newInstance();
		destPlayer.xi_fen_count++;
		for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
			EXPlayer fromPlayer = (EXPlayer)entry.getValue();
			ITObject seatListData = TObject.newInstance();
			if(fromPlayer != destPlayer) {
				this.addScore(destPlayer, fromPlayer, score, EXScore.XI_FEN);
				destPlayer.xiScore += score;
				fromPlayer.xiScore -= score;
				seatListData.putInt("xi_score", fromPlayer.xiScore);
				seatListData.putInt("aid", fromPlayer.playerid);
				seatData.addTObject(seatListData);
			}
		}
		for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
			EXPlayer fromPlayer = (EXPlayer)entry.getValue();
			ITObject seatListData = TObject.newInstance();
			if(fromPlayer == destPlayer) {
				seatListData.putInt("xi_score", destPlayer.xiScore);
				seatListData.putInt("aid", destPlayer.playerid);
				seatData.addTObject(seatListData);
			}
		}
		TObject allData=TObject.newInstance();
		allData.putTArray("gold_list", seatData);
		this.broadCastToClient(0, Config.GAME_EVT_UPDATE_XI_SCORE, allData);
	}

	public void kanFen(EXPlayer destPlayer) {
		TObject allData=TObject.newInstance();
		allData.putTArray("pai_list", CardUtil.toTArray(this.out510KInhand));
		MainServer.instance.sendEvent(Config.GAME_EVT_KAN_FEN, allData, destPlayer.sender);
	}

	/**
	 * 变更5_10_K积分
	 * @param destPlayer
	 */
	public void add510kScore(EXPlayer destPlayer) {
		TArray seatData=TArray.newInstance();
		destPlayer.zhuaScore += curPaiFen;
		for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
			EXPlayer fromPlayer = (EXPlayer)entry.getValue();
			ITObject seatListData = TObject.newInstance();
			if(fromPlayer != destPlayer) {
				//this.addScore(destPlayer, fromPlayer, curPaiFen, EXScore.ZHUA_FEN);
				seatListData.putInt("zhua_score", fromPlayer.zhuaScore);
				seatListData.putInt("aid", fromPlayer.playerid);
				seatData.addTObject(seatListData);
			}else{
				seatListData.putInt("zhua_score", destPlayer.zhuaScore);
				seatListData.putInt("aid", destPlayer.playerid);
				seatData.addTObject(seatListData);
			}
		}
		TObject allData=TObject.newInstance();
		allData.putTArray("gold_list", seatData);
		this.broadCastToClient(0, Config.GAME_EVT_UPDATE_ZHUA_SCORE, allData);
		this.curPaiFen = 0;
	}
	
	/**
	 * 变更游戏积分
	 * @param destPlayer
	 */
	public void addAllScore(EXPlayer destPlayer) {
		
		EXPlayer dp = (EXPlayer)destPlayer;
		
		for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
			
			EXPlayer fromPlayer = (EXPlayer)entry.getValue();
			if(fromPlayer != destPlayer) {
				if (destPlayer.zhuaScore >= fromPlayer.zhuaScore)
				{
					destPlayer.huaScore += 80;
					fromPlayer.huaScore -= 80;
					addScore(destPlayer, fromPlayer, 80, EXScore.WIN);
				}
				else {
					destPlayer.huaScore += 40;
					fromPlayer.huaScore -= 40;
					addScore(destPlayer, fromPlayer, 40, EXScore.WIN);
				}
				addScore(destPlayer, fromPlayer, destPlayer.zhuaScore-fromPlayer.zhuaScore, EXScore.WIN);
			}

			if (fromPlayer.seat == destPlayer.seat)
			{
				fromPlayer.you_seat = 1;
				fromPlayer.one_you++;
			}
			else {
				if (destPlayer.getRoom().maxPlayers == 2)
				{
					fromPlayer.you_seat = 2;
					fromPlayer.two_you++;
				}
				else {
					if (fromPlayer.seat > destPlayer.seat)
					{
						fromPlayer.you_seat = ((fromPlayer.seat - destPlayer.seat) % destPlayer.getRoom().maxPlayers) + 1;
					}
					else {
						fromPlayer.you_seat = ((fromPlayer.seat + destPlayer.getRoom().maxPlayers - destPlayer.seat)%destPlayer.getRoom().maxPlayers) + 1;
					}
				}
			}

			if (fromPlayer.you_seat == 1)
			{
				fromPlayer.one_you++;
			}
			else if (fromPlayer.you_seat == 2)
			{
				fromPlayer.two_you++;
			}
			else if (fromPlayer.you_seat == 3)
			{
				fromPlayer.three_you++;
			}
			else if (fromPlayer.you_seat == 4)
			{
				fromPlayer.four_you++;
			}
		}
	}
	
	@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.putInt("xi_score", player.xiScore);
			obj.putInt("zhua_score", player.zhuaScore);
			obj.putInt("hua_score", player.huaScore);
			obj.putBoolean("entrust", player.entrust);
			info.addTObject(obj);
		}
		pbResult.putTArray("result", info);
		
		pb.addResultCommand(win.seat, pbResult);
		
		pb.addNewRoundCommand();
		super.endGame();
	}
}
