using Assets.src.screen;

namespace Assets.src.rpg
{
	public class GameScrRPG : CScreen, IActionListener
	{
		public const sbyte MAX_PLAYER = 100;

		public static CMapRPG map;

		public static CMapRPG mapItem;

		public ShotRPG[] shots;

		private Camera cam;

		public static PM pm;

		public static MyVector chatList = new MyVector("chatList");

		public static MyVector vNpc = new MyVector("vNpc");

		private static int chatDelay;

		private static int MAX_CHAT_DELAY = 140;

		public static sbyte zone;

		public static sbyte myIndex;

		public static Image imgChest;

		public static FrameImage frChest;

		private int iEnemy;

		private int iFreeMode;

		private int ptLastDownX;

		private int ptLastDownY;

		private int ptFirstDownX;

		private int ptFirstDownY;

		private int ptDownTime;

		private int iFramePointer;

		private int nhun;

		public long lastSingleClick;

		private bool disableSingleClick;

		private int lastClickCMX;

		private int lastClickCMY;

		private int coolDownTime;

		public bool isChestScreen;

		private bool lockInput;

		private bool firstChest;

		private int delayChest;

		private Command cmdChest;

		private Item[] itemChest;

		private int tRung;

		private static int[] FRAME_OPEN_CHEST;

		public int focusChest;

		private int[] isOpenChest = new int[9];

		private int[] frameOpenChest = new int[9];

		public int tFocus;

		public static int curFocus = -1;

		private int tList;

		private int tListStart;

		private int hList;

		public GameScrRPG()
		{
			FRAME_OPEN_CHEST = new int[6] { 0, 0, 1, 1, 2, 2 };
			if (imgChest == null)
			{
				imgChest = CCanvas.loadImage("/gui2/chest.png");
			}
			if (frChest == null)
			{
				frChest = new FrameImage(imgChest, 32, 32);
			}
			pm = new PM();
			pm.init();
			PM.p2 = new CPlayer[PM.MAX_PLAYER];
			shots = new ShotRPG[100];
			iEnemy = 0;
			Camera.mode = 1;
			cam = new Camera();
			Camera.setPlayerModeRPG();
			if (CCanvas.isTouch)
			{
				tList = 0;
				tListStart = -GameScr.hBalo + 22;
			}
			else
			{
				tList = 0;
				tListStart = CScreen.h - 26 + GameScr.hBalo + 4;
			}
			GameScr.focusCoor = new int[16][]
			{
				new int[2]
				{
					CScreen.w - 58,
					12
				},
				new int[2]
				{
					CScreen.w - 14,
					10
				},
				new int[2]
				{
					CScreen.w - 16,
					12
				},
				new int[2] { 13, 9 },
				new int[2]
				{
					CScreen.w - 19,
					CScreen.h - 17
				},
				new int[2]
				{
					17,
					CScreen.h - 17
				},
				new int[2]
				{
					49,
					CScreen.h - 17
				},
				new int[2]
				{
					CScreen.w - 58,
					12
				},
				new int[2]
				{
					CScreen.w - 20,
					CScreen.h - 53
				},
				new int[2]
				{
					CScreen.w - 50,
					CScreen.h - 40
				},
				new int[2]
				{
					CScreen.w - 17,
					12
				},
				new int[2] { 29, 130 },
				new int[0],
				new int[0],
				new int[0],
				new int[0]
			};
		}

		public void initGame()
		{
			BackgroundNew.loadBG(15);
			GameScr.sm = new SmokeManager();
			iEnemy = 0;
			CMapRPG.initMapImage();
			map = new CMapRPG();
			map.loadMap("map0");
			if (!BackgroundNew.lowGraphic)
			{
				mapItem = new CMapRPG();
				mapItem.loadMap("itemmap0");
			}
		}

		public int nextTarget(int curTarget)
		{
			if (PM.getMyPlayer2().target == -1 && vNpc.size() > 0)
			{
				PM.getMyPlayer2().charFocus = null;
			}
			if (PM.getMyPlayer2().charFocus != null)
			{
				for (int i = 0; i < 100; i++)
				{
					if (i > curTarget && PM.p2[i] != null && PM.p2[i] != PM.getMyPlayer2() && PM.p2[i].state != 5)
					{
						PM.getMyPlayer2().charFocus = PM.p2[i];
						PM.getMyPlayer2().npcFocus = null;
						return i;
					}
				}
				if (vNpc.size() > 0)
				{
					Npc npc = (Npc)vNpc.elementAt(0);
					PM.getMyPlayer2().npcFocus = npc;
					PM.getMyPlayer2().charFocus = null;
					return npc.npcId;
				}
			}
			else
			{
				int num = 0;
				if (vNpc.size() == 0)
				{
					return -1;
				}
				if (curTarget == -1 || vNpc.size() == 1)
				{
					Npc npc2 = (Npc)vNpc.elementAt(0);
					PM.getMyPlayer2().npcFocus = npc2;
					PM.getMyPlayer2().charFocus = null;
					return npc2.npcId;
				}
				for (int j = 0; j < vNpc.size(); j++)
				{
					Npc npc3 = (Npc)vNpc.elementAt(j);
					if (npc3.npcId == curTarget)
					{
						num = j;
						break;
					}
				}
				if (num == vNpc.size() - 1)
				{
					for (int k = 0; k < 100; k++)
					{
						if (PM.p2[k] != null && PM.p2[k] != PM.getMyPlayer2() && PM.p2[k].state != 5)
						{
							PM.getMyPlayer2().charFocus = PM.p2[k];
							PM.getMyPlayer2().npcFocus = null;
							return k;
						}
					}
					if (vNpc.size() > 0)
					{
						Npc npc4 = (Npc)vNpc.elementAt(0);
						PM.getMyPlayer2().npcFocus = npc4;
						PM.getMyPlayer2().charFocus = null;
						return npc4.npcId;
					}
				}
				else
				{
					for (int l = 0; l < vNpc.size(); l++)
					{
						if (l > num)
						{
							Npc npc5 = (Npc)vNpc.elementAt(l);
							PM.getMyPlayer2().npcFocus = npc5;
							PM.getMyPlayer2().charFocus = null;
							return npc5.npcId;
						}
					}
				}
			}
			return -1;
		}

		public int searchNearTarget()
		{
			int num = 0;
			int num2 = 0;
			int index = 0;
			CPlayer myPlayer = PM.getMyPlayer2();
			for (int i = 0; i < vNpc.size(); i++)
			{
				Npc npc = (Npc)vNpc.elementAt(i);
				if (npc != null)
				{
					num++;
					int num3 = Math.abs(Math.Sqrt((myPlayer.x - npc.x) * (myPlayer.x - npc.x) - (myPlayer.y - npc.y) * (myPlayer.y - npc.y)));
					if (num == 1)
					{
						num2 = num3;
						index = i;
					}
					else if (num3 < num2)
					{
						num2 = num3;
						index = i;
					}
				}
			}
			if (num > 0)
			{
				PM.getMyPlayer2().npcFocus = (Npc)vNpc.elementAt(index);
				PM.getMyPlayer2().charFocus = null;
				return ((Npc)vNpc.elementAt(index)).npcId;
			}
			PM.getMyPlayer2().charFocus = null;
			PM.getMyPlayer2().npcFocus = null;
			return -1;
		}

		public void createShot(sbyte type, int x, int y, int target)
		{
			if (target == -1)
			{
				return;
			}
			int num = 0;
			for (int i = 0; i < shots.Length; i++)
			{
				if (shots[i] == null)
				{
					num = i;
					break;
				}
			}
			shots[num] = new ShotRPG();
			shots[num].set(type, PM.getMyPlayer2().x, PM.getMyPlayer2().y, 0, PM.p2[target]);
		}

		public void createEnemyAt(int xTile, int yTile)
		{
		}

		public int nEnemy()
		{
			int num = 0;
			for (int i = 0; i < PM.p2.Length; i++)
			{
				if (PM.p2[i] != null)
				{
					num++;
				}
			}
			return num - 1;
		}

		public override void input()
		{
			if (PM.getMyPlayer2() == null)
			{
				return;
			}
			if (isChestScreen)
			{
				inputChest();
				return;
			}
			if (CCanvas.isPointerJustRelease)
			{
				if (CCanvas.panel.nMail > 0 && !CCanvas.panel.isShow && CCanvas.isPointer(CCanvas.w - 30, 20, 30, 30) && CCanvas.isPointerClick)
				{
					CCanvas.isPointerClick = false;
					CCanvas.panel.setTypeMail();
					CCanvas.panel.show();
					CCanvas.isPointerJustRelease = false;
					return;
				}
				if (CCanvas.isPointer(0, 0, 26, 66) && !CCanvas.panel.isShow)
				{
					CCanvas.panel.setTypeRPG();
					CCanvas.panel.show();
					curFocus = 3;
					CCanvas.isPointerJustRelease = false;
					return;
				}
				if (CCanvas.isPointer(CScreen.w - 35, 0, 35, 20))
				{
					if (CCanvas.currentDialog == null)
					{
						CCanvas.inputDlg = new InputDlg();
						CCanvas.inputDlg.setInfo("Chat", new Command(L.ok(), this, 1, null), new Command(L.close(), this, 4, null), TField.INPUT_TYPE_ANY);
						CCanvas.inputDlg.show();
						if (CCanvas.isTouch && mGraphics.zoomLevel > 1)
						{
							CCanvas.inputDlg.tfInput.doChangeToTextBox(false);
						}
						if (Main.isWindowsPhone)
						{
							CCanvas.inputDlg.tfInput.setFocusWithKb(true);
							CCanvas.inputDlg.tfInput.cmdDoneAction = new Command(L.ok(), this, 1, null);
						}
						curFocus = 1;
					}
					CCanvas.isPointerJustRelease = false;
					return;
				}
			}
			if (CCanvas.isPointerClick)
			{
				for (int i = 0; i < vNpc.size(); i++)
				{
					Npc npc = (Npc)vNpc.elementAt(i);
					if (npc.isClickNPC(CCanvas.px + Camera.x, CCanvas.py + Camera.y))
					{
						CCanvas.isPointerClick = false;
						break;
					}
				}
				for (int j = 0; j < 100; j++)
				{
					if (PM.p2[j] != null && !PM.p2[j].isMe && PM.p2[j].isClickChar(CCanvas.px + Camera.x, CCanvas.py + Camera.y))
					{
						CCanvas.isPointerClick = false;
						break;
					}
				}
			}
			PM.getMyPlayer2().input2();
			if (curFocus == 5 || curFocus == 6 || curFocus == 4)
			{
				return;
			}
			if (CCanvas.isTouch)
			{
				if (PM.getMyPlayer2().isHoldAngle || PM.getMyPlayer2().isHoldFire)
				{
					return;
				}
				if (CCanvas.isPointerJustDown)
				{
					CCanvas.isPointerJustDown = false;
					ptDownTime = 0;
					ptLastDownX = (ptFirstDownX = CCanvas.px);
					ptLastDownY = (ptFirstDownY = CCanvas.py);
				}
				if (CCanvas.isPointerDown)
				{
					int num = CCanvas.px - ptLastDownX;
					int num2 = CCanvas.py - ptLastDownY;
					iFreeMode = 0;
					Camera.mode = 9;
					ptLastDownX = CCanvas.px;
					ptLastDownY = CCanvas.py;
					ptDownTime++;
					Camera.dx2 -= num;
					Camera.dy2 -= num2;
				}
				if (Camera.mode == 9 && !CCanvas.isPointerDown)
				{
					iFreeMode++;
					if (iFreeMode > 25)
					{
						iFreeMode = 0;
						Camera.setPlayerModeRPG();
					}
				}
				if (PM.getMyPlayer2().isFlyAvenger() && CCanvas.isPointerJustRelease && CCanvas.isPointerClick)
				{
					if (!PM.getMyPlayer2().isFreeze)
					{
						PM.getMyPlayer2().xToNow = CCanvas.px + Camera.x;
						PM.getMyPlayer2().yToNow = CCanvas.py + Camera.y;
						PM.getMyPlayer2().isPointAvenger = true;
						PM.getMyPlayer2().state = 1;
						PM.getMyPlayer2().isMoveAvenger = true;
						Camera.setPlayerModeRPG();
					}
					CCanvas.isPointerJustRelease = false;
				}
			}
			checkClick();
			if (CCanvas.keyPressed[12])
			{
				CCanvas.panel.setTypeRPG();
				CCanvas.panel.show();
				CScreen.clearKey();
			}
			else
			{
				base.input();
			}
		}

		private void checkClick()
		{
			if (CCanvas.currentDialog != null || PM.getMyPlayer2() == null)
			{
				return;
			}
			long num = mSystem.currentTimeMillis();
			if (lastSingleClick != 0L && num - lastSingleClick > 300)
			{
				lastSingleClick = 0L;
				CCanvas.isPointerJustDown = false;
				if (!disableSingleClick)
				{
					checkSingleClick();
					CCanvas.isPointerClick = false;
				}
			}
			if (CCanvas.isPointerJustRelease)
			{
				if (num - lastSingleClick < 300)
				{
					lastSingleClick = 0L;
					checkDoubleClick();
				}
				else
				{
					lastSingleClick = num;
					lastClickCMX = Camera.x;
					lastClickCMY = Camera.y;
				}
				CCanvas.isPointerJustRelease = false;
			}
		}

		private void checkSingleClick()
		{
			int num = CCanvas.px + lastClickCMX;
			int num2 = CCanvas.py + lastClickCMY;
		}

		private void checkDoubleClick()
		{
			int x = CCanvas.px + lastClickCMX;
			int y = CCanvas.py + lastClickCMY;
			for (int i = 0; i < PM.p2.Length; i++)
			{
				if (PM.p2[i] != null && CRes.inRect(x, y, PM.p2[i].x - 10, PM.p2[i].y - 40, 20, 40))
				{
					PM.getMyPlayer2().charFocus = PM.p2[i];
					PM.getMyPlayer2().npcFocus = null;
					PM.getMyPlayer2().target = i;
					PM.p2[i].doFireRPG();
				}
			}
			for (int j = 0; j < vNpc.size(); j++)
			{
				Npc npc = (Npc)vNpc.elementAt(j);
				if (npc != null && CRes.inRect(x, y, npc.x - 10, npc.y - 40, 20, 40))
				{
					PM.getMyPlayer2().charFocus = null;
					PM.getMyPlayer2().npcFocus = npc;
					PM.getMyPlayer2().target = npc.npcId;
					npc.doFireRPG();
				}
			}
		}

		public override void update()
		{
			BackgroundNew.updateCloud2();
			BackgroundNew.updateFog();
			for (int i = 0; i < vNpc.size(); i++)
			{
				((Npc)vNpc.elementAt(i)).update();
			}
			if (curFocus != -1 && curFocus != 4)
			{
				tFocus++;
				if (tFocus > 10)
				{
					tFocus = 0;
					curFocus = -1;
				}
			}
			if (PM.getMyPlayer2().isPointAvenger)
			{
				PM.getMyPlayer2().isPointerMove = false;
				iFramePointer++;
				if (iFramePointer > 7)
				{
					iFramePointer = 0;
					PM.getMyPlayer2().isPointAvenger = false;
				}
			}
			updateChat();
			updateHeadUpDisplay();
			if (CCanvas.gameTick % 2 == 0)
			{
				nhun++;
				if (nhun > 5)
				{
					nhun = 0;
				}
			}
			for (int j = 0; j < shots.Length; j++)
			{
				if (shots[j] != null)
				{
					if (shots[j].sleep)
					{
						shots[j] = null;
					}
					else
					{
						shots[j].update();
					}
				}
			}
			pm.update2();
			GameScr.sm.update();
			EffecMn.update();
			cam.update();
			Camera.restrict(24, CMapRPG.pxw - 24, 0, CMapRPG.pxh - 24);
		}

		private void updateHeadUpDisplay()
		{
			if (coolDownTime < 100)
			{
				coolDownTime += 5;
			}
			if (coolDownTime > 100)
			{
				coolDownTime = 100;
			}
		}

		private void paintHeadUpDisplay(mGraphics g)
		{
		}

		public void paintTouch(mGraphics g, int x, int y)
		{
			g.drawImage(GameScr.arrowKey, 0, CScreen.h, CRes.BOTTOM_LEFT, false);
			g.drawImage(GameScr.ban, CScreen.w, CScreen.h, CRes.BOTTOM_RIGHT, false);
			g.drawImage(GameScr.function, CScreen.w, 0, CRes.TOP_RIGHT, false);
			g.drawImage(GameScr.list1, 0, 0, 0, false);
		}

		public static void paintLevelPercen(mGraphics g, int x, int y)
		{
			CPlayer myInfo = MyMidlet.myInfo;
			int perCent = myInfo.perCent;
			GameScr.paintOngMau(GameScr.frBack0, GameScr.frBack1, GameScr.frBack2, x, y, 50, g);
			g.setClip(x, y, perCent >> 1, 7);
			GameScr.paintOngMau(GameScr.frBarPow20, GameScr.frBarPow21, GameScr.frBarPow22, x, y, g);
			CCanvas.resetTrans(g);
		}

		public static void paintLevelPercen2(mGraphics g, int x, int y)
		{
			CPlayer myInfo = MyMidlet.myInfo;
			int avenger = myInfo.avenger;
			GameScr.paintOngMau(GameScr.frBack0, GameScr.frBack1, GameScr.frBack2, x, y, 50, g);
			g.setClip(x, y, avenger >> 1, 7);
			GameScr.paintOngMau(GameScr.frBarPow0, GameScr.frBarPow1, GameScr.frBarPow2, x, y, g);
			CCanvas.resetTrans(g);
		}

		public void paintInfo(mGraphics g)
		{
			mFont tahoma_7_yellow = mFont.tahoma_7_yellow;
			mFont tahoma_7_grey = mFont.tahoma_7_grey;
			int hw = CCanvas.hw;
			CPlayer myInfo = MyMidlet.myInfo;
			int num = 2;
			g.drawImage(Panel.coin, hw - 50, num, 0, false);
			tahoma_7_yellow.drawString(g, MyMidlet.myInfo.strXu, hw - 28, num + 2, 0, tahoma_7_grey);
			num = 2;
			g.drawImage(Panel.gold, hw + 40, num, 0, false);
			tahoma_7_yellow.drawString(g, MyMidlet.myInfo.strLuong, hw + 62, num + 2, 0, tahoma_7_grey);
		}

		public void initChest()
		{
			if (!CCanvas.isTouch)
			{
				cmdChest = new Command("Mở", this, 33, null);
			}
			else
			{
				cmdChest = new Command("Đóng", this, 32, null);
			}
			cmdChest.isFocus = true;
			cmdChest.x = CCanvas.w / 2 - CScreen.cmdW / 2;
			int num = 3;
			int num2 = 32;
			int num3 = CScreen.h - num2 * num >> 1;
			cmdChest.y = num3 + 110;
			isChestScreen = true;
			lockInput = false;
			firstChest = false;
			delayChest = 0;
			isOpenChest = new int[9];
			frameOpenChest = new int[9];
			itemChest = new Item[9];
			if (CCanvas.isTouch)
			{
				focusChest = -1;
			}
			center = cmdChest;
		}

		public void resultChest(Item[] t)
		{
			for (int i = 0; i < 9; i++)
			{
				itemChest[i] = t[i];
			}
		}

		private void inputChest()
		{
			tRung--;
			if (tRung < 0)
			{
				tRung = 0;
			}
			if (CCanvas.isTouch)
			{
				if (cmdChest != null && cmdChest.isPointerPressInside())
				{
					cmdChest.performAction();
				}
			}
			else
			{
				base.input();
			}
			if (firstChest)
			{
				delayChest++;
				if (delayChest > 30)
				{
					delayChest = 31;
					for (int i = 0; i < 9; i++)
					{
						if (isOpenChest[i] == 0)
						{
							isOpenChest[i] = 1;
							break;
						}
					}
				}
			}
			for (int j = 0; j < 9; j++)
			{
				if (isOpenChest[j] == 1)
				{
					frameOpenChest[j]++;
					if (frameOpenChest[j] > 5)
					{
						frameOpenChest[j] = 5;
						isOpenChest[j] = 2;
						firstChest = true;
					}
				}
			}
			if (lockInput)
			{
				return;
			}
			if (CCanvas.keyPressed[2])
			{
				tRung = 50;
				focusChest -= 3;
				if (focusChest < 0)
				{
					focusChest += 9;
				}
				CCanvas.keyPressed[2] = false;
			}
			if (CCanvas.keyPressed[8])
			{
				tRung = 50;
				focusChest += 3;
				if (focusChest > 8)
				{
					focusChest -= 9;
				}
				CCanvas.keyPressed[8] = false;
			}
			if (CCanvas.keyPressed[4])
			{
				tRung = 50;
				focusChest--;
				if (focusChest < 0)
				{
					focusChest = 8;
				}
				CCanvas.keyPressed[4] = false;
			}
			if (CCanvas.keyPressed[6])
			{
				tRung = 50;
				focusChest++;
				if (focusChest > 8)
				{
					focusChest = 0;
				}
				CCanvas.keyPressed[6] = false;
			}
			int num = 3;
			int num2 = 3;
			int num3 = 32;
			int num4 = CScreen.w - num3 * num >> 1;
			int num5 = CScreen.h - num3 * num2 >> 1;
			int num6 = num4;
			for (int k = 0; k < num2; k++)
			{
				num4 = num6;
				for (int l = 0; l < num; l++)
				{
					if (CCanvas.isPointerJustRelease && CCanvas.isPointer(num4, num5 + k * num3, num3, num3) && CCanvas.isPointerClick)
					{
						focusChest = l + k * 3;
						isOpenChest[focusChest] = 1;
						CCanvas.isPointerClick = false;
						CCanvas.isPointerJustRelease = false;
						GameService.gI().openChest();
						InfoDlg.showWait();
						lockInput = true;
						return;
					}
					num4 += num3;
				}
			}
		}

		public void paintChest(mGraphics g)
		{
			int num = 3;
			int num2 = 3;
			int num3 = 32;
			int num4 = CScreen.w - num3 * num >> 1;
			int num5 = CScreen.h - num3 * num2 >> 1;
			int num6 = num4;
			CScreen.paintBorderRect2(g, num4 - 3, num5 + 1, 106, 106);
			for (int i = 0; i < num2; i++)
			{
				num4 = num6;
				for (int j = 0; j < num; j++)
				{
					int num7 = j + i * 3;
					int num8 = 0;
					if (isOpenChest[num7] == 1 || (!CCanvas.isTouch && tRung > 0 && focusChest == num7))
					{
						num8 = ((CCanvas.gameTick % 3 != 0) ? ((CCanvas.gameTick % 3 == 1) ? 1 : (-1)) : 0);
					}
					frChest.drawFrame(FRAME_OPEN_CHEST[frameOpenChest[num7]], num4 + num8, num5 + i * num3, 0, 0, g, false);
					if (num7 == focusChest && CCanvas.gameTick % 5 > 2)
					{
						g.drawImage(GameScr.focus, num4 + 1 + 15, num5 + i * num3 + 5 + 15, 3, false);
					}
					if (isOpenChest[num7] == 2)
					{
						Item item = itemChest[num7];
						if (item != null)
						{
							SmallImage.drawSmallImage(g, item.template.iconID, num4 + 16, num5 + i * num3 + 16, 0, 3, true);
							if (itemChest[num7].quantity > 1)
							{
								mFont.smallFontYellow.drawString(g, itemChest[num7].quantity + string.Empty, num4 + 22, num5 + i * num3 + 30, 2, mFont.tahoma_7);
							}
						}
					}
					num4 += num3;
				}
			}
			cmdChest.paint(g);
		}

		public override void paint(mGraphics g)
		{
			g.setColor(0);
			g.fillRect(0, 0, CScreen.w, CScreen.h, false);
			BackgroundNew.paintBGGameScr(g);
			if (!CCanvas.panel.isShow)
			{
				CCanvas.paintInfoPopup(g);
			}
			Camera.translate(g);
			if (map != null)
			{
				map.paintTilemap(g);
			}
			if (!BackgroundNew.lowGraphic && mapItem != null)
			{
				mapItem.paintMapItem(g);
			}
			if (vNpc.size() > 0)
			{
				for (int i = 0; i < vNpc.size(); i++)
				{
					Npc npc = (Npc)vNpc.elementAt(i);
					if (npc != null)
					{
						npc.paint(g);
					}
				}
			}
			for (int j = 0; j < shots.Length; j++)
			{
				if (shots[j] != null)
				{
					shots[j].paint(g);
				}
			}
			pm.paint2(g);
			if (PM.getMyPlayer2() != null && PM.getMyPlayer2().isPointAvenger)
			{
				g.drawImage(GameScr.imgPointer[iFramePointer % 2], PM.getMyPlayer2().xToNow, PM.getMyPlayer2().yToNow, 3, false);
			}
			for (int k = 0; k < CCanvas.currentPopup.size(); k++)
			{
				Popup popup = (Popup)CCanvas.currentPopup.elementAt(k);
				if (popup != InfoPopup.gI())
				{
					((Popup)CCanvas.currentPopup.elementAt(k)).paint(g);
				}
			}
			EffecMn.paintLayer3(g);
			GameScr.sm.paint(g);
			g.translate(-g.getTranslateX(), -g.getTranslateY());
			g.setClip(0, 0, CScreen.w, CScreen.h);
			paintInfo(g);
			if (CCanvas.isTouch)
			{
				paintTouch(g, (CScreen.w >> 1) + 70, CScreen.h - 25);
			}
			else if (pm.isYourTurn())
			{
				g.drawRegion(GameScr.list1, 0, 0, 35, 20, 1, 0, CScreen.h - 20, 0, false);
			}
			if (CCanvas.isTouch && curFocus != -1 && Camera.mode != 0)
			{
				g.drawImage(GameScr.focus, GameScr.focusCoor[curFocus][0], GameScr.focusCoor[curFocus][1], 3, false);
			}
			if (CCanvas.panel.nMail > 0 && !CCanvas.panel.isShow)
			{
				g.drawImage(PrepareScr.iconChat, CCanvas.w - 5, 25, mGraphics.TOP | mGraphics.RIGHT, false);
				mFont.tahoma_7b_white.drawString(g, CCanvas.panel.nMail + string.Empty, CCanvas.w - 28, 27, 1, mFont.tahoma_7b_dark);
			}
			if (isChestScreen)
			{
				paintChest(g);
			}
		}

		public void showChat(int fromID, string text)
		{
			if (fromID != -1)
			{
				CPlayer playerByIndex = PM.getPlayerByIndex2(fromID);
				if (playerByIndex != null)
				{
					ChatPopup chatPopup = new ChatPopup();
					chatPopup.show2(90, playerByIndex, text);
				}
			}
			if (chatDelay == 0)
			{
				chatDelay = MAX_CHAT_DELAY;
			}
		}

		public static void updateChat()
		{
			if (chatDelay > 0)
			{
				chatDelay--;
			}
		}

		public void doChat()
		{
			string text = CCanvas.inputDlg.tfInput.getText();
			if (!text.Equals(string.Empty))
			{
				GameService.gI().chatRPG(text);
			}
			if (text.Equals("dis"))
			{
				Session_ME.gI().close();
			}
			CCanvas.endDlg();
		}

		public new void keyPressed(int keyCode)
		{
			if (!CCanvas.menu.showMenu && !CCanvas.panel.isShow && keyCode != 10 && CCanvas.currentDialog == null && Camera.mode != 0 && Camera.mode != 0 && CCanvas.currentDialog == null && !PrepareScr.keyForPlay(keyCode))
			{
				CCanvas.inputDlg.tfInput.isFocus = true;
				CCanvas.inputDlg.setInfo("Chat", new Command(L.ok(), this, 1, null), new Command(L.close(), this, 2, null), TField.INPUT_TYPE_ANY);
				CCanvas.inputDlg.show();
				CCanvas.inputDlg.keyPress(keyCode);
				if (CCanvas.isTouch && mGraphics.zoomLevel > 1)
				{
					CCanvas.inputDlg.tfInput.doChangeToTextBox(false);
				}
			}
			base.keyPressed(keyCode);
		}

		public new void show()
		{
			PrepareScr.exitFromPrepareScr = false;
			CCanvas.endDlg();
			if (PM.getMyPlayer2().look == 2)
			{
				Camera.x = PM.getMyPlayer2().x - 40 - CCanvas.hw;
			}
			else
			{
				Camera.x = PM.getMyPlayer2().x + 40 - CCanvas.hw;
			}
			Camera.y = PM.getMyPlayer2().y - CCanvas.hh - ((CCanvas.h < 200) ? 40 : 0);
			Camera.setPlayerModeRPG();
			base.show();
			MM.unload();
			if (CCanvas.gameScr != null)
			{
				CCanvas.gameScr.clearObject();
				CCanvas.gameScr.unload();
			}
			SmallImage.unload();
		}

		public void giaodich(int playerID)
		{
			CPlayer playerByIDDB = PM.getPlayerByIDDB2(playerID);
			if (playerByIDDB != null)
			{
				CCanvas.startYesNoDlg(playerByIDDB.name + L.want_to_trade, new Command(L.yes(), this, 11114, playerByIDDB), new Command(L.no(), this, 4, playerByIDDB));
			}
		}

		public void perform(int idAction, object p)
		{
			if (idAction == 1)
			{
				doChat();
			}
			if (idAction == 2)
			{
				CPlayer cPlayer = (CPlayer)p;
				int indexByIDDB = PM.getIndexByIDDB2(cPlayer.IDDB);
				if (indexByIDDB != -1)
				{
					CCanvas.startYesNoDlg("Người chơi " + cPlayer.name + " muốn thách đấu với bạn", new Command(L.yes(), this, 3, null), new Command(L.no(), this, 4, null));
				}
			}
			if (idAction == 3)
			{
				GameService.gI().invite_ok();
				CCanvas.endDlg();
			}
			if (idAction == 4)
			{
				CCanvas.endDlg();
			}
			if (idAction == 5)
			{
				CCanvas.panel.hide();
				CCanvas.endDlg();
			}
			if (idAction == 6)
			{
				CPlayer cPlayer2 = (CPlayer)p;
				if (cPlayer2 != null)
				{
					GameService.gI().giaodich(0, cPlayer2.IDDB, -1, -1);
				}
				CCanvas.endDlg();
			}
			if (idAction == 7)
			{
				CPlayer cPlayer3 = (CPlayer)p;
				int indexByIDDB2 = PM.getIndexByIDDB2(cPlayer3.IDDB);
				if (indexByIDDB2 != -1)
				{
					GameService.gI().invite((sbyte)indexByIDDB2);
				}
				CCanvas.endDlg();
			}
			if (idAction == 11113)
			{
				CPlayer cPlayer4 = (CPlayer)p;
				if (cPlayer4 != null)
				{
					GameService.gI().giaodich(0, cPlayer4.IDDB, -1, -1);
				}
			}
			if (idAction == 11114)
			{
				CCanvas.endDlg();
				CPlayer cPlayer5 = (CPlayer)p;
				if (cPlayer5 == null)
				{
					return;
				}
				GameService.gI().giaodich(1, cPlayer5.IDDB, -1, -1);
			}
			if (idAction == 8)
			{
				GameService.gI().confirmMenu((sbyte)(CCanvas.menu.menuSelectedItem - 1));
				ChatPopupNR.currChatPopup = null;
			}
			if (idAction == 9)
			{
				CPlayer cPlayer6 = (CPlayer)p;
				GameService.gI().viewPlayerInfo(cPlayer6.IDDB);
			}
			if (idAction == 10)
			{
				CPlayer cPlayer7 = (CPlayer)p;
				if (cPlayer7 != null)
				{
					GameService.gI().friend(1, cPlayer7.IDDB);
				}
				InfoDlg.showWait();
			}
			if (idAction == 11)
			{
				ChatPopupNR.currChatPopup = null;
				InfoDlg.showWait();
				GameService.gI().getShopItem();
			}
			if (idAction == 12)
			{
				ChatPopupNR.currChatPopup = null;
				InfoDlg.showWait();
				GameService.gI().getShopEquip();
			}
			if (idAction == 13)
			{
				ChatPopupNR.currChatPopup = null;
				if (MenuScr.subMenuString[MenuScr.MENU_CUAHANG].Length == 4)
				{
					MenuScr.gI().doChargeMoney(0);
				}
			}
			if (idAction == 14)
			{
				ChatPopupNR.currChatPopup = null;
				if (MenuScr.subMenuString[MenuScr.MENU_CUAHANG].Length == 4)
				{
					MenuScr.gI().doChargeMoney(1);
				}
			}
			if (idAction == 15)
			{
				ChatPopupNR.currChatPopup = null;
				CCanvas.panel.setTypeClan();
				CCanvas.panel.show();
			}
			if (idAction == 16)
			{
				ChatPopupNR.currChatPopup = null;
				MenuScr.gI().doShowFriend();
			}
			if (idAction == 17)
			{
				ChatPopupNR.currChatPopup = null;
				GameService.gI().top(-1);
				InfoDlg.showWait();
			}
			if (idAction == 18)
			{
				ChatPopupNR.currChatPopup = null;
				GameService.gI().viewAchievement();
				InfoDlg.showWait();
			}
			if (idAction == 19)
			{
				MyVector myVector = new MyVector("menu");
				myVector.addElement(new Command(L.emptyRoom(), this, 23, null));
				myVector.addElement(new Command("1 VS 1", this, 23, null));
				myVector.addElement(new Command("2 VS 2", this, 23, null));
				myVector.addElement(new Command("3 VS 3", this, 23, null));
				myVector.addElement(new Command("4 VS 4", this, 23, null));
				CCanvas.menu.startAt(myVector, 0);
			}
			if (idAction == 20)
			{
				ChatPopupNR.currChatPopup = null;
				if (checkItemBroken())
				{
					CCanvas.startOKDlg("Cần sửa trang bị trước khi chơi.");
				}
				else if (PM.getMyPlayer2().isAvenger != 0 && MyMidlet.myInfo.avenger <= 0)
				{
					CCanvas.startOKDlg("Năng lượng avenger đã cạn");
				}
				else
				{
					WaitingScr.gI().show();
					PrepareScr.exitFromPrepareScr = false;
					MenuScr.isTraining = true;
					GameScr.trainingMode = true;
					GameScr.chatList.removeAllElements();
					CCanvas.startWaitDlg(L.pleaseWait());
					GameService.gI().training();
				}
			}
			if (idAction == 21)
			{
				ChatPopupNR.currChatPopup = null;
				MenuScr.viewInfo = true;
				GameService.gI().charactorUpgrade();
				InfoDlg.showWait();
			}
			if (idAction == 22)
			{
				ChatPopupNR.currChatPopup = null;
				MenuScr.gI().doKhamnam();
			}
			if (idAction == 23)
			{
				CCanvas.panel.hide();
				CCanvas.endDlg();
				ChatPopupNR.currChatPopup = null;
				if (checkItemBroken())
				{
					CCanvas.startOKDlg("Cần sửa trang bị trước khi chơi.");
				}
				else if (PM.getMyPlayer2().isAvenger != 0 && MyMidlet.myInfo.avenger <= 0)
				{
					CCanvas.startOKDlg("Năng lượng avenger đã cạn");
				}
				else
				{
					int menuSelectedItem = CCanvas.menu.menuSelectedItem;
					if (menuSelectedItem == 0)
					{
						MM.mapImages.removeAllElements();
						GameService.gI().requestEmptyRoom(0, -1, null);
						InfoDlg.showWait();
					}
					else
					{
						MM.maps.removeAllElements();
						MM.mapImages.removeAllElements();
						MenuScr.gI().doPlayNow((sbyte)menuSelectedItem);
					}
				}
			}
			if (idAction == 26)
			{
				MenuScr.gI().goToGame();
			}
			if (idAction == 27)
			{
				mSystem.disableSound = !mSystem.disableSound;
				if (mSystem.disableSound)
				{
					Music.stopAll();
				}
				CRes.saveRMSInt("dissound", mSystem.disableSound ? 1 : 0);
			}
			if (idAction == 28)
			{
				bool flag = Rms.loadRMSInt("graphic") == 1;
				flag = !flag;
				if (flag)
				{
					CRes.saveRMSInt("effect", 1);
				}
				CRes.saveRMSInt("graphic", flag ? 1 : 0);
				CCanvas.startOKDlg("Vui lòng khởi động lại game để thay đổi cấu hình", new Command(L.ok(), this, 29, null));
			}
			if (idAction == 29)
			{
				Main.exit();
			}
			if (idAction == 30)
			{
				ChatPopupNR.currChatPopup = null;
				GameService.gI().repairAllItem();
			}
			if (idAction == 31)
			{
				ChatPopupNR.currChatPopup = null;
				Session_ME.gI().close();
				LoginScr.gI().actRegister();
				LoginScr.gI().show();
			}
			if (idAction == 32)
			{
				isChestScreen = false;
				center = null;
			}
			if (idAction == 33)
			{
				isOpenChest[focusChest] = 1;
				GameService.gI().openChest();
				InfoDlg.showWait();
				lockInput = true;
				if (!CCanvas.isTouch)
				{
					cmdChest = new Command("Đóng", this, 32, null);
					cmdChest.isFocus = true;
					cmdChest.x = CCanvas.w / 2 - CScreen.cmdW / 2;
					int num = 3;
					int num2 = 32;
					int num3 = CScreen.h - num2 * num >> 1;
					cmdChest.y = num3 + 110;
				}
				center = cmdChest;
			}
		}

		public static bool checkItemBroken()
		{
			for (int i = 0; i < CPlayer.arrItemBody.Length; i++)
			{
				if (CPlayer.arrItemBody[i] != null && CPlayer.arrItemBody[i].HP <= 0)
				{
					return true;
				}
			}
			return false;
		}
	}
}
