package com.backfire.room.manager;

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

import android.view.MotionEvent;

import com.backfire.graphics.PCanvas;
import com.backfire.room.data.HandlerMsg;
import com.backfire.room.data.RoomInfo;
import com.backfire.room.data.State.GameState;
import com.backfire.widget.RoomButton;

/**
 * @Title: RoomFixationBtnManager.java
 * @Description:管理房间内的固定按钮
 * @author Potter
 * @date 2013年10月30日 下午5:58:03
 * @version V2.0
 */
public class RoomFixationBtnManager {
	public Map<Integer, RoomButton> btnMap = null;

	public RoomFixationBtnManager(Map<Integer, RoomButton> btnMap) {
		this.btnMap = btnMap;
	}

	public void draw(PCanvas canvas) {
		for (Entry<Integer, RoomButton> entry : btnMap.entrySet()) {
			RoomButton button = entry.getValue();
			if (button != null) {
				synchronized (button) {
					button.initDraw();
				}
			}
			button.draw(canvas);
		}
	}

	public void updateState() {
		int gameState = RoomInfo.getInstance().getState();
		boolean selfIsDeposit = RoomInfo.getInstance().getSelfIsDeposit();
		ArrayList<Integer> keys = new ArrayList<Integer>();
		switch (gameState) {
		case GameState.GAME_QIANG_ZHUANG:
			// keys.add(HandlerConstant.MSG_CHAT);
			break;
		case GameState.GAME_PLAY:
			keys.add(HandlerMsg.MSG_DISPOSIT);
			// keys.add(HandlerConstant.MSG_CHAT);
			break;
		}
		keys.add(HandlerMsg.MSG_CHAT);
		keys.add(HandlerMsg.MSG_SET);
		keys.add(HandlerMsg.MSG_ADD_VCOIN);

		for (Entry<Integer, RoomButton> entry : btnMap.entrySet()) {
			int key = entry.getKey();
			RoomButton button = entry.getValue();

			if (button != null) {
				synchronized (button) {
					if (keys.contains(key)) {
						if (HandlerMsg.MSG_DISPOSIT == key && selfIsDeposit) {
							button.setEnable(false);
						} else {
							button.setEnable(true);
						}
					} else {
						button.setEnable(false);
					}
				}
			}
		}
	}

	public boolean touch(MotionEvent event) {
		boolean result = false;
		for (Entry<Integer, RoomButton> entry : btnMap.entrySet()) {
			RoomButton button = entry.getValue();
			if (button != null) {
				synchronized (button) {
					result = button.dispatch(event);
				}
				if (result)
					break;
			}
		}
		return result;
	}

	public void updateBtnState(int msg, boolean isVisible, boolean enable) {
		RoomButton button = btnMap.get(msg);
		if (null != button) {
			synchronized (button) {
				button.setVisible(isVisible);
				button.setEnable(enable);
			}
		}
	}

	public void clear() {
		if (btnMap != null) {
			synchronized (btnMap) {
				for (Entry<Integer, RoomButton> entry : btnMap.entrySet()) {
					RoomButton button = entry.getValue();
					if (button != null) {
						synchronized (button) {
							button.clear();
						}
						button = null;
					}
				}
			}
			btnMap.clear();
			btnMap = null;
		}
	}

	public void reset() {
		// TODO Auto-generated method stub

	}
}
