package com.game.data;

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

import com.data.util.ErrorCode;
import com.game.Global;
import com.game.data.Timer.ITaskHandler;
import com.game.room.state.RoomDestoryGameState;

/**
 * 房间解散处理
 * 
 */
public class RoomDismiss {
	private Room			owner;
	public boolean			start		= false;
	private boolean			agree		= true;
	/**
	 * 最多申请次数,超过次数，该玩家不能申请解散
	 */
	private final int				MAX_ASK_COUNT	= 3;

	private Timer			timer;
	private Player			askPlayer;
	public int				maxTime		= 90;
	private List<Player>	playerList;

	public RoomDismiss(Room owner) {
		this.owner = owner;
		playerList = new ArrayList<>();
	}

	public void askDismiss(Player player, int gid) {
		if (start) {
			player.response(null, gid, 3);
			return;
		}
		if (player.dismissCount >= MAX_ASK_COUNT) {
			player.response(null, gid, ErrorCode.ROOM_DIS_UPPER_LIMIT);
			return;
		}

		if (player.room.isBanDismiss) {
			player.response(null, gid, ErrorCode.ROOM_DIS_NOT_ALLOWED);
			return;
		}

		askPlayer = player;
		startDismiss(player);

		this.dismissCallback(player);
	}

	public void responseDismiss(Player player, boolean agree) {
		if (start == false || player.dismissState != 0) {
			return;
		}
		if (agree) {
			player.dismissState = 1;
		} else {
			player.dismissState = 2;
		}
		dismissCallback(player);
	}

	private void dismissCallback(Player player) {
		int agreeCount = 0;
		int notAgreeCount = 0;
		for (Player p : playerList) {
			if (p.dismissState == 1) {
				agreeCount += 1;
			} else if (p.dismissState == 2) {
				notAgreeCount += 1;
			}
		}

		agree = notAgreeCount == 0 && agreeCount == playerList.size();

		if (agree) {
			interruptTimer();
		} else {
			if (notAgreeCount > 0) {
				Global.gameCtr.dismisRoomFail(owner);
				interruptTimer();
			} else {
				Global.gameCtr.dismissRoom(askPlayer, null, playerList, getLeftTime());
			}

		}
	}

	private void interruptTimer() {
		if (timer != null)
			timer.stop();
		timer = null;
		checkDestroy();
	}

	private void startDismiss(Player player) {
		playerList.clear();
		for (Entry<Integer, Player> entry : owner.playerMapBySeat.entrySet()) {
			Player p = entry.getValue();
			if (p.already_round > 0) {
				playerList.add(p);
				p.dismissState = 0;
			}
		}
		timer = new Timer(maxTime * 1000, new ITaskHandler() {
			@Override
			public void doTask(Timer timer) {
				disMissTimeOutCallback();
				Global.logger.error("[" + owner.room_key + "] room dismiss time out destroy!");
			}
		});
		owner.addTimer(timer);
		start = true;
		player.dismissState = 1;
		player.dismissCount++;
		Global.logger.info("playerid :" + player.playerid + " ask dissmis room :" + player.room.roomid + ",dismissCount :" + player.dismissCount);
	}

	public void reload(Player owner) {
		if (start) {
			if (playerList.contains(owner)) {
				//jefe 重新加载不用重置数据
				Global.gameCtr.dismissRoom(askPlayer, owner, playerList, getLeftTime());
			}

		}
	}

	public int getLeftTime() {
		return Math.abs(timer.getCountdownTime() / 1000);
	}

	private void checkDestroy() {
		start = false;
		if (agree) {
			owner.saveMilitaryTotal(true);
			owner.stateMachine.changeState(Global.getState(RoomDestoryGameState.class));
		}
		agree = true;
	}

	private void disMissTimeOutCallback() {
		for (Player p : playerList) {
			p.dismissState = 1;
		}
		agree = true;
		checkDestroy();
	}

}
