using System;

public class ChatPopupNR : Effect2, IActionListener
{
	public int sayWidth = 100;

	public int delay;

	public int sayRun;

	public string[] says;

	public int cx;

	public int cy;

	public int ch;

	public int cmx;

	public int cmy;

	private bool outSide;

	private int currentLine;

	private string[] lines;

	public Command cmdNextLine;

	public Command cmdMsg1;

	public Command cmdMsg2;

	public static ChatPopupNR currChatPopup;

	public static ChatPopupNR serverChatPopUp;

	public static string nextMultiChatPopUp;

	public bool isShopDetail;

	public static Scroll scr;

	public static bool isHavePetNpc;

	public int mH;

	public static int performDelay;

	public static int face;

	public int dx;

	public int dy;

	public Item item;

	public int strY;

	public static void addChatPopup(string chat, int howLong, int x, int y)
	{
		ChatPopupNR chatPopupNR = (currChatPopup = new ChatPopupNR());
		scr = null;
		chatPopupNR.sayWidth = CCanvas.w - 30 - (CCanvas.menu.showMenu ? CCanvas.menu.menuX : 0);
		if (chatPopupNR.sayWidth > 320)
		{
			chatPopupNR.sayWidth = 320;
		}
		if (chat.Length < 10)
		{
			chatPopupNR.sayWidth = 60;
		}
		if (CCanvas.w == 128)
		{
			chatPopupNR.sayWidth = 128;
		}
		chatPopupNR.says = mFont.tahoma_7b_red.splitFontArray(chat, chatPopupNR.sayWidth - 10);
		chatPopupNR.delay = howLong;
		chatPopupNR.ch = 15 - chatPopupNR.sayRun + chatPopupNR.says.Length * 12 + 10;
		Effect2.vEffect2.addElement(chatPopupNR);
		face = -1;
	}

	public static void addChatPopup(int face1, string chat, int howLong, int x, int y)
	{
		addChatPopup(chat, howLong, x, y);
		face = face1;
	}

	public override void update()
	{
		if (scr != null)
		{
			scr.updatecm();
		}
		else if (CCanvas.menu.showMenu)
		{
			strY = 0;
			cx = CCanvas.w / 2 - sayWidth / 2 - 1;
			cy = CCanvas.menu.menuY - ch;
		}
		else
		{
			strY = 0;
			if (CCanvas.curScr.right != null || CCanvas.curScr.left != null || CCanvas.curScr.center != null || cmdNextLine != null || cmdMsg1 != null)
			{
				strY = 5;
				cx = CCanvas.w / 2 - sayWidth / 2 - 1;
				cy = CCanvas.h - 20 - ch;
			}
			else
			{
				cx = CCanvas.w / 2 - sayWidth / 2 - 1;
				cy = CCanvas.h - 5 - ch;
			}
		}
		if (delay > 0)
		{
			delay--;
		}
		if (performDelay > 0)
		{
			performDelay--;
		}
		if (sayRun > 1)
		{
			sayRun--;
		}
	}

	public override void paint(mGraphics g)
	{
		CCanvas.resetTrans(g);
		int num = cx;
		int num2 = cy;
		int num3 = sayWidth + 2;
		int num4 = ch;
		if (num <= 0 || num2 <= 0)
		{
			return;
		}
		Panel.paintPopUp(g, num, num2, num3, num4, 16777215, false);
		if (face != -1)
		{
			SmallImage.drawSmallImage(g, face, cx + 14, cy, 0, CRes.BOTTOM_LEFT, false);
		}
		if (item != null)
		{
			Item.DrawItem(g, item.type, cx + 18, cy);
		}
		if (scr != null)
		{
			g.setClip(num, num2, num3, num4 - 16);
			g.translate(0, -scr.cmy);
		}
		int num5 = -1;
		if (says == null)
		{
			return;
		}
		for (int i = 0; i < says.Length; i++)
		{
			if (says[i].StartsWith("--"))
			{
				g.setColor(0);
				g.fillRect(num + 10, cy + sayRun + i * 12 + 6, num3 - 20, 1, false);
				continue;
			}
			mFont mFont2 = mFont.tahoma_7;
			int num6 = 2;
			string st = says[i];
			int num7 = 0;
			if (says[i].StartsWith("|"))
			{
				string[] array = CRes.split(says[i], "|", 0);
				if (array.Length == 3)
				{
					st = array[2];
				}
				if (array.Length == 4)
				{
					st = array[3];
					num6 = int.Parse(array[2]);
				}
				num7 = int.Parse(array[1]);
				num5 = num7;
			}
			else
			{
				num7 = num5;
			}
			switch (num7)
			{
			case -1:
				mFont2 = mFont.tahoma_7;
				break;
			case 0:
				mFont2 = mFont.tahoma_7b_dark;
				break;
			case 1:
				mFont2 = mFont.tahoma_7b_green;
				break;
			case 2:
				mFont2 = mFont.tahoma_7b_blue;
				break;
			case 3:
				mFont2 = mFont.tahoma_7_red;
				break;
			case 4:
				mFont2 = mFont.tahoma_7_green;
				break;
			case 5:
				mFont2 = mFont.tahoma_7_blue;
				break;
			case 7:
				mFont2 = mFont.tahoma_7b_red;
				break;
			}
			if (num6 == 2)
			{
				mFont2.drawString(g, st, cx + sayWidth / 2, cy + sayRun + i * 12 - strY + 12, num6);
			}
			if (num6 == 1)
			{
				mFont2.drawString(g, st, cx + sayWidth - 5, cy + sayRun + i * 12 - strY + 12, num6);
			}
		}
	}

	public void updateKey()
	{
		if (scr != null)
		{
			if (CCanvas.isTouch)
			{
				scr.updateKey();
			}
			if (CCanvas.keyHold[2])
			{
				scr.cmtoY -= 12;
				if (scr.cmtoY < 0)
				{
					scr.cmtoY = 0;
				}
			}
			if (CCanvas.keyHold[8])
			{
				CCanvas.keyPressed[8] = false;
				scr.cmtoY += 12;
				if (scr.cmtoY > scr.cmyLim)
				{
					scr.cmtoY = scr.cmyLim;
				}
			}
		}
		if (CCanvas.keyPressed[5] || CScreen.getCmdPointerLast(CCanvas.curScr.center))
		{
			CCanvas.keyPressed[5] = false;
			CScreen.keyTouch = -1;
			if (cmdNextLine != null)
			{
				cmdNextLine.performAction();
			}
			else if (cmdMsg1 != null)
			{
				cmdMsg1.performAction();
			}
			else if (cmdMsg2 != null)
			{
				cmdMsg2.performAction();
			}
		}
		if (scr == null || !scr.pointerIsDowning)
		{
			if (cmdMsg1 != null && (CCanvas.keyPressed[12] || CCanvas.keyPressed[5] || CScreen.getCmdPointerLast(cmdMsg1)))
			{
				CCanvas.keyPressed[12] = false;
				CCanvas.keyPressed[5] = false;
				CCanvas.isPointerClick = false;
				CCanvas.isPointerJustRelease = false;
				cmdMsg1.performAction();
				CScreen.keyTouch = -1;
			}
			if (cmdMsg2 != null && (CCanvas.keyPressed[13] || CScreen.getCmdPointerLast(cmdMsg2)))
			{
				CCanvas.keyPressed[13] = false;
				CCanvas.isPointerClick = false;
				CCanvas.isPointerJustRelease = false;
				cmdMsg2.performAction();
				CScreen.keyTouch = -1;
			}
		}
	}

	public void paintCmd(mGraphics g)
	{
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(0, 0, CCanvas.w, CCanvas.h);
		if (cmdNextLine != null)
		{
			CCanvas.paintz.paintCmdBar(g, null, cmdNextLine, null);
		}
		if (cmdMsg1 != null)
		{
			CCanvas.paintz.paintCmdBar(g, cmdMsg1, null, cmdMsg2);
		}
	}

	public void perform(int idAction, object p)
	{
		if (idAction == 1000)
		{
			try
			{
				MyMidlet.instance.platformRequest((string)p);
			}
			catch (Exception ex)
			{
				Cout.LogTryCatch("ChatpopupNR " + ex.StackTrace.ToString());
			}
			MyMidlet.instance.notifyDestroyed();
			CCanvas.endDlg();
		}
		if (idAction == 1001)
		{
			scr = null;
			currChatPopup = null;
		}
	}
}
