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

// Token: 0x0200008D RID: 141
public class KeyDrag : MonoBehaviour, IPointerEnterHandler, IEventSystemHandler, IPointerExitHandler
{
	// Token: 0x06000DF1 RID: 3569 RVA: 0x001072F5 File Offset: 0x001054F5
	private void Awake()
	{
		this.GetComponents();
	}

	// Token: 0x06000DF2 RID: 3570 RVA: 0x001072FD File Offset: 0x001054FD
	public void SetBox()
	{
		if (this.savedBox)
		{
			this.SetSavedBox(this.savedBox);
		}
	}

	// Token: 0x06000DF3 RID: 3571 RVA: 0x00107318 File Offset: 0x00105518
	private void Start()
	{
		if (this.savedBox)
		{
			this.SetSavedBox(this.savedBox);
		}
	}

	// Token: 0x06000DF4 RID: 3572 RVA: 0x00107334 File Offset: 0x00105534
	public void GetComponents()
	{
		if (!this.tr)
		{
			this.tr = base.gameObject.transform;
			this.rt = base.gameObject.GetComponent<RectTransform>();
			this.img = base.gameObject.GetComponent<Image>();
			this.button = base.gameObject.GetComponent<Button>();
		}
	}

	// Token: 0x06000DF5 RID: 3573 RVA: 0x00107394 File Offset: 0x00105594
	public void OnPointerEnter(PointerEventData eventData)
	{
		if (!this.keyControl.dragging && !this.dragging)
		{
			Records.x.grabbingSomething = true;
			this.hovering = true;
			this.keyControl.hoverObj = this;
			Links.x.gameplay.uiCanClick = true;
		}
	}

	// Token: 0x06000DF6 RID: 3574 RVA: 0x001073E4 File Offset: 0x001055E4
	public void OnPointerExit(PointerEventData eventData)
	{
		if (!this.keyControl.dragging && !this.dragging)
		{
			Records.x.grabbingSomething = false;
			this.keyControl.hoverObj = null;
			this.hovering = false;
			Links.x.gameplay.uiCanClick = false;
		}
		if (!this.dragging)
		{
			this.hovering = false;
		}
	}

	// Token: 0x06000DF7 RID: 3575 RVA: 0x00107443 File Offset: 0x00105643
	public void SetSavedBox(KeyDrop newBox)
	{
		this.savedBox = newBox;
		this.setting = true;
		this.keyControl.hoverBox = newBox;
		this.OnPointerUp();
	}

	// Token: 0x06000DF8 RID: 3576 RVA: 0x00107468 File Offset: 0x00105668
	public void SetSize(KeyDrop dropBox)
	{
		RectTransform component = dropBox.gameObject.GetComponent<RectTransform>();
		this.rt.sizeDelta = new Vector2(component.sizeDelta.x, component.sizeDelta.y);
		this.rt.anchoredPosition3D = component.anchoredPosition3D;
	}

	// Token: 0x06000DF9 RID: 3577 RVA: 0x001074B8 File Offset: 0x001056B8
	public void OnPointerUp()
	{
		if (this.dragging || this.setting)
		{
			this.GetComponents();
			if ((this.setting || this.keyControl.canDrop) && this.keyControl.hoverBox)
			{
				this.Snap();
				this.img.raycastTarget = true;
				this.button.interactable = true;
				this.savedBox = this.keyControl.hoverBox;
				this.keyControl.SetNewKey(this, this.savedBox);
			}
			else
			{
				this.img.raycastTarget = true;
				this.button.interactable = true;
			}
			RectTransform component = this.savedBox.gameObject.GetComponent<RectTransform>();
			this.rt.sizeDelta = new Vector2(component.sizeDelta.x, component.sizeDelta.y);
			this.rt.anchoredPosition3D = component.anchoredPosition3D;
			if (this.dragging)
			{
				Links.x.gameplay.StoneSoundQuieter();
			}
			this.dragging = false;
			this.keyControl.dragging = false;
			this.keyControl.hoverObj = null;
			this.keyControl.hoverBox = null;
			this.hovering = false;
			this.setting = false;
			Records.x.grabbingSomething = false;
			this.keyControl.EndDrag();
			Links.x.gameplay.uiCanClick = false;
		}
	}

	// Token: 0x06000DFA RID: 3578 RVA: 0x00107620 File Offset: 0x00105820
	public void OnPointerDown()
	{
		if (!this.keyControl.dragging && !this.dragging)
		{
			Links.x.gameplay.uiCanClick = true;
			Links.x.gameplay.StoneSoundQuieter();
			this.dragging = true;
			this.keyControl.StartDrag();
			this.keyControl.dragging = true;
			this.savedPos = this.tr.position;
			this.keyControl.hoverObj = this;
			this.keyControl.hoverBox = null;
			this.img.raycastTarget = false;
			this.button.interactable = false;
			this.offset = this.tr.position - Links.x.menuCamera.ScreenToWorldPoint(Input.mousePosition);
			this.offset.z = 100f;
			base.gameObject.transform.SetAsLastSibling();
		}
	}

	// Token: 0x06000DFB RID: 3579 RVA: 0x00107710 File Offset: 0x00105910
	private void Update()
	{
		if (this.hovering)
		{
			if (Input.GetMouseButtonDown(0))
			{
				this.OnPointerDown();
			}
			if (Input.GetMouseButtonUp(0))
			{
				this.OnPointerUp();
			}
		}
		if (this.dragging)
		{
			Records.x.grabbingSomething = true;
			this.tr.position = Links.x.menuCamera.ScreenToWorldPoint(Input.mousePosition) + this.offset;
			this.Snap();
		}
	}

	// Token: 0x06000DFC RID: 3580 RVA: 0x00107784 File Offset: 0x00105984
	public void Snap()
	{
		if (!this.rt)
		{
			this.rt = base.gameObject.GetComponent<RectTransform>();
		}
		Vector3 anchoredPosition3D = this.rt.anchoredPosition3D;
		anchoredPosition3D = new Vector3((float)Mathf.RoundToInt(anchoredPosition3D.x), (float)Mathf.RoundToInt(anchoredPosition3D.y), (float)Mathf.RoundToInt(anchoredPosition3D.z));
		this.rt.anchoredPosition3D = anchoredPosition3D;
	}

	// Token: 0x04001671 RID: 5745
	public KeyControl keyControl;

	// Token: 0x04001672 RID: 5746
	public bool dragging;

	// Token: 0x04001673 RID: 5747
	public Image img;

	// Token: 0x04001674 RID: 5748
	public Button button;

	// Token: 0x04001675 RID: 5749
	public bool setting;

	// Token: 0x04001676 RID: 5750
	private Transform tr;

	// Token: 0x04001677 RID: 5751
	public KeyDrop savedBox;

	// Token: 0x04001678 RID: 5752
	public string keyFunction;

	// Token: 0x04001679 RID: 5753
	private bool hovering;

	// Token: 0x0400167A RID: 5754
	private Vector3 offset;

	// Token: 0x0400167B RID: 5755
	private RectTransform rt;

	// Token: 0x0400167C RID: 5756
	private Vector3 savedPos;
}
