using System;
using System.Drawing;
using OpenTK;
using OpenTK.Input;

namespace SharpQuake
{
	internal static class input
	{
		public static bool IsMouseActive
		{
			get
			{
				return input._IsMouseActive;
			}
		}

		public static Point WindowCenter
		{
			get
			{
				Rectangle bounds = mainwindow.Instance.Bounds;
				Point location = bounds.Location;
				location.Offset(bounds.Width / 2, bounds.Height / 2);
				return location;
			}
		}

		public static void Init()
		{
			if (input._MouseFilter == null)
			{
				input._MouseFilter = new cvar("m_filter", "0");
			}
			input._IsMouseActive = Mouse.GetState(0).IsConnected;
			if (input._IsMouseActive)
			{
				input._MouseButtons = 3;
			}
		}

		public static void Shutdown()
		{
			input.DeactivateMouse();
			input.ShowMouse();
		}

		public static void Commands()
		{
		}

		public static void ActivateMouse()
		{
			input._MouseActivateToggle = true;
			if (Mouse.GetState(0).IsConnected)
			{
				Mouse.SetPosition((double)input.WindowCenter.X, (double)input.WindowCenter.Y);
				input._IsMouseActive = true;
			}
		}

		public static void DeactivateMouse()
		{
			input._MouseActivateToggle = false;
			input._IsMouseActive = false;
		}

		public static void HideMouse()
		{
			if (input._MouseShowToggle)
			{
				input._MouseShowToggle = false;
			}
		}

		public static void ShowMouse()
		{
			if (!input._MouseShowToggle)
			{
				bool isFullscreen = mainwindow.IsFullscreen;
				input._MouseShowToggle = true;
			}
		}

		public static void Move(usercmd_t cmd)
		{
			if (!mainwindow.Instance.Focused)
			{
				return;
			}
			if (mainwindow.Instance.WindowState == WindowState.Minimized)
			{
				return;
			}
			input.MouseMove(cmd);
		}

		public static void ClearStates()
		{
			if (input._IsMouseActive)
			{
				input._MouseAccum = Vector2.Zero;
				input._MouseOldButtonState = 0;
			}
		}

		public static void MouseEvent(int mstate)
		{
			if (input._IsMouseActive)
			{
				for (int i = 0; i < input._MouseButtons; i++)
				{
					if ((mstate & 1 << i) != 0 && (input._MouseOldButtonState & 1 << i) == 0)
					{
						Key.Event(200 + i, true);
					}
					if ((mstate & 1 << i) == 0 && (input._MouseOldButtonState & 1 << i) != 0)
					{
						Key.Event(200 + i, false);
					}
				}
				input._MouseOldButtonState = mstate;
			}
		}

		private static void MouseMove(usercmd_t cmd)
		{
			if (!input._IsMouseActive)
			{
				return;
			}
			Point point = new Point(Mouse.GetCursorState().X, Mouse.GetCursorState().Y);
			Point windowCenter = input.WindowCenter;
			int num = (int)((float)(point.X - windowCenter.X) + input._MouseAccum.X);
			int num2 = (int)((float)(point.Y - windowCenter.Y) + input._MouseAccum.Y);
			input._MouseAccum.X = 0f;
			input._MouseAccum.Y = 0f;
			if (input._MouseFilter.Value != 0f)
			{
				input._Mouse.X = ((float)num + input._OldMouse.X) * 0.5f;
				input._Mouse.Y = ((float)num2 + input._OldMouse.Y) * 0.5f;
			}
			else
			{
				input._Mouse.X = (float)num;
				input._Mouse.Y = (float)num2;
			}
			input._OldMouse.X = (float)num;
			input._OldMouse.Y = (float)num2;
			input._Mouse *= client.Sensitivity;
			if (client_input.StrafeBtn.IsDown || (client.LookStrafe && client_input.MLookBtn.IsDown))
			{
				cmd.sidemove += client.MSide * input._Mouse.X;
			}
			else
			{
				client_state_t cl = client.cl;
				cl.viewangles.Y = cl.viewangles.Y - client.MYaw * input._Mouse.X;
			}
			view.StopPitchDrift();
			client_state_t cl2 = client.cl;
			cl2.viewangles.X = cl2.viewangles.X + client.MPitch * input._Mouse.Y;
			client.cl.viewangles.X = MathHelper.Clamp(client.cl.viewangles.X, -70f, 80f);
			if (num != 0 || num2 != 0)
			{
				Mouse.SetPosition((double)windowCenter.X, (double)windowCenter.Y);
			}
		}

		private static cvar _MouseFilter;

		private static Vector2 _OldMouse;

		private static Vector2 _Mouse;

		private static Vector2 _MouseAccum;

		private static bool _IsMouseActive;

		private static int _MouseButtons;

		private static int _MouseOldButtonState;

		private static bool _MouseActivateToggle;

		private static bool _MouseShowToggle = true;
	}
}
