using System;

public class ServerListScreen : CScreen, IActionListener
{
	public static string[] nameServer;

	public static string[] address;

	public static short[] port;

	public static int selected;

	public static bool isWait;

	public static Command cmdUpdateServer;

	public static ServerListScreen instance;

	private static sbyte[] language;

	private static bool isVNServer;

	public TField tUser;

	public TField tServer;

	private int xLog;

	private int yLog;

	private new Command[] cmd;

	private int nCmdPlay;

	private int lY;

	public static string linkGetHost;

	public static string linkDefault;

	public new int keyTouch = -1;

	private int tam;

	public static string size;

	public static string size2;

	private bool haveAccount;

	public static int ipSelect;

	private bool isClientRequest;

	public static bool bigOk;

	public static int percent;

	public static string strWait;

	public static int nBig;

	public static int nBg;

	public static int demPercent;

	public static int maxBg;

	public static bool isGetData;

	private Command cmdDownload;

	private Command cmdStart;

	private Command cmdGraphics;

	public string dataSize;

	public static int p;

	public static int testConnect;

	public static bool loadScreen;

	public static bool isLoginde;

	public ServerListScreen()
	{
		int num = 4;
		if (num * 32 + 23 + 33 >= CCanvas.w)
		{
			num--;
		}
		initCommand();
		if (!CCanvas.isTouch)
		{
			selected = 0;
			processInput();
		}
		cmdUpdateServer = new Command();
		cmdUpdateServer.actionChat = delegate(string str)
		{
			removeSpecialChar(str);
			string text = str;
			string text2 = str;
			if (text == null)
			{
				CCanvas.startOKDlg(L.connectFail());
			}
			else
			{
				if (text == null && text2 != null)
				{
					if (text2.Equals(string.Empty) || text2.Length < 20)
					{
						text2 = "The Xiao:127.0.0.1:19150:0";
					}
					getServerList(text2);
				}
				if (text != null && text2 == null)
				{
					if (text.Equals(string.Empty) || text.Length < 20)
					{
						text = "The Xiao:127.0.0.1:19150:0";
					}
					getServerList(text);
				}
				if (text != null && text2 != null)
				{
					if (text.Length > text2.Length)
					{
						getServerList(text);
					}
					else
					{
						getServerList(text2);
					}
				}
			}
		};
	}

	static ServerListScreen()
	{
		linkGetHost = "http://mobiarmy3.com/srvip/";
		linkDefault = "The Xiao:127.0.0.1:19150:0";
		testConnect = -1;
	}

	public static ServerListScreen gI()
	{
		if (instance == null)
		{
			instance = new ServerListScreen();
		}
		return instance;
	}

	private void initCommand()
	{
		nCmdPlay = 0;
		string text = Rms.loadRMSString("armyUser");
		if (text == null)
		{
			if (Rms.loadRMS("userAo") != null)
			{
				nCmdPlay = 1;
			}
		}
		else if (text.Equals(string.Empty))
		{
			if (Rms.loadRMS("userAo") != null)
			{
				nCmdPlay = 1;
			}
		}
		else
		{
			nCmdPlay = 1;
		}
		cmd = new Command[(mGraphics.zoomLevel <= 1) ? (4 + nCmdPlay) : (3 + nCmdPlay)];
		int num = CCanvas.hh - 15 * cmd.Length + 28;
		for (int i = 0; i < cmd.Length; i++)
		{
			switch (i)
			{
			case 0:
				cmd[0] = new Command(string.Empty, this, 3, null);
				if (text == null)
				{
					cmd[0].caption = "Chơi mới";
					if (Rms.loadRMSString("userAo") != null)
					{
						cmd[0].caption = "Chơi tiếp";
					}
					break;
				}
				if (text.Equals(string.Empty))
				{
					cmd[0].caption = "Chơi mới";
					if (Rms.loadRMSString("userAo") != null)
					{
						cmd[0].caption = "Chơi tiếp";
					}
					break;
				}
				cmd[0].caption = "Chơi TK: " + text;
				if (cmd[0].caption.Length > 23)
				{
					cmd[0].caption = cmd[0].caption.Substring(0, 23);
					cmd[0].caption += "...";
				}
				break;
			case 1:
				if (nCmdPlay == 1)
				{
					cmd[1] = new Command(string.Empty, this, 10100, null);
					cmd[1].caption = "Chơi mới";
				}
				else
				{
					cmd[1] = new Command("Chọn tài khoản", this, 7, null);
				}
				break;
			case 2:
				if (nCmdPlay == 1)
				{
					cmd[2] = new Command("Chọn tài khoản", this, 7, null);
				}
				else
				{
					cmd[2] = new Command(string.Empty, this, 5, null);
				}
				break;
			case 3:
				if (nCmdPlay == 1)
				{
					cmd[3] = new Command(string.Empty, this, 5, null);
				}
				else
				{
					cmd[3] = new Command("Cấu hình", this, 8, null);
				}
				break;
			case 4:
				cmd[4] = new Command("Cấu hình", this, 8, null);
				break;
			}
			cmd[i].y = num;
			cmd[i].setType();
			cmd[i].x = (CCanvas.w - cmd[i].w) / 2;
			num += 30;
		}
	}

	public static string removeSpecialChar(string info)
	{
		string text = string.Empty;
		for (int i = 0; i < info.Length; i++)
		{
			if ((info[i] >= '0' && info[i] <= '9') || (info[i] >= 'a' && info[i] <= 'z') || (info[i] >= 'A' && info[i] <= 'Z') || info[i] == ',' || info[i] == ':' || info[i] == '.' || info[i] == ' ')
			{
				text += info[i];
			}
		}
		return text;
	}

	public static void doUpdateServer()
	{
		getServerList(linkDefault);
	}

	public static void getServerList(string str)
	{
		CRes.outz("getServerList str= " + str);
		string[] array = CRes.split(str.Trim(), ",", 0);
		isVNServer = array[array.Length - 1].Equals("0");
		nameServer = new string[array.Length];
		address = new string[array.Length];
		port = new short[array.Length];
		language = new sbyte[array.Length];
		for (int i = 0; i < array.Length; i++)
		{
			string[] array2 = CRes.split(array[i].Trim(), ":", 0);
			nameServer[i] = array2[0];
			address[i] = array2[1];
			port[i] = short.Parse(array2[2]);
			language[i] = sbyte.Parse(array2[3].Trim());
		}
		saveIP();
		CCanvas.splashScr.loadIp();
		CCanvas.endDlg();
	}

	public override void paint(mGraphics g)
	{
		if (!loadScreen)
		{
			g.translate(-g.getTranslateX(), -g.getTranslateY());
			g.setClip(0, 0, CScreen.w, CScreen.h);
			g.setColor(0);
			g.fillRect(0, 0, CScreen.w, CScreen.h, false);
			if (!bigOk && isGetData)
			{
			}
		}
		else
		{
			BackgroundNew.paintBGGameScr(g);
		}
		int y = 2;
		mFont.tahoma_7_white.drawString(g, "v" + MyMidlet.version, CCanvas.w - 2, y, 1);
		int w2 = CCanvas.w;
		if (CCanvas.currentDialog != null || MyMidlet.BIG_PROVIDER != 0)
		{
			return;
		}
		if (!loadScreen)
		{
			if (bigOk)
			{
				return;
			}
			g.drawImage(GameScr.logoGame, CCanvas.hw, CCanvas.hh - 32, 3, false);
			if (isGetData)
			{
				if (cmdDownload != null)
				{
					cmdDownload.paint(g);
				}
				mFont.tahoma_7b_white.drawString(g, "Đang tải dữ liệu", CCanvas.w / 2, CCanvas.hh + 24, 2);
				GameScr.paintOngMauPercent(GameScr.frBarPow20, GameScr.frBarPow21, GameScr.frBarPow22, CCanvas.w / 2 - 50, CCanvas.hh + 45, 100, 100, g);
				GameScr.paintOngMauPercent(GameScr.frBarPow0, GameScr.frBarPow1, GameScr.frBarPow2, CCanvas.w / 2 - 50, CCanvas.hh + 45, 100, percent, g);
			}
			else
			{
				mFont.tahoma_7b_white.drawString(g, "Tải " + dataSize + " dữ liệu để chơi", CCanvas.hw, CCanvas.hh + 24, 2);
				if (cmdDownload != null)
				{
					cmdDownload.paint(g);
				}
			}
		}
		else
		{
			int num = CCanvas.hh - 80;
			if (num < 45)
			{
				num = 45;
			}
			g.drawImage(GameScr.logoGame, CCanvas.hw, num, 3, false);
			for (int i = 0; i < cmd.Length; i++)
			{
				cmd[i].paint(g);
			}
			g.setClip(0, 0, CScreen.w, CScreen.h);
			if (testConnect == -1)
			{
				CCanvas.paintShukiren(5, cmd[2 + nCmdPlay].y + 11, g);
			}
			else
			{
				g.drawRegion(Panel.imgRoomStat, 0, testConnect * 7, 7, 7, 0, (CCanvas.w - mFont.tahoma_7b_dark.getWidth(cmd[2 + nCmdPlay].caption) >> 1) - 10, cmd[2 + nCmdPlay].y + 9, 0, false);
			}
		}
	}

	public override void update()
	{
		BackgroundNew.updateCloud2();
		if (!CCanvas.isTouch)
		{
			for (int i = 0; i < cmd.Length; i++)
			{
				cmd[i].isFocus = i == selected;
			}
		}
		if (!loadScreen)
		{
			Camera.x++;
			if (bigOk || percent == 100)
			{
				cmdDownload = null;
			}
		}
		base.update();
	}

	private void processInput()
	{
		if (!loadScreen && mGraphics.zoomLevel != 1)
		{
			return;
		}
		for (int i = 0; i < cmd.Length; i++)
		{
			if (i == selected)
			{
				cmd[i].isFocus = true;
			}
			else
			{
				cmd[i].isFocus = false;
			}
		}
		center = cmd[selected];
	}

	public override void input()
	{
		if (CCanvas.isTouch)
		{
			if (!loadScreen)
			{
				if (cmdDownload != null && cmdDownload.isPointerPressInside())
				{
					cmdDownload.performAction();
				}
				base.input();
				return;
			}
			for (int i = 0; i < cmd.Length; i++)
			{
				if (cmd[i] != null && cmd[i].isPointerPressInside())
				{
					cmd[i].performAction();
				}
			}
		}
		else if (loadScreen || mGraphics.zoomLevel == 1)
		{
			if (CCanvas.keyPressed[8])
			{
				CCanvas.keyPressed[8] = false;
				selected++;
				if (selected > cmd.Length - 1)
				{
					selected = 0;
				}
				processInput();
			}
			if (CCanvas.keyPressed[2])
			{
				CCanvas.keyPressed[2] = false;
				selected--;
				if (selected < 0)
				{
					selected = cmd.Length - 1;
				}
				processInput();
			}
		}
		if (!isWait)
		{
			base.input();
		}
	}

	public static void saveIP()
	{
		DataOutputStream dataOutputStream = new DataOutputStream();
		try
		{
			dataOutputStream.writeBoolean(isVNServer);
			dataOutputStream.writeByte((sbyte)nameServer.Length);
			for (int i = 0; i < nameServer.Length; i++)
			{
				dataOutputStream.writeUTF(nameServer[i]);
				dataOutputStream.writeUTF(address[i]);
				dataOutputStream.writeShort(port[i]);
				dataOutputStream.writeByte(language[i]);
			}
			Rms.saveRMS("MAlink", dataOutputStream.toByteArray());
			dataOutputStream.close();
		}
		catch (Exception)
		{
		}
	}

	public static void loadIP()
	{
		getServerList("The Xiao:127.0.0.1:19150:0");
	}

	public override void show()
	{
		Camera.y = 0;
		Camera.x = 0;
		initCommand();
		isWait = false;
		LoginScr.instance = null;
		if (Rms.loadRMSInt("vcBig") > 0 || mGraphics.zoomLevel == 1)
		{
			loadScreen = true;
			BackgroundNew.loadBG(6);
		}
		bigOk = true;
		cmd[2 + nCmdPlay].caption = "Máy chủ: " + nameServer[ipSelect];
		base.show();
		if (!CCanvas.isTouch)
		{
			selected = 0;
			processInput();
		}
	}

	public static void login()
	{
		LoginScr.gI().show();
	}

	public void connectOk()
	{
		if (!isClientRequest)
		{
			testConnect = 2;
			Rms.saveRMSInt("svselect", ipSelect);
			MyMidlet.IP = address[ipSelect];
			MyMidlet.PORT = port[ipSelect];
			CRes.saveIP(MyMidlet.IP + ":" + MyMidlet.PORT + ":link");
			isClientRequest = true;
		}
		GameService.gI().setProvider(MyMidlet.PROVIDER);
		GameService.gI().platform_request();
	}

	public void perform(int idAction, object p)
	{
		CRes.outz("perform " + idAction);
		if (idAction == 10100)
		{
			LoginScr.gI().show();
			LoginScr.connect();
			CCanvas.startWaitDlgWithoutCancel(L.pleaseWait());
			GameService.gI().login2(string.Empty);
			CRes.outz("tao user ao");
			LoginScr.serverName = nameServer[ipSelect];
		}
		if (idAction == 1000)
		{
			LoginScr.connect();
		}
		if (idAction == 1 || idAction == 4)
		{
			if (CCanvas.serverScreen == null)
			{
				CCanvas.serverScreen = new ServerListScreen();
			}
			if (!loadScreen)
			{
				GameScr.vBig = -100;
			}
			demPercent = 0;
			percent = 0;
			CCanvas.serverScreen.show2();
			isGetData = false;
			cmdDownload.isFocus = true;
			center = new Command(string.Empty, this, 2, null);
		}
		if (idAction == 2)
		{
			LoginScr.connect();
			cmdDownload = new Command("Hủy", this, 4, null);
			cmdDownload.x = CCanvas.w / 2 - CScreen.cmdW / 2;
			cmdDownload.y = CCanvas.hh + 65;
			center = new Command(string.Empty, this, 4, null);
			if (!isGetData)
			{
				GameService.gI().requestBig();
				if (!CCanvas.isTouch)
				{
					cmdDownload.isFocus = true;
					center = new Command(string.Empty, this, 4, null);
				}
				isGetData = true;
			}
		}
		if (idAction == 3)
		{
			CRes.outz("toi day");
			LoginScr.gI().show();
			bool num = Rms.loadRMSString("armyUser") != null && !Rms.loadRMSString("armyUser").Equals(string.Empty);
			bool flag = Rms.loadRMSString("userAo") != null && !Rms.loadRMSString("userAo").Equals(string.Empty);
			if (!num && !flag)
			{
				LoginScr.connect();
				CCanvas.startWaitDlgWithoutCancel(L.pleaseWait());
				string text = Rms.loadRMSString("userAo");
				if (text == null || text.Equals(string.Empty))
				{
					GameService.gI().login2(string.Empty);
				}
				else
				{
					LoginScr.gI().isLogin2 = true;
					GameService.gI().login(text, string.Empty, MyMidlet.version, 1);
					CCanvas.startWaitDlgWithoutCancel(L.pleaseWait());
				}
			}
			else
			{
				LoginScr.gI().doLogin();
			}
		}
		if (idAction == 5)
		{
			if (mSystem.clientType == mSystem.GOOGLE_PLAY || mSystem.clientType == mSystem.IP_APPSTORE || mSystem.clientType == mSystem.WINDOWS_PHONE)
			{
				doUpdateServer();
			}
			if (nameServer.Length == 1)
			{
				return;
			}
			MyVector myVector = new MyVector(string.Empty);
			for (int i = 0; i < nameServer.Length; i++)
			{
				myVector.addElement(new Command(nameServer[i], this, 6, null));
			}
			CCanvas.menu.startAt(myVector, 0);
		}
		if (idAction == 6)
		{
			ipSelect = CCanvas.menu.menuSelectedItem;
			if (Session_ME.gI().isConnected())
			{
				Session_ME.gI().close();
				testConnect = -1;
			}
			MyMidlet.IP = address[ipSelect];
			MyMidlet.PORT = port[ipSelect];
			initCommand();
			LoginScr.connect();
			cmd[2 + nCmdPlay].caption = "Máy chủ: " + nameServer[ipSelect];
		}
		if (idAction == 7)
		{
			LoginScr.gI().show();
		}
		if (idAction == 8)
		{
			bool num2 = Rms.loadRMSInt("graphic") == 1;
			MyVector myVector2 = new MyVector("cau hinh");
			myVector2.addElement(new Command("Hình ảnh thấp", this, 9, null));
			myVector2.addElement(new Command("Hình ảnh cao", this, 10, null));
			CCanvas.menu.startAt(myVector2, 0);
			if (num2)
			{
				CCanvas.menu.menuSelectedItem = 0;
			}
			else
			{
				CCanvas.menu.menuSelectedItem = 1;
			}
		}
		if (idAction == 9)
		{
			CRes.saveRMSInt("graphic", 1);
			CRes.saveRMSInt("effect", 1);
			CCanvas.startOKDlg("Vui lòng khởi động lại game để thay đổi cấu hình", new Command(L.ok(), this, 12, null));
		}
		if (idAction == 10)
		{
			CRes.saveRMSInt("graphic", 0);
			CRes.saveRMSInt("effect", 0);
			CCanvas.startOKDlg("Vui lòng khởi động lại game để thay đổi cấu hình", new Command(L.ok(), this, 12, null));
		}
		if (idAction == 11)
		{
			LoginScr.gI().show();
			LoginScr.connect();
			CCanvas.startWaitDlgWithoutCancel(L.pleaseWait());
			string text2 = LoginScr.getIMEI();
			if (text2 == null || text2.Equals(string.Empty))
			{
				text2 = mSystem.currentTimeMillis() + "a" + CRes.random(0, 100) + string.Empty;
			}
			Rms.saveRMSString("userAo", text2);
			GameService.gI().login2(text2);
			CRes.outz("tao user ao");
		}
		if (idAction == 12)
		{
			MyMidlet.instance.exit();
		}
	}

	public void init()
	{
		cmd[2].caption = "Máy chủ: " + nameServer[ipSelect];
		if (!loadScreen)
		{
			cmdDownload = new Command("Tải dữ liệu", this, 2, null);
			cmdDownload.isFocus = true;
			cmdDownload.x = CCanvas.w / 2 - CScreen.cmdW / 2;
			cmdDownload.y = CCanvas.hh + 45;
			if (cmdDownload.y > CCanvas.h - 26)
			{
				cmdDownload.y = CCanvas.h - 26;
			}
		}
		if (!CCanvas.isTouch)
		{
			selected = 0;
			processInput();
		}
	}

	public void show2()
	{
		Camera.y = 0;
		Camera.x = 0;
		initCommand();
		loadScreen = false;
		percent = 0;
		bigOk = false;
		isGetData = false;
		p = 0;
		demPercent = 0;
		strWait = L.pleaseWait();
		init();
		base.show();
	}
}
