using System.Collections.Generic;
using UnityEngine;
using Wizard;

public class InputMgr
{
	public enum TYPE
	{
		NONE,
		PRESS,
		DOWN,
		UP,
		STAY,
		MAX
	}

	public enum LAYER_TYPE
	{
		NONE = 0,
		UI = 32,
		PARTICLE = 256,
		GAME = 512,
		ALL = 65535
	}

	private enum ClickState
	{
		None,
		Down,
		LongPress,
		UpThisFrame,
		DownMoved
	}

	private enum DoubleClickState
	{
		None,
		FirstClickDown,
		SecondClickDown,
		SecondClickUp
	}

	private const float FLICK_SEC = 0.2f;

	private const string CHOICE_BUTTON_TAG = "ChoiceSelectButton";

	private Vector2 m_NonePos = new Vector2(-10000f, -10000f);

	private IList<TYPE> m_NowTypeList = new List<TYPE>();

	private int m_LayerMask;

	private IList<Vector2> m_FirstPosList = new List<Vector2>();

	private IList<Vector2> m_NowPosList = new List<Vector2>();

	private IList<Vector2> m_FixedPosList = new List<Vector2>();

	private IList<Vector2> m_EndPosList = new List<Vector2>();

	private float m_DraggingSec;

	private bool m_IsFlick;

	private IList<GameObject> m_DragEfcList = new List<GameObject>();

	private bool _isInputEnable = true;

	private bool _isBackKeyEnable = true;

	public BattleCamera m_BattleCamera;

	public BackGroundBase m_BackGround;

	private bool _wentOverDrag;

	public static bool MouseControl;

	public static bool KeyboardControl;

	public static bool KeyboardControlSpace;

	public static bool KeyboardControlEvolution;

	public bool KeyboardEnableControlSpace = true;

	public float _lastFirstClick;

	private DoubleClickState _doubleClick;

	private Vector2 _firstClickPosition;

	private const float DOUBLE_CLICK_TIME = 0.5f;

	private float _timeSincePress;

	private Vector2 _downPositionStart;

	private const float LONG_PRESS_TIME = 0.3f;

	private const float LONG_PRESS_THRESHOLD = 100f;

	private ClickState _clickState;

	public const int MOUSE_SHORTCUT_RIGHT_CLICK = 1;

	public const int MOUSE_SHORTCUT_DOUBLE_CLICK = 2;

	private float _KeyboardEmotionWaitTime;

	public bool isBackKeyEnable
	{
		get
		{
			return _isBackKeyEnable;
		}
		set
		{
			_isBackKeyEnable = value;
		}
	}

	public bool WentOverDrag
	{
		get
		{
			return _wentOverDrag;
		}
		set
		{
			_wentOverDrag = value;
		}
	}

	public static bool ShowDetailLeftAndRight => MouseControl;

	public InputMgr()
	{
		m_LayerMask = 0;
		for (int i = 0; i < 1; i++)
		{
			m_NowTypeList.Add(TYPE.NONE);
			m_FirstPosList.Add(m_NonePos);
			m_NowPosList.Add(m_NonePos);
			m_EndPosList.Add(m_NonePos);
			m_FixedPosList.Add(m_NonePos);
			m_DragEfcList.Add(null);
		}
		m_DraggingSec = 0f;
		m_IsFlick = false;
		Input.multiTouchEnabled = false;
		MouseControl = PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.MOUSE_CONTROL);
	}

	~InputMgr()
	{
	}

	public void Update()
	{
		for (int i = 0; i < 1; i++)
		{
			m_NowTypeList[i] = TYPE.NONE;
		}
		if (_isInputEnable)
		{
			MouseClick();
			CheckFlick();
		}
	}

	private void CheckFlick()
	{
		m_IsFlick = false;
		if (IsUp())
		{
			if (m_DraggingSec < 0.2f && 30f < Mathf.Abs(GetVec().x))
			{
				m_IsFlick = true;
			}
			m_DraggingSec = 0f;
		}
		if (IsPress())
		{
			m_DraggingSec += Time.deltaTime;
		}
	}

	public void SetInputEnable(bool isEnable)
	{
		_isInputEnable = isEnable;
		StopDragEffect();
	}

	private void StopDragEffect()
	{
		GameMgr.GetIns().GetEffectMgr().FadeStop(m_DragEfcList[0]);
	}

	private void MouseClick()
	{
		m_NowPosList[0] = Input.mousePosition;
		m_FixedPosList[0] = ToolboxGame.UIManager.UIRootLoadingCamera.ScreenToWorldPoint(m_NowPosList[0]);
		if (MouseControl)
		{
			if (_lastFirstClick > 0f && !Input.GetMouseButtonDown(0))
			{
				if (_firstClickPosition != m_NowPosList[0])
				{
					_lastFirstClick = 0f;
					_doubleClick = DoubleClickState.None;
				}
				else
				{
					_lastFirstClick -= Time.deltaTime;
				}
			}
			if (_doubleClick == DoubleClickState.SecondClickUp)
			{
				_doubleClick = DoubleClickState.None;
			}
			else if (_doubleClick == DoubleClickState.SecondClickDown && (_lastFirstClick <= 0f || _firstClickPosition != m_NowPosList[0]))
			{
				_doubleClick = DoubleClickState.None;
			}
			if (_clickState == ClickState.UpThisFrame)
			{
				_clickState = ClickState.None;
			}
			switch (_clickState)
			{
			case ClickState.None:
				if (Input.GetMouseButtonDown(0))
				{
					_timeSincePress = 0f;
					_downPositionStart = m_NowPosList[0];
					_clickState = ClickState.Down;
					if (Input.GetMouseButtonUp(0))
					{
						_clickState = ClickState.UpThisFrame;
					}
				}
				break;
			case ClickState.Down:
				if (Input.GetMouseButtonUp(0))
				{
					_clickState = ClickState.UpThisFrame;
				}
				else if (Input.GetMouseButton(0))
				{
					if (IsInThreshold())
					{
						_timeSincePress += Time.unscaledDeltaTime;
						if (_timeSincePress >= 0.3f)
						{
							_clickState = ClickState.LongPress;
						}
					}
					else
					{
						_clickState = ClickState.DownMoved;
					}
				}
				else
				{
					_clickState = ClickState.None;
				}
				break;
			case ClickState.LongPress:
				if (!Input.GetMouseButton(0))
				{
					_clickState = ClickState.None;
				}
				else if (!IsInThreshold())
				{
					_clickState = ClickState.DownMoved;
				}
				break;
			case ClickState.DownMoved:
				if (!Input.GetMouseButton(0))
				{
					_clickState = ClickState.None;
				}
				break;
			}
		}
		if (Input.GetMouseButtonDown(0))
		{
			if (MouseControl)
			{
				if (_doubleClick == DoubleClickState.FirstClickDown && _lastFirstClick > 0f)
				{
					_doubleClick = DoubleClickState.SecondClickDown;
					if (Input.GetMouseButtonUp(0))
					{
						_doubleClick = DoubleClickState.SecondClickUp;
					}
				}
				else
				{
					_doubleClick = DoubleClickState.FirstClickDown;
					_lastFirstClick = 0.5f;
					_firstClickPosition = m_NowPosList[0];
					m_FirstPosList[0] = m_NowPosList[0];
					m_NowTypeList[0] = TYPE.DOWN;
					CheckTouchObject(0);
				}
			}
			else
			{
				m_FirstPosList[0] = m_NowPosList[0];
				m_NowTypeList[0] = TYPE.DOWN;
				CheckTouchObject(0);
			}
		}
		else if (Input.GetMouseButtonUp(0))
		{
			m_EndPosList[0] = m_NowPosList[0];
			m_NowTypeList[0] = TYPE.UP;
			if (_doubleClick == DoubleClickState.SecondClickDown)
			{
				_doubleClick = DoubleClickState.SecondClickUp;
				_lastFirstClick = 0f;
			}
		}
		else if (Input.GetMouseButton(0))
		{
			m_NowTypeList[0] = TYPE.PRESS;
			if (m_DragEfcList[0] != null)
			{
				m_DragEfcList[0].transform.position = m_FixedPosList[0];
			}
		}
		else
		{
			StopDragEffect();
		}
		if (m_NowTypeList[0] != TYPE.NONE && IsLayerMask())
		{
			m_NowTypeList[0] = TYPE.NONE;
		}
	}

	public void SetBattleCamera(BattleCamera battleCamera)
	{
		m_BattleCamera = battleCamera;
	}

	public void SetBackGround(BackGroundBase backGround)
	{
		m_BackGround = backGround;
	}

	private void CheckTouchObject(int touchId)
	{
		Ray ray;
		if (!ToolboxGame.UIManager.IsCurrentScene(UIManager.ViewScene.Battle))
		{
			ray = ToolboxGame.UIManager.UIRootLoadingCamera.ScreenPointToRay(m_NowPosList[touchId]);
		}
		else
		{
			if (BattleManagerBase.GetIns() == null || m_BattleCamera == null || m_BattleCamera.Get3DCamera() == null)
			{
				return;
			}
			ray = m_BattleCamera.Get3DCamera().ScreenPointToRay(m_NowPosList[touchId]);
		}
		RaycastHit hitInfo = default(RaycastHit);
		if (ToolboxGame.UIManager.IsCurrentScene(UIManager.ViewScene.Battle))
		{
			bool flag = false;
			bool flag2 = false;
			if (TouchControl.CastRayWithTags(m_NowPosList[touchId], new List<string> { "BattleUI", "ChoiceSelectButton" }))
			{
				flag = true;
			}
			else
			{
				RaycastHit[] array = Physics.RaycastAll(ray.origin, ray.direction, float.PositiveInfinity);
				float num = float.PositiveInfinity;
				float num2 = float.PositiveInfinity;
				Collider collider = null;
				Collider collider2 = null;
				for (int i = 0; i < array.Length; i++)
				{
					float distance = array[i].distance;
					if (distance < num)
					{
						num = distance;
						collider = array[i].collider;
					}
					switch (array[i].collider.tag)
					{
					case "Player":
					case "Enemy":
					case "PlayerToken":
					case "EnemyToken":
					case "DetailPanel":
					case "ClassBtn":
					case "CardHolder":
					case "ECardHolder":
					case "EpPanel":
						flag = true;
						break;
					case "Detail":
					{
						int layer = array[i].collider.gameObject.layer;
						if (UIManager.GetInstance().GetCurrentScene() != UIManager.ViewScene.Battle)
						{
							if (layer == 14)
							{
								flag2 = true;
							}
						}
						else if (layer == 14)
						{
							flag = true;
						}
						break;
					}
					case "Untappable":
						if (distance < num2)
						{
							num2 = distance;
							collider2 = array[i].collider;
						}
						break;
					}
				}
				if (collider == collider2)
				{
					return;
				}
			}
			if (flag2)
			{
				Transform transform = GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_INPUT_TOUCH_2, m_FixedPosList[touchId].x, m_FixedPosList[touchId].y)
					.transform;
				UIManager.GetInstance().SetLayerRecursive(transform, 14);
			}
			else if (flag)
			{
				Transform transform2 = GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_INPUT_TOUCH_2, m_FixedPosList[touchId].x, m_FixedPosList[touchId].y)
					.transform;
				UIManager.GetInstance().SetLayerRecursive(transform2, 22);
			}
		}
		else if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, float.PositiveInfinity))
		{
			Transform transform3 = GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_INPUT_TOUCH_2, m_FixedPosList[touchId].x, m_FixedPosList[touchId].y)
				.transform;
			UIManager.GetInstance().SetLayerRecursive(transform3, 22);
		}
		else
		{
			GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_INPUT_TOUCH_1, m_FixedPosList[touchId].x, m_FixedPosList[touchId].y);
		}
		if (m_DragEfcList[touchId] == null)
		{
			if (GameMgr.GetIns().GetEffectMgr().IsPreInEffectReady)
			{
				m_DragEfcList[touchId] = GameMgr.GetIns().GetEffectMgr().StartTouchEffect(EffectMgr.EffectType.CMN_INPUT_DRAG_1, m_FixedPosList[touchId].x, m_FixedPosList[touchId].y)
					.GetGameObjIns();
			}
		}
		else
		{
			GameMgr.GetIns().GetEffectMgr().FadePlay(m_DragEfcList[touchId], m_FixedPosList[touchId].x, m_FixedPosList[touchId].y);
		}
	}

	private bool IsLayerMask()
	{
		if (m_LayerMask == 0)
		{
			return false;
		}
		if (m_NowTypeList[0] != TYPE.DOWN)
		{
			return false;
		}
		if (Physics2D.GetRayIntersection(ToolboxGame.UIManager.UIRootLoadingCamera.ScreenPointToRay(m_NowPosList[0]), float.PositiveInfinity, m_LayerMask).collider != null)
		{
			return true;
		}
		return false;
	}

	public bool IsNone()
	{
		if (m_NowTypeList[0] == TYPE.NONE)
		{
			return true;
		}
		return false;
	}

	public bool IsPress()
	{
		if (m_NowTypeList[0] == TYPE.PRESS)
		{
			return true;
		}
		return false;
	}

	public bool IsDown()
	{
		if (m_NowTypeList[0] == TYPE.DOWN)
		{
			return true;
		}
		return false;
	}

	public bool IsUp()
	{
		if (m_NowTypeList[0] == TYPE.UP)
		{
			return true;
		}
		return false;
	}

	public bool IsStay()
	{
		if (m_NowTypeList[0] == TYPE.STAY)
		{
			return true;
		}
		return false;
	}

	public void InitLayerMask()
	{
		m_LayerMask = 0;
	}

	public void SetLayerMask(int LayerMask)
	{
		m_LayerMask = LayerMask;
	}

	public Vector2 GetFirstPos()
	{
		return m_FirstPosList[0];
	}

	public Vector2 GetEndPos()
	{
		return m_EndPosList[0];
	}

	public Vector2 GetPos()
	{
		return m_NowPosList[0];
	}

	public Vector2 GetFixedPos()
	{
		if (m_NowTypeList[0] == TYPE.NONE)
		{
			return m_NonePos;
		}
		return m_FixedPosList[0];
	}

	public Vector2 GetVec()
	{
		return m_NowPosList[0] - m_FirstPosList[0];
	}

	public float GetAngle()
	{
		return Mathf.Atan2(m_FirstPosList[0].y - m_NowPosList[0].y, m_FirstPosList[0].x - m_NowPosList[0].x) * 57.29578f + 180f;
	}

	public bool IsFlick()
	{
		return m_IsFlick;
	}

	public Vector2 GetFlickVec()
	{
		return GetVec();
	}

	public bool IsDoubleClick()
	{
		return _doubleClick == DoubleClickState.SecondClickUp;
	}

	public bool IsLongPress()
	{
		return _clickState == ClickState.LongPress;
	}

	public bool IsClick()
	{
		return _clickState == ClickState.UpThisFrame;
	}

	public bool IsDownMoved()
	{
		return _clickState == ClickState.DownMoved;
	}

	private bool IsInThreshold()
	{
		return (_downPositionStart - m_NowPosList[0]).sqrMagnitude <= 100f;
	}

	public bool IsOverDragDistanceHandPlay(Vector2? firstPosition = null)
	{
		if (!firstPosition.HasValue)
		{
			firstPosition = GetFirstPos();
		}
		bool flag = !(GetPos().y - firstPosition.Value.y < 40f);
		_wentOverDrag |= flag;
		return flag;
	}

	public bool IsOverDragDistanceInPlay()
	{
		if (!(Mathf.Abs(GetPos().y - GetFirstPos().y) > 40f))
		{
			return Mathf.Abs(GetPos().x - GetFirstPos().x) > 40f;
		}
		return true;
	}

	public bool IsOverDragDistanceMulligan()
	{
		return Vector2.Distance(GetPos(), GetFirstPos()) > 40f;
	}

	public bool IsKeyboardUpArrow()
	{
		if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardDownArrow()
	{
		if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardRightArrow()
	{
		if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardLeftArrow()
	{
		if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardEnter()
	{
		if (Input.GetKeyDown(KeyCode.Q) || Input.GetKeyDown(KeyCode.Return))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardSpace()
	{
		if (Input.GetKeyDown(KeyCode.Space))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardShift()
	{
		if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardCancel()
	{
		if (Input.GetKeyDown(KeyCode.Backspace) || Input.GetKeyDown(KeyCode.C))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardEvolve()
	{
		if (Input.GetKeyDown(KeyCode.E))
		{
			return true;
		}
		return false;
	}

	public void SetKeyboardEmotionWaitTime(float wait)
	{
		_KeyboardEmotionWaitTime = wait;
	}

	public bool IsKeyboardEmotion()
	{
		if (_KeyboardEmotionWaitTime > 0f)
		{
			_KeyboardEmotionWaitTime -= Time.deltaTime;
			return false;
		}
		if (Input.GetKeyDown(KeyCode.Alpha1))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardBattleLog()
	{
		if (Input.GetKeyDown(KeyCode.B))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardBattleMenu()
	{
		if (Input.GetKeyDown(KeyCode.Escape))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardDeckNextPage()
	{
		if (Input.GetKeyDown(KeyCode.X) || Input.GetKeyDown(KeyCode.Period))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardDeckPrevPage()
	{
		if (Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.Comma))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardClassInfomation()
	{
		if (Input.GetKey(KeyCode.Tab))
		{
			return true;
		}
		return false;
	}

	public bool IsKeyboardAny()
	{
		if (!IsKeyboardUpArrow() && !IsKeyboardDownArrow() && !IsKeyboardRightArrow() && !IsKeyboardLeftArrow() && !IsKeyboardEnter() && !IsKeyboardSpace() && !IsKeyboardShift() && !IsKeyboardCancel() && !IsKeyboardEvolve() && !IsKeyboardEmotion() && !IsKeyboardBattleLog() && !IsKeyboardBattleMenu())
		{
			return IsKeyboardClassInfomation();
		}
		return true;
	}

	public bool IsAnyMouse()
	{
		if (!MouseControl || _clickState == ClickState.None)
		{
			return !IsNone();
		}
		return true;
	}
}
