package robot.mj;

import java.util.Map;

import com.robot.data.Room;

public class EXRoom extends Room {

	public Map<Integer, Object> tipMap; // TipManager
	public List<Object> actionList; // Action
	public List<Object> actionWinList; // Action
	public Object card; // RoomCard

	public Map<Integer, ITArray> playerOutcardMap;
	public Map<Integer, ITArray> playerOpcardMap;
	public Map<Integer, ITArray> playerpengMap;
	public Map<Integer, ITArray> playermingMap;
	public Map<Integer, ITArray> playerziMap;

	public int activeCard;
	public int currenDiscardSeat;
	public int lastDiscardSeat;
	public int winCount = 0;
	public boolean liuju = false;
	public int tongpao = 0;

	public boolean activeHaidi = false;
	public int haidiCard;

	public Object activeKong = null; // OpenKong

	public List<Object> niao; // CardNiao
	public int adminSeat = 0;

	public int qsWinCount = 0;

	public int haidiCount = 0;
	public int piaoNiaoCount = 0;
	public int tianTingCount = 0;
	private boolean closeWinCallBack = false;
	public int fengdingScore = 0;
	public int difen_score = 0;
	public int niao_score = 0;

	public int xi_pai_score = 1;
	public float an_chou_score = 1;

	public EXRoom(String roomid, Map<String, String> redis_room_map) {
		super(roomid, redis_room_map);

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

		if (!this.config.containsKey(Config.ROOM_CONFIG_FENGDING_SCORE)) {
			this.config.putInt(Config.ROOM_CONFIG_FENGDING_SCORE, 0);
		} else {
			int fengding_score = this.config.getInt(Config.ROOM_CONFIG_FENGDING_SCORE);
			if (fengding_score == 0) {
				this.fengdingScore = 0;
			} else if (fengding_score == 1) {
				this.fengdingScore = 35;
			} else if (fengding_score == 2) {
				this.fengdingScore = 40;
			} else if (fengding_score == 3) {
				this.fengdingScore = 45;
			}
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_PIAO_NIAO)) {
			this.config.putInt(Config.ROOM_CONFIG_PIAO_NIAO, 0);
		}

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

		if (!this.config.containsKey(Config.ROOM_CONFIG_PIAO_NIAO_OPT)) {
			this.config.putInt(Config.ROOM_CONFIG_PIAO_NIAO_OPT, 0);
		}

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

		if (!this.config.containsKey(Config.ROOM_CONFIG_DIFEN_SCORE)) {
			this.config.putInt(Config.ROOM_CONFIG_DIFEN_SCORE, 0);
			this.difen_score = 0;
		} else {
			this.difen_score = this.config.getInt(Config.ROOM_CONFIG_DIFEN_SCORE);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_NIAOFEN_SCORE)) {
			this.config.putInt(Config.ROOM_CONFIG_NIAOFEN_SCORE, 0);
			this.niao_score = 0;
		} else {
			this.niao_score = this.config.getInt(Config.ROOM_CONFIG_NIAOFEN_SCORE);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_NIAOFEN_OPT)) {
			this.config.putInt(Config.ROOM_CONFIG_NIAOFEN_OPT, 0);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_KAI_GONG)) {
			this.config.putInt(Config.ROOM_CONFIG_KAI_GONG, 0);
		}

		// 初始化数据结构
		this.tipMap = new HashMap<>();
		this.actionList = new ArrayList<>();
		this.actionWinList = new ArrayList<>();
		this.niao = new ArrayList<>();
		this.isEntrust = true;
		this.adminSeat = 0;
	}

	public void addAction(Object action) {
		addAction(action, true);
	}

	public void addAction(Object action, boolean check) {
		if (this.actionList.size() == 0) {
			this.actionList.add(action);
		} else {
			// 简化处理：直接添加
			this.actionList.add(action);
		}

		if (check)
			checkAction();
	}

	public void checkAction() {
		boolean isWeightest = this.isWeightestAction();
		if (!isWeightest) {
			return;
		}
		if (this.isAllPass()) {
			this.actionWinList.clear();
			EXPlayer activePlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);
			// 简化处理：直接切换状态
		} else {
			// 处理actionList中的动作
			this.actionList.clear();
		}
	}

	public boolean isAllPass() {
		return this.actionList.size() == 0 && this.tipMap.size() == 0;
	}

	public boolean isWeightestAction() {
		if (this.isAllPass()) {
			return true;
		}
		if (this.actionList.size() == 0) {
			return false;
		}

		// 简化处理：返回true
		return true;
	}

	public ITObject getReloadInfo(Player player) {
		ITObject data = super.getReloadInfo(player);
		EXPlayer p = (EXPlayer) player;
		data.putTArray("hand_card", com.robot.Util.toTArray(p.cardInhand));
		data.putInt("left_card", 0); // 使用默认值代替card.getCount()
		data.putInt("curren_outcard_seat", currenDiscardSeat);
		data.putInt("last_outcard_seat", lastDiscardSeat);
		if (this.activeKong != null) {
			// data.putTObject("okinfo", this.activeKong.getTObject());
		}
		return data;
	}

	public boolean checkZhuangxian(EXPlayer destPlayer, EXPlayer fromPlayer, int type) {
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		if (zhuangxian) {
			if (destPlayer.seat == this.bankerSeat || fromPlayer.seat == this.bankerSeat) {
				return true;
			}
		}
		if (!zhuangxian)
			return true;
		return false;
	}

	public void addScore(EXPlayer destPlayer, EXPlayer fromPlayer, int score, int type) {
		// 简化处理：仅更新基本分数
		if (type == 0) { // WIN
			// 处理胡牌得分
		}

		destPlayer.score.round_score += score;
		destPlayer.score.total_score += score;

		fromPlayer.score.round_score -= score;
		fromPlayer.score.total_score -= score;
	}

	public void addQSScore(EXPlayer destPlayer, int score, int id, Object qs_log) {
		// 实现七星得分添加逻辑
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			this.addScore(destPlayer, fromPlayer, score, 0); // QS
		}
	}

	public void addAllScore(EXPlayer destPlayer, int socre, int type) {
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			this.addScore(destPlayer, fromPlayer, socre, type);
		}
	}

	public void addNiaoScore(EXPlayer destPlayer, EXPlayer fromPlayer, int score) {
		// 实现鸟牌得分添加逻辑
		this.addScore(destPlayer, fromPlayer, score, 1); // NIAO
	}

	public void addAllNiaoScore(EXPlayer destPlayer, int score) {
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			addNiaoScore(destPlayer, fromPlayer, score);
		}
	}

	public void addPiaoNiaoScore(EXPlayer destPlayer, EXPlayer fromPlayer) {
		int piao_niao = this.config.getInt(Config.ROOM_CONFIG_PIAO_NIAO);
		if (piao_niao == 0)
			return;
		this.addScore(destPlayer, fromPlayer, destPlayer.piaoNiao + fromPlayer.piaoNiao, 2); // PIAO_NIAO
	}

	public void addAllPiaoNiaoScore(EXPlayer destPlayer) {
		int piao_niao = this.config.getInt(Config.ROOM_CONFIG_PIAO_NIAO);
		if (piao_niao == 0)
			return;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			addPiaoNiaoScore(destPlayer, fromPlayer);
		}
	}

	public void niao_tongPao() {
		// 实现通炮鸟牌逻辑
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		EXPlayer dianPaoPlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);
		EXPlayer banker = (EXPlayer) this.playerMapBySeat.get(this.bankerSeat);
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		// __zhuaNiao_Win(zhuangxian ? banker : dianPaoPlayer);
		if (niao.size() == 0)
			return;
		List<EXPlayer> winPlayerList = new ArrayList<EXPlayer>();
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.winer == 1) {
				winPlayerList.add((EXPlayer) player);
			} else {
				player.win_count = 0;
			}
		}
		// 处理鸟牌逻辑
	}

	public void niao_dianPao(EXPlayer winer) {
		// 实现点炮鸟牌逻辑
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		EXPlayer dianPaoPlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);
		EXPlayer banker = (EXPlayer) this.playerMapBySeat.get(this.bankerSeat);
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		// __zhuaNiao_Win(zhuangxian ? banker : winer);
		if (niao.size() == 0)
			return;

		// 处理点炮鸟牌逻辑
	}

	public void niao_selfWin(EXPlayer owner) {
		// 实现自摸鸟牌逻辑
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		EXPlayer banker = (EXPlayer) this.playerMapBySeat.get(this.bankerSeat);
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		// __zhuaNiao_Win(zhuangxian ? banker : owner);
		if (niao.size() == 0)
			return;

		// 处理自摸鸟牌逻辑
	}

	public void niao_qsWin(EXPlayer owner, int id, Object qs_log) {
		// 实现七星鸟牌逻辑
		EXPlayer banker = (EXPlayer) this.playerMapBySeat.get(this.bankerSeat);
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		// qs_log.niaoList.clear();
		// for (int k = 0; k < 2; ++k) {
		// 	int card = CardUtil.randomDiceNum();
		// 	this.__zhuaNiao(zhuangxian ? banker : owner, qs_log.niaoList, card);
		// }
	}

	public void banban_qsWin(EXPlayer owner, int id, Object qs_log) {
		// 实现板板七星逻辑
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(owner.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			// __addQsWinNiao(owner, fromPlayer, id, qs_log);
		}
	}

	private void winAction(EXPlayer owner, boolean tongpao) {
		for (Entry<Integer, Player> entry : playerMapById.entrySet()) {
			EXPlayer p = (EXPlayer) entry.getValue();
			if (p.winer != 1) {
				p.win_count = 0;
			}
		}
		if (tongpao) {
			niao_tongPao();
			this.bankerSeat = this.activeSeat;
		} else {
			niao_dianPao(owner);
			this.bankerSeat = owner.seat;
		}
		this.endGame();
	}

	public void winCallback(EXPlayer owner) {
		if (closeWinCallBack)
			return;
		if (this.tongpao > 1) {
			this.closeWinCallBack = true;
			// List<TipManager> tlist = new ArrayList<TipManager>(this.tipMap.values());
			// for (TipManager tipmgr : tlist) {
			// 	tipmgr.winAction();
			// }
			for (Object action : this.actionWinList) {
				// action.run();
			}
			this.actionWinList.clear();
			winAction(owner, true);
			this.closeWinCallBack = false;
		} else {
			winAction(owner, false);
		}
	}

	@Override
	protected void roomResult() {
		// 这里需要调用实际的roomResult方法
	}

	@Override
	public void endGame() {
		boolean fengding = this.config.getBoolean(Config.ROOM_CONFIG_FENGDING);
		boolean four_win = this.config.getBoolean(Config.ROOM_CONFIG_FOUR_WIN);
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.winer == 1) {
				// List<HUScore> list = ((EXScore) player.score).hu_score;
				// 处理胡牌得分
			}
		}
		super.endGame();
	}

	@Override
	public void saveMilitaryTotal(boolean dissmiss) {
		super.saveMilitaryTotal(dissmiss);
		// 这里需要调用实际的roomTotalResult方法
	}

	@Override
	public void clear() {
		super.clear();
		this.liuju = false;
		this.haidiCard = 0;
		this.closeWinCallBack = false;
		this.tongpao = 0;
		this.activeSeat = this.lastDiscardSeat = this.currenDiscardSeat = 0;
		this.qsWinCount = this.haidiCount = this.winCount = this.piaoNiaoCount = 0;
		this.activeKong = null;
		this.activeHaidi = false;
		this.niao.clear();
		this.actionWinList.clear();
	}

}