using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x020000C9 RID: 201
public class Pin : MonoBehaviour, IPointerEnterHandler, IEventSystemHandler, IPointerExitHandler, IPointerDownHandler
{
	// Token: 0x06001291 RID: 4753 RVA: 0x00167ADC File Offset: 0x00165CDC
	private void Awake()
	{
		this.tr = base.transform;
		this.btn = base.gameObject.GetComponent<Button>();
		this.rt = base.gameObject.GetComponent<RectTransform>();
		this.timer2 = -1f;
		this.startPosition = this.rt.anchoredPosition;
		this.cb = this.btn.colors;
	}

	// Token: 0x06001292 RID: 4754 RVA: 0x00167B49 File Offset: 0x00165D49
	public void GetStartPosition()
	{
		this.rt = base.gameObject.GetComponent<RectTransform>();
		this.startPosition = this.rt.anchoredPosition;
	}

	// Token: 0x06001293 RID: 4755 RVA: 0x00167B74 File Offset: 0x00165D74
	private void OnDisable()
	{
		if (this.dragging)
		{
			this.dragging = false;
		}
		this.img.raycastTarget = true;
		this.timer2 = -1f;
		this.btn.interactable = true;
		this.btn.colors = this.cb;
	}

	// Token: 0x06001294 RID: 4756 RVA: 0x00167BC4 File Offset: 0x00165DC4
	public void SetPos(Vector3 pos)
	{
		if (!this.tr)
		{
			this.tr = base.transform;
		}
		this.tr.position = pos;
	}

	// Token: 0x06001295 RID: 4757 RVA: 0x00167BEB File Offset: 0x00165DEB
	public void FromLoad(Vector3 pos, string noteText)
	{
		this.tr.position = pos;
		this.note = noteText;
		Links.x.sceneMap.SetPinImage(this);
	}

	// Token: 0x06001296 RID: 4758 RVA: 0x00167C10 File Offset: 0x00165E10
	public void OnPointerEnter(PointerEventData eventData)
	{
		if (!this.dragging)
		{
			Links.x.sceneMap.StartPinHover(this.note, this);
		}
		this.hovering = true;
		this.img.material = Links.x.sceneMap.hoverMaterial;
	}

	// Token: 0x06001297 RID: 4759 RVA: 0x00167C5C File Offset: 0x00165E5C
	public void OnPointerExit(PointerEventData eventData)
	{
		Links.x.sceneMap.EndPinHover(this);
		this.hovering = false;
		this.img.material = Links.x.sceneMap.normalMaterial;
	}

	// Token: 0x06001298 RID: 4760 RVA: 0x00167C90 File Offset: 0x00165E90
	public void OnPointerDown(PointerEventData eventData)
	{
		if (!this.noDrag)
		{
			if (Links.x.sceneMap.pinEditBox.activeSelf)
			{
				Links.x.sceneMap.ClosePinEditBox();
			}
			this.dragging = true;
			Links.x.sceneMap.dragging = true;
			Links.x.sceneMap.currPin = this;
			Links.x.sceneMap.StartDrag();
			if (Links.x.joy)
			{
				Links.x.sceneMap.EndPinHover(this);
			}
			this.startDragPosition = this.tr.position;
			this.startDragPositionRT = this.rt.anchoredPosition3D;
			this.offset = this.tr.position - Links.x.menuCamera.ScreenToWorldPoint(Input.mousePosition);
			this.offset.z = 100f;
			Links.x.hudControl.JoyBox(false);
			ColorBlock colors = this.btn.colors;
			colors.disabledColor = Color.white;
			this.btn.colors = colors;
		}
	}

	// Token: 0x06001299 RID: 4761 RVA: 0x00167DAF File Offset: 0x00165FAF
	public void DragFromTrigger()
	{
		if (!this.dragging)
		{
			this.OnPointerDown(null);
			return;
		}
		this.OnPointerUp();
	}

	// Token: 0x0600129A RID: 4762 RVA: 0x00167DC8 File Offset: 0x00165FC8
	public void PointerUpFromTrigger()
	{
		if (!this.dragging)
		{
			if (Links.x.sceneMap.currPin != this)
			{
				EventSystem.current.SetSelectedGameObject(base.gameObject);
				Links.x.sceneMap.UpdateLastPinOpened(this);
			}
			if (!this.inBox)
			{
				Links.x.sceneMap.OpenPinEditBox(this, this.note);
				return;
			}
		}
		else if (Links.x.joy)
		{
			this.DragFromTrigger();
		}
	}

	// Token: 0x0600129B RID: 4763 RVA: 0x00167E48 File Offset: 0x00166048
	public void OnPointerUp()
	{
		if (!this.noDrag)
		{
			if (this.dragging)
			{
				Vector2 vector = RectTransformUtility.WorldToScreenPoint(Links.x.hudCanvas.worldCamera, base.gameObject.transform.position);
				Links.x.sceneMap.overPinBox = RectTransformUtility.RectangleContainsScreenPoint(Links.x.sceneMap.pinBox, vector, Links.x.hudCanvas.worldCamera);
				if (Links.x.sceneMap.overPinBox)
				{
					this.note = "";
					this.inBox = true;
				}
				else
				{
					this.inBox = false;
				}
				this.dragging = false;
				this.btn.colors = this.cb;
				this.img.raycastTarget = true;
				this.btn.interactable = true;
				Links.x.sceneMap.EndDrag();
				Links.x.sceneMap.SetPinImage(this);
			}
			Links.x.sceneMap.currPin = null;
			Links.x.sceneMap.dragging = false;
			if (Links.x.joy)
			{
				EventSystem.current.SetSelectedGameObject(base.gameObject);
				Links.x.hudControl.SetJoyBox(base.gameObject.GetComponent<UITriggerButton>());
				this.OnPointerEnter(null);
			}
			if (Vector3.Distance(this.rt.anchoredPosition3D, this.startDragPositionRT) < this.threshhold && Links.x.mk)
			{
				this.img.raycastTarget = true;
				this.btn.interactable = true;
				this.tr.position = this.startDragPosition;
				Links.x.sceneMap.OpenPinEditBox(this, this.note);
			}
		}
	}

	// Token: 0x0600129C RID: 4764 RVA: 0x00168004 File Offset: 0x00166204
	private void Update()
	{
		if (Links.x.joy)
		{
			if (base.gameObject == Links.x.gameplay.currentEventObject)
			{
				if (!this.hovering)
				{
					this.OnPointerEnter(null);
				}
			}
			else if (this.hovering && !this.dragging)
			{
				this.OnPointerExit(null);
			}
		}
		if (this.dragging)
		{
			if (Links.x.joy)
			{
				Links.x.hudControl.JoyBox(false);
			}
			if (!Links.x.sceneMap.dragging && this.timer2 == -1f)
			{
				this.timer2 = Time.realtimeSinceStartup;
				if (Links.x.mk)
				{
					this.img.raycastTarget = true;
					this.btn.interactable = true;
				}
			}
			else if (Links.x.sceneMap.dragging)
			{
				if (Links.x.mk)
				{
					Vector3 vector = Links.x.menuCamera.ScreenToWorldPoint(Input.mousePosition) + this.offset;
					this.tr.position = new Vector3(Mathf.Clamp(vector.x, Links.x.sceneMap.bottomLeft.transform.position.x, Links.x.sceneMap.bottomRight.transform.position.x), Mathf.Clamp(vector.y, Links.x.sceneMap.bottomLeft.transform.position.y, Links.x.sceneMap.topRight.transform.position.y), vector.z);
				}
				else
				{
					EventSystem.current.SetSelectedGameObject(base.gameObject);
					float num = Links.x.fellowship.move.magnitude / 2f;
					Vector3 vector2 = new Vector3(Links.x.fellowship.move.x, Links.x.fellowship.move.y, 0f) * num;
					bool flag = true;
					Vector3 vector3 = this.tr.position + vector2;
					if (vector3.x >= Links.x.sceneMap.bottomRight.transform.position.x)
					{
						flag = false;
					}
					if (vector3.x <= Links.x.sceneMap.bottomLeft.transform.position.x)
					{
						flag = false;
					}
					if (vector3.y >= Links.x.sceneMap.topRight.transform.position.y)
					{
						flag = false;
					}
					if (vector3.y <= Links.x.sceneMap.bottomRight.transform.position.y)
					{
						flag = false;
					}
					if (flag)
					{
						this.tr.position += vector2;
					}
				}
			}
			if (Time.realtimeSinceStartup > this.timer2 + 0.15f && !Links.x.sceneMap.dragging && this.timer2 > -1f && Links.x.mk)
			{
				this.dragging = false;
				this.timer2 = -1f;
			}
			if (Links.x.sceneMap.dragging && Links.x.mk && Vector3.Distance(this.tr.position, this.startDragPosition) >= this.threshhold)
			{
				this.img.raycastTarget = false;
				Links.x.sceneMap.EndPinHover(this);
				this.btn.interactable = false;
			}
		}
	}

	// Token: 0x04001EB9 RID: 7865
	public bool dragging;

	// Token: 0x04001EBA RID: 7866
	public string note;

	// Token: 0x04001EBB RID: 7867
	private float timer2;

	// Token: 0x04001EBC RID: 7868
	private Vector3 startDragPosition;

	// Token: 0x04001EBD RID: 7869
	private Vector3 startDragPositionRT;

	// Token: 0x04001EBE RID: 7870
	public Transform tr;

	// Token: 0x04001EBF RID: 7871
	public RectTransform rt;

	// Token: 0x04001EC0 RID: 7872
	private Vector3 offset;

	// Token: 0x04001EC1 RID: 7873
	public Image img;

	// Token: 0x04001EC2 RID: 7874
	private Button btn;

	// Token: 0x04001EC3 RID: 7875
	public float threshhold = 0.05f;

	// Token: 0x04001EC4 RID: 7876
	public bool noDrag;

	// Token: 0x04001EC5 RID: 7877
	private bool hovering;

	// Token: 0x04001EC6 RID: 7878
	public bool forWorldMap;

	// Token: 0x04001EC7 RID: 7879
	public bool inBox;

	// Token: 0x04001EC8 RID: 7880
	public Vector3 startPosition;

	// Token: 0x04001EC9 RID: 7881
	private ColorBlock cb;
}
