package Game;

import DreamBubbleMidlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import bt.BTMain;

public class NetGameSelect extends GameCanvas implements Runnable {

	public final static int MAX_SELECT_ROOM_INDEX = 6;
	public final static int MAX_ITEM_INDEX = 2;
	public final static int MAX_SELECT_MAP_INDEX = 5;

	protected DreamBubbleMidlet dreamBubbleMidlet;

	protected Graphics g;
	protected Font font;
	protected Image backgroundImage;
	protected Image initImage;
	protected Image selectMapImage;
	protected Image selectRoomImage;
	protected Image playerListImage;

	protected int screenWidth;
	protected int screenHeight;
	protected int itemIndex = MAX_ITEM_INDEX;
	protected int selectMapIndex = MAX_SELECT_MAP_INDEX;
	protected int selectRoomIndex = MAX_SELECT_ROOM_INDEX;
	protected int waitingPlayer;
	protected int waitingRoom;

	protected boolean isShowPlayerList;
	protected boolean isEnd;
	protected boolean isShowMessageInWaitingPlayer;
	protected boolean isShowMessageInWaitingRoom;
	public boolean isInRoom = false;

	protected String message;

	protected Vector playerList;
	protected Vector roomList;

	public NetGameSelect(DreamBubbleMidlet dreamBubbleMidlet) {
		super(false);
		this.setFullScreenMode(true);
		this.dreamBubbleMidlet = dreamBubbleMidlet;

		this.screenWidth = this.getWidth();
		this.screenHeight = this.getHeight();

		this.waitingPlayer = screenWidth / 2;
		this.waitingRoom = screenWidth / 2;

		try {
			this.backgroundImage = Image.createImage("/Menu/Set.png");
			this.initImage = Image.createImage("/Menu/InitNetGame.png");
			this.selectMapImage = Image.createImage("/Menu/SelectMap.png");
			this.selectRoomImage = Image.createImage("/Menu/SelectRoom.png");
			this.playerListImage = Image.createImage("/Menu/PlayerList.png");
		} catch (IOException e) {
			e.printStackTrace();
		}

		this.g = this.getGraphics();
		this.font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED,
				Font.SIZE_LARGE);
		this.roomList = new Vector();
		this.playerList = new Vector();
	}

	public void run() {
		while (!isEnd) {
			drawScreen();
			this.flushGraphics();
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void drawScreen() {
		if (this.isShowPlayerList) {
			this.drawShowPlayerList();
			return;
		}
		if (this.selectMapIndex < MAX_SELECT_MAP_INDEX) {
			this.drawSelectMapScreen();
			return;
		}

		if (this.selectRoomIndex < MAX_SELECT_ROOM_INDEX) {
			drawSelectRoomScreen();
			return;
		}
		drawInitScreen();
	}

	protected void keyPressed(int key) {
		switch (key) {
		case -1:
		case -3:
			if (this.selectRoomIndex < MAX_SELECT_ROOM_INDEX) {
				if (this.roomList.size() != 0)
					if (this.selectRoomIndex == 0) {
						this.selectRoomIndex = this.roomList.size() - 1;
					} else {
						this.selectRoomIndex = (this.selectRoomIndex - 1)
								% this.roomList.size();
					}
				return;
			}

			if (this.selectMapIndex < MAX_SELECT_MAP_INDEX) {
				if (this.selectMapIndex == 0) {
					this.selectMapIndex = MAX_SELECT_MAP_INDEX - 1;
				} else {
					this.selectMapIndex = (this.selectMapIndex - 1)
							% MAX_SELECT_MAP_INDEX;
				}
				return;
			}

			if (this.itemIndex == MAX_ITEM_INDEX) {
				this.itemIndex = 0;
			} else if (this.itemIndex == 0) {
				this.itemIndex = 1;
			} else if (this.itemIndex == 1) {
				this.itemIndex = 0;
			}
			return;
		case -2:
		case -4:
			if (this.selectRoomIndex < MAX_SELECT_ROOM_INDEX) {
				if (this.roomList.size() != 0)
					this.selectRoomIndex = (this.selectRoomIndex + 1)
							% this.roomList.size();
				return;
			}
			if (this.selectMapIndex < MAX_SELECT_MAP_INDEX) {
				this.selectMapIndex = (this.selectMapIndex + 1)
						% MAX_SELECT_MAP_INDEX;
				return;
			}
			if (this.itemIndex == MAX_ITEM_INDEX) {
				this.itemIndex = 0;
			} else if (this.itemIndex == 0) {
				this.itemIndex = 1;
			} else if (this.itemIndex == 1) {
				this.itemIndex = 0;
			}
			return;
		case -5:
		case -6:
			// 主机开始游戏
			if (isShowPlayerList) {
				if (this.playerList.size() != 0)
					this.startGame();
				return;
			}

			// 主机选择地图之后
			if (this.selectMapIndex < MAX_SELECT_MAP_INDEX) {
				isShowPlayerList = true;
				switch (selectMapIndex) {
				case 3:
					selectMapIndex = 4;
					break;
				case 4:
					selectMapIndex = 3;
					break;
				}
				this.waitingPlayer = screenWidth / 2;
				this.checkPlayer(selectMapIndex,
						this.dreamBubbleMidlet.menu.netGameHeroType);
				this.selectMapIndex = MAX_SELECT_MAP_INDEX;
				return;
			}

			// 客户选择房间之后，点击进入房间
			if (this.selectRoomIndex < MAX_SELECT_ROOM_INDEX) {
				if (this.roomList.size() != 0)
					this.enterRoom((String) this.roomList
							.elementAt(this.selectRoomIndex),
							this.selectRoomIndex);
				return;
			}

			// 在主菜单中选项
			if (this.itemIndex < MAX_ITEM_INDEX) {
				if (this.itemIndex == 0) {
					this.itemIndex = MAX_ITEM_INDEX;
					this.selectMapIndex = 0;
					return;
				}
				if (this.itemIndex == 1) {
					this.itemIndex = MAX_ITEM_INDEX;
					this.waitingRoom = screenWidth / 2;
					checkRoom(this.dreamBubbleMidlet.menu.netGameHeroType);
					this.selectRoomIndex = 0;
					return;
				}
			}
			return;
		case -7:
			if (this.isShowPlayerList) {
				this.playerList.removeAllElements();
				this.isShowPlayerList = false;
				this.selectMapIndex = 0;
				return;
			}
			if (this.selectMapIndex < MAX_SELECT_MAP_INDEX) {
				this.itemIndex = MAX_ITEM_INDEX;
				this.selectMapIndex = MAX_SELECT_MAP_INDEX;
				return;
			}
			if (this.selectRoomIndex < MAX_SELECT_ROOM_INDEX) {
				this.roomList.removeAllElements();
				this.itemIndex = MAX_ITEM_INDEX;
				this.selectRoomIndex = MAX_SELECT_ROOM_INDEX;
				return;
			}
			showMenu();
			return;
		}
	}

	public void drawShowPlayerList() {
		g.setColor(0x000000);
		g.fillRect(0, 0, getWidth(), getHeight());

		g.drawImage(backgroundImage, screenWidth / 2, screenHeight / 2,
				Graphics.VCENTER | Graphics.HCENTER);
		g.drawImage(playerListImage, screenWidth / 2, screenHeight / 4,
				Graphics.VCENTER | Graphics.HCENTER);

		if (this.isShowMessageInWaitingPlayer) {
			this.font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
					Font.SIZE_LARGE);
			g.setFont(font);
			int stringLength = font.stringWidth(this.message);

			if (this.waitingPlayer - stringLength / 2 < this.screenWidth) {
				this.waitingPlayer += 2;
			} else {
				this.waitingPlayer = 0 - stringLength / 2;
			}

			g.drawString(message, waitingPlayer, 40, Graphics.TOP
					| Graphics.HCENTER);
		}
		Enumeration enu = this.playerList.elements();
		int index = 0;
		while (enu.hasMoreElements()) {
			String playerName = (String) enu.nextElement();
			this.font = Font.getFont(Font.FACE_PROPORTIONAL,
					Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
			g.setColor(0x0000ff);
			g.setFont(font);
			g.drawString(playerName, this.screenWidth / 2, 100 + index * 30,
					Graphics.HCENTER | Graphics.TOP);
			index++;
		}
	}

	public void drawSelectRoomScreen() {
		g.setColor(0x000000);
		g.fillRect(0, 0, getWidth(), getHeight());

		g.drawImage(backgroundImage, screenWidth / 2, screenHeight / 2,
				Graphics.VCENTER | Graphics.HCENTER);
		g.drawImage(selectRoomImage, screenWidth / 2, screenHeight / 4,
				Graphics.VCENTER | Graphics.HCENTER);

		if (this.isShowMessageInWaitingRoom) {
			this.font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
					Font.SIZE_LARGE);
			g.setFont(font);
			int stringLength = font.stringWidth(this.message);

			if (this.waitingRoom - stringLength / 2 < this.screenWidth) {
				this.waitingRoom += 2;
			} else {
				this.waitingRoom = 0 - stringLength / 2;
			}

			g.drawString(this.message, waitingRoom, 40, Graphics.TOP
					| Graphics.HCENTER);
		}

		Enumeration enu = this.roomList.elements();
		int index = 0;
		while (enu.hasMoreElements()) {
			String roomName = (String) enu.nextElement();
			if (this.selectRoomIndex < MAX_SELECT_ROOM_INDEX) {
				if (index == this.selectRoomIndex) {
					this.font = Font.getFont(Font.FACE_PROPORTIONAL,
							Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
					g.setColor(0x0000ff);
					g.setFont(font);
					g.drawString(roomName, this.screenWidth / 2,
							100 + index * 30, Graphics.HCENTER | Graphics.TOP);
				} else {
					this.font = Font.getFont(Font.FACE_PROPORTIONAL,
							Font.STYLE_BOLD, Font.SIZE_MEDIUM);
					g.setColor(0x0000ff);
					g.setFont(font);
					g.drawString(roomName, this.screenWidth / 2,
							100 + index * 30, Graphics.HCENTER | Graphics.TOP);
				}
			}
			index++;
		}
	}

	public void drawSelectMapScreen() {
		g.setColor(0x000000);
		g.fillRect(0, 0, getWidth(), getHeight());

		g.drawImage(backgroundImage, screenWidth / 2, screenHeight / 2,
				Graphics.VCENTER | Graphics.HCENTER);
		g.drawImage(selectMapImage, screenWidth / 2, screenHeight / 2,
				Graphics.VCENTER | Graphics.HCENTER);

		if (this.selectMapIndex < 5) {
			g.setColor(0x0000ff);
			if (this.selectMapIndex < 4) {
				g.drawRect((screenWidth - 50) / 2 - 5,
						80 + 42 * selectMapIndex - 5, 50 + 10, 24 + 10);
			} else {
				g.drawRect((screenWidth - 70) / 2 - 5,
						80 + 42 * selectMapIndex - 5, 70 + 10, 24 + 10);
			}
		}
	}

	public void addRoom(String roomName) {
		this.roomList.addElement(roomName);
	}

	public void removeRoom(String roomName) {
		this.roomList.removeElement(roomName);
	}

	public void addPlayer(String playerName) {
		this.playerList.addElement(playerName);
	}

	public void removePlayer(String playerName) {
		this.playerList.removeElement(playerName);
	}

	public void isShowMessageInWaitingPlayer(
			boolean isShowMessageInWaitingPlayer, String message) {
		this.isShowMessageInWaitingPlayer = isShowMessageInWaitingPlayer;
		this.message = message;
	}

	public void isShowMessageInWaitingRoom(boolean isShowMessageInWaitingRoom,
			String message) {
		this.isShowMessageInWaitingRoom = isShowMessageInWaitingRoom;
		this.message = message;
	}

	// 作为客户端加入游戏
	private void checkRoom(int role) {
		BTMain btmain = new BTMain(this.dreamBubbleMidlet, this);
		btmain.startSearch(false, (byte) -1, (byte) role);
		isShowMessageInWaitingRoom(true, "正在寻找主机。。。");
	}

	/**
	 * 写你自己的进入房间的代码
	 * 
	 * @param roomName
	 */
	private void enterRoom(String roomName, int index) {
		if (!isInRoom)
			BTMain.instance.enterRoom();
		else
			isShowMessageInWaitingRoom(true, "您已经加入该房间");
	}

	private void checkPlayer(int map, int role) {
		BTMain btmain = new BTMain(this.dreamBubbleMidlet, this);
		btmain.startSearch(true, (byte) map, (byte) role);
		isShowMessageInWaitingPlayer(true, "正在等待玩家。。。");
	}

	/**
	 * 写你自己的进入游戏的代码
	 */
	private void startGame() {
		BTMain.instance.startGame();
	}

	public void drawInitScreen() {
		g.setColor(0x000000);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.drawImage(backgroundImage, screenWidth / 2, screenHeight / 2,
				Graphics.VCENTER | Graphics.HCENTER);
		g.drawImage(initImage, screenWidth / 2, screenHeight / 2,
				Graphics.VCENTER | Graphics.HCENTER);

		if (this.itemIndex == 0) {
			g.setColor(0x0000ff);
			g.drawRect((screenWidth - initImage.getWidth()) / 2 - 5,
					(screenHeight - initImage.getHeight()) / 2 - 5, initImage
							.getWidth() + 10, initImage.getHeight() / 3 + 10);
		} else if (this.itemIndex == 1) {
			g.setColor(0x0000ff);
			g.drawRect((screenWidth - initImage.getWidth()) / 2 - 5,
					(screenHeight + initImage.getHeight() / 2) / 2 - 15,
					initImage.getWidth() + 10, initImage.getHeight() / 3 + 10);
		}
	}

	public void showMenu() {
		this.isEnd = true;
		this.itemIndex = MAX_ITEM_INDEX;
		this.selectMapIndex = MAX_SELECT_MAP_INDEX;
		this.selectRoomIndex = MAX_SELECT_ROOM_INDEX;
		this.isShowPlayerList = false;
		this.dreamBubbleMidlet.menu.showMe();
	}

	public void releasReource() {
		this.isEnd = true;
	}

	public void showMe() {
		new Thread(this).start();
		this.dreamBubbleMidlet.show(this);
	}
}
