using System;

public class Menu
{
	public bool showMenu;

	public MyVector menuItems;

	public int menuSelectedItem;

	public int menuX;

	public int menuY;

	public int menuW;

	public int menuH;

	public static int[] menuTemY;

	public static int cmtoX;

	public static int cmx;

	public static int cmdy;

	public static int cmvy;

	public static int cmxLim;

	public static int xc;

	private Command left = new Command(L.select(), 0);

	private Command right = new Command(L.close(), 0, CCanvas.w - 71, CCanvas.h - CScreen.cmdH + 1);

	private Command center;

	public static Image imgMenu0;

	public static Image imgMenu1;

	private bool disableClose;

	public int tDelay;

	public int w;

	private int pa;

	private bool trans;

	private int pointerDownTime;

	private int pointerDownFirstX;

	private int[] pointerDownLastX = new int[3];

	private bool pointerIsDowning;

	private bool isDownWhenRunning;

	private bool wantUpdateList;

	private int waitToPerform;

	private int cmRun;

	private bool touch;

	private bool close;

	private int cmvx;

	private int cmdx;

	private bool isClose;

	public bool[] isNotClose;

	public static void loadImage()
	{
		imgMenu0 = CCanvas.loadImage("/gui2/menu0.png");
		imgMenu1 = CCanvas.loadImage("/gui2/menu1.png");
	}

	public void startWithoutCloseButton(MyVector menuItems, int pos)
	{
		startAt(menuItems, pos);
		disableClose = true;
	}

	public void startAt(MyVector menuItems, int x, int y)
	{
		startAt(menuItems, 0);
		menuX = x;
		menuY = y;
		while (menuY + menuH > CCanvas.h)
		{
			menuY -= 2;
		}
	}

	public void startAt(MyVector menuItems, int pos)
	{
		if (showMenu)
		{
			return;
		}
		CCanvas.clearAllPointerEvent();
		CCanvas.clearKeyPressed();
		CCanvas.clearKeyHold();
		cmtoX = 0;
		cmx = 0;
		isClose = false;
		touch = false;
		close = false;
		tDelay = 0;
		if (menuItems.size() == 1)
		{
			menuSelectedItem = 0;
			Command command = (Command)menuItems.elementAt(0);
			if (command != null && command.caption.Equals("Camera"))
			{
				command.performAction();
				showMenu = false;
				return;
			}
		}
		isNotClose = new bool[menuItems.size()];
		for (int i = 0; i < isNotClose.Length; i++)
		{
			isNotClose[i] = false;
		}
		disableClose = false;
		if (menuItems.size() != 0)
		{
			this.menuItems = menuItems;
			menuW = 60;
			menuH = 60;
			for (int j = 0; j < menuItems.size(); j++)
			{
				Command command2 = (Command)this.menuItems.elementAt(j);
				command2.isPlaySoundButton = false;
				int width = mFont.tahoma_7b_dark.getWidth(command2.caption);
				command2.subCaption = mFont.tahoma_7b_dark.splitFontArray(command2.caption, menuW - 16);
			}
			menuTemY = new int[menuItems.size()];
			menuX = (CCanvas.w - menuItems.size() * menuW) / 2;
			if (menuX < 1)
			{
				menuX = 1;
			}
			menuY = CCanvas.h - menuH - (Paint.hTab + 1) - 1;
			if (CCanvas.isTouch)
			{
				menuY -= 3;
			}
			menuY += 27;
			for (int k = 0; k < menuTemY.Length; k++)
			{
				menuTemY[k] = CCanvas.h;
			}
			showMenu = true;
			menuSelectedItem = 0;
			cmxLim = this.menuItems.size() * menuW - CCanvas.w;
			if (cmxLim < 0)
			{
				cmxLim = 0;
			}
			xc = 50;
			w = menuItems.size() * menuW - 1;
			if (w > CCanvas.w - 2)
			{
				w = CCanvas.w - 2;
			}
			if (CCanvas.isTouch)
			{
				menuSelectedItem = -1;
			}
		}
	}

	public bool isScrolling()
	{
		if ((!isClose && menuTemY[menuTemY.Length - 1] > menuY) || (isClose && menuTemY[menuTemY.Length - 1] < CCanvas.h))
		{
			return true;
		}
		return false;
	}

	public void updateMenuKey()
	{
		if (!showMenu || isScrolling())
		{
			return;
		}
		bool flag = false;
		if (CCanvas.keyPressed[2] || CCanvas.keyPressed[4])
		{
			flag = true;
			menuSelectedItem--;
			if (menuSelectedItem < 0)
			{
				menuSelectedItem = menuItems.size() - 1;
			}
		}
		else if (CCanvas.keyPressed[8] || CCanvas.keyPressed[6])
		{
			flag = true;
			menuSelectedItem++;
			if (menuSelectedItem > menuItems.size() - 1)
			{
				menuSelectedItem = 0;
			}
		}
		else if (CCanvas.keyPressed[5])
		{
			if (center != null)
			{
				if (center.idAction > 0)
				{
					if (center.actionListener == CCanvas.gameScr)
					{
						CCanvas.gameScr.actionPerform(center.idAction, center.p);
					}
					else
					{
						perform(center.idAction, center.p);
					}
				}
			}
			else
			{
				waitToPerform = 2;
			}
		}
		else if (CCanvas.keyPressed[12])
		{
			if (isScrolling())
			{
				return;
			}
			if (left.idAction > 0)
			{
				perform(left.idAction, left.p);
			}
			else
			{
				waitToPerform = 2;
			}
		}
		else if (!disableClose && (CCanvas.keyPressed[13] || CScreen.getCmdPointerLast(right)))
		{
			if (isScrolling())
			{
				return;
			}
			if (!close)
			{
				close = true;
			}
			isClose = true;
			ChatPopupNR.currChatPopup = null;
		}
		if (flag)
		{
			cmtoX = menuSelectedItem * menuW + menuW - CCanvas.w / 2;
			if (cmtoX > cmxLim)
			{
				cmtoX = cmxLim;
			}
			if (cmtoX < 0)
			{
				cmtoX = 0;
			}
			if (menuSelectedItem == menuItems.size() - 1 || menuSelectedItem == 0)
			{
				cmx = cmtoX;
			}
		}
		if (!disableClose && CCanvas.isPointerJustRelease && !CCanvas.isPointer(menuX, menuY, w, menuH) && !pointerIsDowning)
		{
			if (!isScrolling())
			{
				pointerDownTime = (pointerDownFirstX = 0);
				pointerIsDowning = false;
				CCanvas.clearAllPointerEvent();
				isClose = true;
				close = true;
				if (ChatPopupNR.currChatPopup != null)
				{
					ChatPopupNR.currChatPopup = null;
				}
			}
			return;
		}
		if (CCanvas.isPointerDown)
		{
			if (!pointerIsDowning && CCanvas.isPointer(menuX, menuY, w, menuH))
			{
				for (int i = 0; i < pointerDownLastX.Length; i++)
				{
					pointerDownLastX[0] = CCanvas.px;
				}
				pointerDownFirstX = CCanvas.px;
				pointerIsDowning = true;
				isDownWhenRunning = cmRun != 0;
				cmRun = 0;
			}
			else if (pointerIsDowning)
			{
				pointerDownTime++;
				if (pointerDownTime > 5 && pointerDownFirstX == CCanvas.px && !isDownWhenRunning)
				{
					pointerDownFirstX = -1000;
					menuSelectedItem = (cmtoX + CCanvas.px - menuX) / menuW;
				}
				int num = CCanvas.px - pointerDownLastX[0];
				if (num != 0 && menuSelectedItem != -1)
				{
					menuSelectedItem = -1;
				}
				for (int num2 = pointerDownLastX.Length - 1; num2 > 0; num2--)
				{
					pointerDownLastX[num2] = pointerDownLastX[num2 - 1];
				}
				pointerDownLastX[0] = CCanvas.px;
				cmtoX -= num;
				if (cmtoX < 0)
				{
					cmtoX = 0;
				}
				if (cmtoX > cmxLim)
				{
					cmtoX = cmxLim;
				}
				if (cmx < 0 || cmx > cmxLim)
				{
					num /= 2;
				}
				cmx -= num;
				if (cmx < -(CCanvas.h / 3))
				{
					wantUpdateList = true;
				}
				else
				{
					wantUpdateList = false;
				}
			}
		}
		if (CCanvas.isPointerJustRelease && pointerIsDowning)
		{
			int i2 = CCanvas.px - pointerDownLastX[0];
			CCanvas.isPointerJustRelease = false;
			if (CRes.abs(i2) < 20 && CRes.abs(CCanvas.px - pointerDownFirstX) < 20 && !isDownWhenRunning)
			{
				cmRun = 0;
				cmtoX = cmx;
				pointerDownFirstX = -1000;
				menuSelectedItem = (cmtoX + CCanvas.px - menuX) / menuW;
				pointerDownTime = 0;
				waitToPerform = 10;
			}
			else if (menuSelectedItem != -1 && pointerDownTime > 5)
			{
				pointerDownTime = 0;
				waitToPerform = 1;
			}
			else if (menuSelectedItem == -1 && !isDownWhenRunning)
			{
				if (cmx < 0)
				{
					cmtoX = 0;
				}
				else if (cmx > cmxLim)
				{
					cmtoX = cmxLim;
				}
				else
				{
					int num3 = CCanvas.px - pointerDownLastX[0] + (pointerDownLastX[0] - pointerDownLastX[1]) + (pointerDownLastX[1] - pointerDownLastX[2]);
					num3 = ((num3 > 10) ? 10 : ((num3 < -10) ? (-10) : 0));
					cmRun = -num3 * 100;
				}
			}
			pointerIsDowning = false;
			pointerDownTime = 0;
			CCanvas.isPointerJustRelease = false;
		}
		CCanvas.clearKeyPressed();
		CCanvas.clearKeyHold();
	}

	public void moveCamera()
	{
		if (cmRun != 0 && !pointerIsDowning)
		{
			cmtoX += cmRun / 100;
			if (cmtoX < 0)
			{
				cmtoX = 0;
			}
			else if (cmtoX > cmxLim)
			{
				cmtoX = cmxLim;
			}
			else
			{
				cmx = cmtoX;
			}
			cmRun = cmRun * 9 / 10;
			if (cmRun < 100 && cmRun > -100)
			{
				cmRun = 0;
			}
		}
		if (cmx != cmtoX && !pointerIsDowning)
		{
			cmvx = cmtoX - cmx << 2;
			cmdx += cmvx;
			cmx += cmdx >> 4;
			cmdx &= 15;
		}
	}

	public void paintMenu(mGraphics g)
	{
		CCanvas.resetTrans(g);
		g.translate(-cmx, 0);
		for (int i = 0; i < menuItems.size(); i++)
		{
			if (i == menuSelectedItem)
			{
				g.drawImage(imgMenu1, menuX + i * menuW + 1, menuTemY[i] + 1, 0, false);
			}
			else
			{
				g.drawImage(imgMenu0, menuX + i * menuW + 1, menuTemY[i] + 1, 0, false);
			}
			string[] array = ((Command)menuItems.elementAt(i)).subCaption;
			if (array == null)
			{
				array = new string[1] { ((Command)menuItems.elementAt(i)).caption };
			}
			int num = menuTemY[i] + (menuH - array.Length * 14 >> 1) + 1;
			for (int j = 0; j < array.Length; j++)
			{
				mFont.tahoma_7b_dark.drawString2(g, array[j], menuX + i * menuW + menuW / 2, num + j * 14, 2);
			}
		}
		g.translate(-g.getTranslateX(), -g.getTranslateY());
	}

	public void doCloseMenu()
	{
		isClose = false;
		showMenu = false;
		CCanvas.panel.cp = null;
		if (CCanvas.panel2 != null && CCanvas.panel2.cp != null)
		{
			CCanvas.panel2.cp = null;
		}
		if (!close && touch && menuSelectedItem >= 0)
		{
			Command command = (Command)menuItems.elementAt(menuSelectedItem);
			if (command != null)
			{
				command.performAction();
			}
		}
	}

	public void performSelect()
	{
		if (menuSelectedItem >= 0)
		{
			Command command = (Command)menuItems.elementAt(menuSelectedItem);
			if (command != null)
			{
				command.performAction();
			}
		}
	}

	public void updateMenu()
	{
		try
		{
			moveCamera();
			if (!isClose)
			{
				tDelay++;
				for (int i = 0; i < menuTemY.Length; i++)
				{
					if (menuTemY[i] > menuY)
					{
						int num = menuTemY[i] - menuY >> 1;
						if (num < 1)
						{
							num = 1;
						}
						if (tDelay > i)
						{
							menuTemY[i] -= num;
						}
					}
				}
				if (menuTemY[menuTemY.Length - 1] <= menuY)
				{
					tDelay = 0;
				}
			}
			else
			{
				tDelay++;
				for (int j = 0; j < menuTemY.Length; j++)
				{
					if (menuTemY[j] < CCanvas.h)
					{
						int num2 = (CCanvas.h - menuTemY[j] >> 1) + 2;
						if (num2 < 1)
						{
							num2 = 1;
						}
						if (tDelay > j)
						{
							menuTemY[j] += num2;
						}
					}
				}
				if (menuTemY[menuTemY.Length - 1] >= CCanvas.h)
				{
					tDelay = 0;
					doCloseMenu();
				}
			}
			if (xc != 0)
			{
				xc >>= 1;
				if (xc < 0)
				{
					xc = 0;
				}
			}
			if (isScrolling() || waitToPerform <= 0)
			{
				return;
			}
			waitToPerform--;
			if (waitToPerform == 0)
			{
				if (!isNotClose[menuSelectedItem])
				{
					isClose = true;
					touch = true;
				}
				else
				{
					performSelect();
				}
			}
		}
		catch (Exception)
		{
		}
	}

	public void perform(int idAction, object p)
	{
	}
}
