package com.backfire.room.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.yeeyo.doudizhu.feiyu.R;
import android.view.MotionEvent;

import com.backfire.arithmetic.Card;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.graphics.PCanvas;
import com.backfire.room.Room;
import com.backfire.room.Interface.IClickListener;
import com.backfire.room.Interface.IUpdateBtnState;
import com.backfire.room.Interface.IUpdateGameControlBnt;
import com.backfire.room.data.ConfigCenter;
import com.backfire.room.data.HandlerMsg;
import com.backfire.room.data.RoomInfo;
import com.backfire.room.data.State.GameState;
import com.backfire.widget.RoomButton;

public class BtnManagerCenter implements IUpdateBtnState,IUpdateGameControlBnt{
	private RoomFixationBtnManager fixationBtnManager=null;
	private GameControlBtnManager gameControlBtnManager=null;
	
	private IClickListener iClickListener=null;
	private Room mRoom;
	
	public BtnManagerCenter(Room room,IClickListener iClickListener){
		mRoom = room;
		this.iClickListener=iClickListener;
		//----房间固定按钮-----
		initFixationBtn();
		
		//----玩牌动态按钮----
		initMotionBtn();
	}
	
	private void initFixationBtn(){
		Map<Integer, RoomButton> fixationBtnArray=new HashMap<Integer, RoomButton>();
		int toolBtnW=ConfigCenter.toolItemW;
		int toolBtnH=ConfigCenter.toolItemH;
		int toolBtnY=ConfigCenter.toolItemY;
		boolean enable=false;
		
		RoomButton button=new RoomButton(iClickListener, HandlerMsg.MSG_DISPOSIT, R.drawable.desposit_bt_d,ConfigCenter.toolDepositeItemX, toolBtnY, toolBtnW, toolBtnH);
		button.setEnable(enable);
		fixationBtnArray.put(HandlerMsg.MSG_DISPOSIT, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_TOOLS, R.drawable.daoju_bt_n,ConfigCenter.toolToolsItemX, toolBtnY, toolBtnW, toolBtnH);
		button.setEnable(enable);
		fixationBtnArray.put(HandlerMsg.MSG_TOOLS, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_CHAT, R.drawable.chat_bt_n,ConfigCenter.toolChatItemX , toolBtnY, toolBtnW, toolBtnH);
		button.setEnable(enable);
		fixationBtnArray.put(HandlerMsg.MSG_CHAT, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_SET, R.drawable.set_bt_n,ConfigCenter.toolSetItemX , toolBtnY, toolBtnW, toolBtnH);
		button.setEnable(enable);
		fixationBtnArray.put(HandlerMsg.MSG_SET, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_ADD_VCOIN, R.drawable.add_vcoin_n_bt,ConfigCenter.add_VcoinItemX , ConfigCenter.add_VcoinItemY
				, ConfigCenter.add_VcoinItemW, ConfigCenter.add_VcoinItemH);
		button.setEnable(enable);
		fixationBtnArray.put(HandlerMsg.MSG_ADD_VCOIN, button);
		
		fixationBtnManager=new RoomFixationBtnManager(fixationBtnArray);
	}
	
	private void initMotionBtn(){
		Map<Integer, RoomButton> btnMap=new HashMap<Integer, RoomButton>();
		
		final int w=ConfigCenter.room_bt_W;
		final int h=ConfigCenter.room_bt_H;
		final int y=ConfigCenter.room_bt_Y;
		final boolean isVisible=false;
		
		RoomButton button=new RoomButton(iClickListener, HandlerMsg.MSG_READY, R.drawable.ready_bt,ConfigCenter.read_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_READY, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_LEAVE, R.drawable.likai_bt,ConfigCenter.leave_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_LEAVE, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_CENTER_LEAVE, R.drawable.likai_bt,
				(DdzApplication.getApplication().screenWidth-ConfigCenter.room_bt_W)/2, y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_CENTER_LEAVE, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_ONE, R.drawable.yibei_bt,ConfigCenter.one_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_ONE, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_TWO, R.drawable.liangbei_bt,ConfigCenter.two_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_TWO, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_THREE, R.drawable.sanbei_bt,ConfigCenter.three_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_THREE, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_GRAB, R.drawable.grab_bt,ConfigCenter.grab_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_GRAB, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_NO_GRAB, R.drawable.no_grab_bt,ConfigCenter.nograb_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_NO_GRAB, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_PASS, R.drawable.pass_bt,ConfigCenter.pass_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_PASS, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_RE_SELECT, R.drawable.chongxuan_bt,ConfigCenter.reselect_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_RE_SELECT, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_PROMPT, R.drawable.tishi_bt,ConfigCenter.prompt_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_PROMPT, button);
		
		button=new RoomButton(iClickListener, HandlerMsg.MSG_PLAY, R.drawable.play_bt,ConfigCenter.play_bt_X , y, w, h);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_PLAY, button);
		
		int posX=(DdzApplication.getApplication().screenWidth-ConfigCenter.desposit_tag_bt_W)/2;
		int posY=DdzApplication.getApplication().screenHeight-ConfigCenter.desposit_tag_bt_H;
		button=new RoomButton(iClickListener, HandlerMsg.MSG_CANCEL_DESPOSITE, R.drawable.desposit_tag_bt, posX, posY
				,ConfigCenter.desposit_tag_bt_W, ConfigCenter.desposit_tag_bt_H);
		button.setVisible(isVisible);
		btnMap.put(HandlerMsg.MSG_CANCEL_DESPOSITE, button);
		
		gameControlBtnManager=new GameControlBtnManager(mRoom,btnMap,iClickListener);
	}
	
	
	public void clearAll(){
		if(null!=fixationBtnManager){
			fixationBtnManager.clear();
		}
		fixationBtnManager = null;
		if(null!=gameControlBtnManager){
			gameControlBtnManager.clear();
		}
		gameControlBtnManager = null;
	}
	
	/**
	 * 重置所有按钮状态:fixationBtnManager和gameControlBtnManager
	 */
	public void reset() {
		if(null!=fixationBtnManager){
			fixationBtnManager.reset();
		}
		
		if(null!=gameControlBtnManager){
			gameControlBtnManager.reset();
		}
	}
	
	public void initDisplayButton(){
		
	}
	
	public void draw(PCanvas canvas, int gameState){
		//----房间固定按钮-----
		fixationBtnManager.draw(canvas);
		// ----玩牌控制牌按钮----
		if (GameState.GAME_OVER != gameState
				&& !RoomInfo.getInstance().isSendReqing()
				&& !RoomInfo.getInstance().isRefight()) {
			gameControlBtnManager.draw(canvas);
		}
	}
	
	public boolean dispatch(MotionEvent event){
		boolean result=false;
		//----房间固定按钮-----
		if(!result){
			if(fixationBtnManager!=null){
				result=fixationBtnManager.touch(event);
			}
		}
		//----玩牌控制牌按钮----
		if(!result){
			if(gameControlBtnManager!=null){
				result=gameControlBtnManager.touch(event);
			}
		}
		return result;
	}

	@Override
	public void updateState(int msg, boolean isVisible, boolean enable) {
		if(fixationBtnManager.btnMap.containsKey(msg)){
			fixationBtnManager.updateBtnState(msg, isVisible, enable);
		}else if(gameControlBtnManager.btnMap.containsKey(msg)){
			gameControlBtnManager.updateBtn(msg, isVisible, enable);
		}
	}

	@Override
	public void updateState() {
		if(null!=gameControlBtnManager){
			gameControlBtnManager.updateBtnState();
		}
		if(null!=fixationBtnManager){
			fixationBtnManager.updateState();
		}
	}

	@Override
	public void updateGameControlBtnState(ArrayList<Card> selectCards) {
		if(null!=gameControlBtnManager){
			gameControlBtnManager.updateGameControlBtnState(selectCards);
		}
	}

	@Override
	public void updatePlayPrompt() {
		if(null!=gameControlBtnManager){
			gameControlBtnManager.updatePlayPrompt();
		}
	}
}

