using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x0200009C RID: 156
public class CompanionIcon : MonoBehaviour, IPointerEnterHandler, IEventSystemHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
{
	// Token: 0x06000E96 RID: 3734 RVA: 0x00116388 File Offset: 0x00114588
	private void Awake()
	{
		this.normal = this.btn.colors.normalColor;
		this.useItemTr = this.useItemsRT.gameObject.transform;
		this.autoCastTr = this.autoCastRT.gameObject.transform;
		this.leaderTr = this.leaderRT.gameObject.transform;
		this.useItemOffset = this.rt.anchoredPosition3D - this.useItemsRT.anchoredPosition3D;
		this.autoCastOffset = this.rt.anchoredPosition3D - this.autoCastRT.anchoredPosition3D;
		this.leaderOffset = this.rt.anchoredPosition3D - this.leaderRT.anchoredPosition3D;
		this.useItems.gameObject.SetActive(false);
		this.autoCast.gameObject.SetActive(false);
		this.leader.gameObject.SetActive(false);
		this.useItemsJoy.gameObject.SetActive(false);
		this.autoCastJoy.gameObject.SetActive(false);
		this.leaderJoy.gameObject.SetActive(false);
		if (this.selected)
		{
			this.selected.enabled = false;
		}
	}

	// Token: 0x06000E97 RID: 3735 RVA: 0x001164D4 File Offset: 0x001146D4
	private void Start()
	{
		if (this.selected)
		{
			this.selected.enabled = false;
		}
		if (!this.tr)
		{
			this.tr = base.transform;
		}
		this.allCompanionIcons = Links.x.formation.companionIcons;
	}

	// Token: 0x06000E98 RID: 3736 RVA: 0x00116528 File Offset: 0x00114728
	public void TurnOff()
	{
		this.character = null;
		this.leader.gameObject.SetActive(false);
		this.leaderJoy.isOn = false;
		this.leaderJoy.gameObject.SetActive(false);
	}

	// Token: 0x06000E99 RID: 3737 RVA: 0x00116560 File Offset: 0x00114760
	public void Setup(Character newCharacter)
	{
		if (this.selected)
		{
			this.selected.enabled = false;
		}
		this.character = newCharacter;
		this.dragging = false;
		Links.x.renderPortraits.AddCharacter(this.character, this.character.stats.xmlName, null, true, null, null, null, this);
		this.updateTime = Time.realtimeSinceStartup;
		this.useItems.isOn = false;
		this.autoCast.isOn = false;
		this.useItems.interactable = true;
		this.autoCast.interactable = true;
		this.useItems.targetGraphic.raycastTarget = true;
		this.autoCast.targetGraphic.raycastTarget = true;
		this.useItems.graphic.raycastTarget = true;
		this.autoCast.graphic.raycastTarget = true;
		this.useItemsJoy.isOn = this.useItems.isOn;
		this.autoCastJoy.isOn = this.autoCast.isOn;
		this.leaderJoy.isOn = this.leader.isOn;
		if (!this.leader.isOn)
		{
			this.leaderJoy.interactable = false;
		}
		this.CheckLeader();
		if (Links.x.mk)
		{
			this.useItems.gameObject.SetActive(false);
			this.autoCast.gameObject.SetActive(true);
			this.leader.gameObject.SetActive(true);
			this.useItemsJoy.gameObject.SetActive(false);
			this.autoCastJoy.gameObject.SetActive(false);
			this.leaderJoy.gameObject.SetActive(false);
			return;
		}
		this.useItems.gameObject.SetActive(false);
		this.autoCast.gameObject.SetActive(false);
		this.leader.gameObject.SetActive(false);
	}

	// Token: 0x06000E9A RID: 3738 RVA: 0x00116748 File Offset: 0x00114948
	public void CheckLeader()
	{
		if (this.character)
		{
			if (Links.x.formation.SharingFormationGroup(this.character))
			{
				if (this.character.stats.isLeader)
				{
					this.leader.interactable = true;
					this.leader.targetGraphic.raycastTarget = true;
					this.leader.graphic.raycastTarget = true;
					this.leader.isOn = true;
					this.leader.gameObject.SetActive(true);
				}
				else if (this.character.stats.defendMainInCombat)
				{
					this.leader.gameObject.SetActive(true);
					this.leader.interactable = true;
					this.leader.targetGraphic.raycastTarget = true;
					this.leader.graphic.raycastTarget = true;
					this.leader.isOn = false;
				}
				else
				{
					this.leader.isOn = false;
					this.leader.gameObject.SetActive(false);
				}
			}
			else
			{
				this.leader.gameObject.SetActive(false);
				this.leaderJoy.gameObject.SetActive(false);
			}
			if (Links.x.joy)
			{
				this.leaderJoy.isOn = this.leader.isOn;
				this.leaderJoy.interactable = this.leader.interactable;
				this.leader.interactable = false;
				if (base.gameObject == Links.x.gameplay.currentEventObject && this.character.stats.defendMainInCombat)
				{
					this.leaderJoy.gameObject.SetActive(true);
				}
			}
		}
	}

	// Token: 0x06000E9B RID: 3739 RVA: 0x00116904 File Offset: 0x00114B04
	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 (Time.realtimeSinceStartup > this.updateTime + 0.15f)
		{
			this.updateTime = Time.realtimeSinceStartup;
			this.companionPortrait.texture = this.texturesIdle;
		}
		if (Links.x.mk && (this.hovering || this.dragging))
		{
			if (Input.GetMouseButtonDown(0))
			{
				this.OnPointerDown(null);
			}
			if (Input.GetMouseButtonUp(0))
			{
				this.OnPointerUp(null);
			}
		}
		if (this.dragging)
		{
			if (Links.x.mk)
			{
				this.tr.position = Links.x.menuCamera.ScreenToWorldPoint(Input.mousePosition) + this.offset;
			}
			else
			{
				EventSystem.current.SetSelectedGameObject(base.gameObject);
				float num = Links.x.fellowship.move.magnitude / 2f;
				Vector3 vector = new Vector3(Links.x.fellowship.move.x, Links.x.fellowship.move.y, 0f) * num;
				bool flag = true;
				Vector3 vector2 = this.tr.position + vector;
				if (vector2.x >= Links.x.formation.bottomRight.transform.position.x)
				{
					flag = false;
				}
				if (vector2.x <= Links.x.formation.bottomLeft.transform.position.x)
				{
					flag = false;
				}
				if (vector2.y >= Links.x.formation.topRight.transform.position.y)
				{
					flag = false;
				}
				if (vector2.y <= Links.x.formation.bottomRight.transform.position.y)
				{
					flag = false;
				}
				if (flag)
				{
					this.tr.position += vector;
				}
				CompanionDrop closestBox = Links.x.formation.GetClosestBox(this.tr.position);
				this.prevBox = Links.x.formation.hoverBox;
				if (closestBox != this.prevBox)
				{
					if (this.prevBox)
					{
						this.prevBox.OnPointerExit(null);
						this.prevBox.img.material = Links.x.sceneMap.normalMaterial;
					}
					if (closestBox)
					{
						closestBox.OnPointerEnter(null);
						closestBox.img.material = Links.x.sceneMap.hoverMaterial;
					}
				}
				Links.x.formation.hoverBox = closestBox;
			}
			Links.x.formation.dragMessage.text = Links.x.formation.formationDescription;
		}
	}

	// Token: 0x06000E9C RID: 3740 RVA: 0x00116C14 File Offset: 0x00114E14
	private void LateUpdate()
	{
		this.SetIcons();
		if (this.waitForDrag && Links.x.mk)
		{
			if (Input.GetMouseButton(0) && (Time.realtimeSinceStartup > this.waitForDragTime + 0.3f || Vector3.Distance(this.waitForDragPos, Input.mousePosition) > Links.x.formation.dragThreshold))
			{
				this.Drag();
				return;
			}
			if (Time.realtimeSinceStartup > this.waitForDragTime + 0.3f)
			{
				this.waitForDrag = false;
			}
		}
	}

	// Token: 0x06000E9D RID: 3741 RVA: 0x00116C98 File Offset: 0x00114E98
	private void Drag()
	{
		this.dragging = true;
		for (int i = 0; i < this.allCompanionIcons.Count; i++)
		{
			if (this.allCompanionIcons[i].character && (Links.x.mk || (Links.x.joy && this.allCompanionIcons[i] != this)))
			{
				this.allCompanionIcons[i].border.raycastTarget = false;
				this.allCompanionIcons[i].btn.interactable = false;
			}
		}
		Links.x.formationTooltip.EndHoverText(base.gameObject, false);
		this.savedPos = this.tr.position;
		CompanionDrop component = this.tr.parent.gameObject.GetComponent<CompanionDrop>();
		this.tr.SetParent(Links.x.formation.dragHolder, true);
		Links.x.formation.dragging = true;
		this.useItems.interactable = false;
		this.autoCast.interactable = false;
		this.useItems.targetGraphic.raycastTarget = false;
		this.autoCast.targetGraphic.raycastTarget = false;
		this.useItems.graphic.raycastTarget = false;
		this.autoCast.graphic.raycastTarget = false;
		this.leader.interactable = false;
		this.leader.interactable = false;
		this.leader.isOn = false;
		this.character.stats.isLeader = false;
		this.leaderJoy.isOn = this.leader.isOn;
		this.leaderJoy.interactable = this.leader.interactable;
		if (Links.x.joy)
		{
			this.leaderJoy.gameObject.SetActive(this.leader.interactable);
		}
		this.leader.targetGraphic.raycastTarget = false;
		this.leader.graphic.raycastTarget = false;
		Links.x.formation.group1box.StartDrag();
		Links.x.formation.group2box.StartDrag();
		Links.x.formation.group3box.StartDrag();
		Links.x.formation.group4box.StartDrag();
		Links.x.formation.group5box.StartDrag();
		Links.x.formation.group6box.StartDrag();
		Links.x.formation.group7box.StartDrag();
		Links.x.formation.group8box.StartDrag();
		Links.x.formation.group9box.StartDrag();
		this.offset = this.tr.position - Links.x.menuCamera.ScreenToWorldPoint(Input.mousePosition);
		Links.x.formation.StartDrag(this, component);
		this.SetIcons();
		this.SetButton(true);
		this.SetControllerIcons(false);
		this.waitForDrag = false;
		if (Links.x.joy)
		{
			Links.x.hudControl.JoyBox(false);
		}
	}

	// Token: 0x06000E9E RID: 3742 RVA: 0x00116FCC File Offset: 0x001151CC
	private void SetIcons()
	{
		this.useItemsRT.anchoredPosition3D = this.rt.anchoredPosition3D - this.useItemOffset;
		this.autoCastRT.anchoredPosition3D = this.rt.anchoredPosition3D - this.autoCastOffset;
		this.leaderRT.anchoredPosition3D = this.rt.anchoredPosition3D - this.leaderOffset;
		if (this.useItemTr.parent != this.tr.parent)
		{
			this.useItemTr.SetParent(this.tr.parent);
			this.autoCastTr.SetParent(this.tr.parent);
			this.leaderTr.SetParent(this.tr.parent);
			this.autoCastTr.SetAsLastSibling();
			this.useItemTr.SetAsLastSibling();
			this.leaderTr.SetAsLastSibling();
			this.useItemTr.localScale = Vector3.one;
			this.autoCastTr.localScale = Vector3.one;
		}
	}

	// Token: 0x06000E9F RID: 3743 RVA: 0x001170DF File Offset: 0x001152DF
	public void PointerUpFromTrigger()
	{
		if (this.dragging)
		{
			this.OnPointerUp(null);
			return;
		}
		this.OnPointerDown(null);
	}

	// Token: 0x06000EA0 RID: 3744 RVA: 0x001170F8 File Offset: 0x001152F8
	public void OnPointerDown(PointerEventData eventData)
	{
		if (Links.x.formation.selectedCharacter)
		{
			CompanionIcon selectedIcon = Links.x.formation.GetSelectedIcon();
			if (selectedIcon)
			{
				selectedIcon.selected.enabled = false;
			}
		}
		Links.x.formation.UpdateSelectedCharacter(this.character);
		this.selected.enabled = true;
		if (!this.dragging)
		{
			if (Links.x.joy)
			{
				this.Drag();
				return;
			}
			if (!this.waitForDrag)
			{
				this.waitForDrag = true;
				this.waitForDragTime = Time.realtimeSinceStartup;
				this.waitForDragPos = Input.mousePosition;
			}
		}
	}

	// Token: 0x06000EA1 RID: 3745 RVA: 0x001171A0 File Offset: 0x001153A0
	public void SetButton(bool state)
	{
		if (state)
		{
			this.normal * 1.5f;
			Button button = this.btn;
			ColorBlock colors = button.colors;
			colors.disabledColor = colors.highlightedColor;
			button.colors = colors;
			return;
		}
		Button button2 = this.btn;
		ColorBlock colors2 = button2.colors;
		colors2.disabledColor = this.normal;
		button2.colors = colors2;
	}

	// Token: 0x06000EA2 RID: 3746 RVA: 0x00117203 File Offset: 0x00115403
	public void OnPointerUp(PointerEventData eventData)
	{
		if (this.dragging)
		{
			this.EndDrag();
		}
		this.waitToEndDrag = true;
		if (Links.x.joy)
		{
			this.dragging = false;
			this.waitToEndDrag = false;
		}
	}

	// Token: 0x06000EA3 RID: 3747 RVA: 0x00117234 File Offset: 0x00115434
	public void EndDrag()
	{
		this.SetButton(false);
		this.dragging = false;
		this.useItems.interactable = true;
		this.autoCast.interactable = true;
		this.useItems.targetGraphic.raycastTarget = true;
		this.autoCast.targetGraphic.raycastTarget = true;
		this.useItems.graphic.raycastTarget = true;
		this.autoCast.graphic.raycastTarget = true;
		if (Links.x.formation.hoverBox != null)
		{
			this.character.stats.formationGroup = Links.x.formation.hoverBox.dropNum;
			Links.x.formation.SetPortraitParent(this.index, true);
			Links.x.formation.group1box.EndDrag();
			Links.x.formation.group2box.EndDrag();
			Links.x.formation.group3box.EndDrag();
			Links.x.formation.group4box.EndDrag();
			Links.x.formation.group5box.EndDrag();
			Links.x.formation.group6box.EndDrag();
			Links.x.formation.group7box.EndDrag();
			Links.x.formation.group8box.EndDrag();
			Links.x.formation.group9box.EndDrag();
		}
		else
		{
			this.tr.position = this.savedPos;
			Links.x.formation.SetPortraitParent(this.index, true);
			Links.x.formation.group1box.EndDrag();
			Links.x.formation.group2box.EndDrag();
			Links.x.formation.group3box.EndDrag();
			Links.x.formation.group4box.EndDrag();
			Links.x.formation.group5box.EndDrag();
			Links.x.formation.group6box.EndDrag();
			Links.x.formation.group7box.EndDrag();
			Links.x.formation.group8box.EndDrag();
			Links.x.formation.group9box.EndDrag();
		}
		for (int i = 0; i < this.allCompanionIcons.Count; i++)
		{
			if (this.allCompanionIcons[i].character)
			{
				this.allCompanionIcons[i].border.raycastTarget = true;
				this.allCompanionIcons[i].btn.interactable = true;
			}
		}
		if (this.prevBox)
		{
			this.prevBox.OnPointerExit(null);
			this.prevBox.img.material = Links.x.sceneMap.normalMaterial;
		}
		CompanionDrop component = this.tr.parent.gameObject.GetComponent<CompanionDrop>();
		Links.x.SetFormationParty();
		Links.x.formation.EndDrag(component);
		this.SetIcons();
		this.SetControllerIcons(true);
		if (Links.x.joy)
		{
			EventSystem.current.SetSelectedGameObject(base.gameObject);
			this.OnPointerEnter(null);
			Links.x.hudControl.SetJoyBox(base.gameObject.GetComponent<UITriggerButton>());
		}
	}

	// Token: 0x06000EA4 RID: 3748 RVA: 0x001175A4 File Offset: 0x001157A4
	public void OnPointerEnter(PointerEventData eventData)
	{
		this.hovering = true;
		if (!this.character)
		{
			return;
		}
		if (!Input.GetMouseButton(0))
		{
			bool flag = this.dragging;
		}
		bool joy = Links.x.joy;
		CompanionDrop component = this.tr.parent.gameObject.GetComponent<CompanionDrop>();
		if (component)
		{
			component.SetButton(true);
		}
		if (Links.x.formation && component)
		{
			Links.x.formation.GetDescription(this, component);
			Links.x.formation.UpdateSliderValues(this.character);
		}
		if (this.waitToEndDrag)
		{
			this.dragging = false;
			this.waitToEndDrag = false;
		}
	}

	// Token: 0x06000EA5 RID: 3749 RVA: 0x0011765A File Offset: 0x0011585A
	public CompanionDrop GetDrop()
	{
		return this.tr.parent.gameObject.GetComponent<CompanionDrop>();
	}

	// Token: 0x06000EA6 RID: 3750 RVA: 0x00117674 File Offset: 0x00115874
	public void OnPointerExit(PointerEventData eventData)
	{
		CompanionDrop component = this.tr.parent.gameObject.GetComponent<CompanionDrop>();
		if (component)
		{
			component.SetButton(false);
		}
		this.hovering = false;
		if (!Links.x.formation.selectedCharacter)
		{
			Links.x.formation.formationDescription = Links.x.formation.defaultText;
			Links.x.formation.EmptySliders();
		}
		else if (Links.x.mk)
		{
			Links.x.formation.GetDescription(null, null);
			Links.x.formation.UpdateSliderValues(Links.x.formation.selectedCharacter);
		}
		this.waitForDragTime = 0f;
		this.waitForDrag = false;
	}

	// Token: 0x06000EA7 RID: 3751 RVA: 0x00117740 File Offset: 0x00115940
	public void SetControllerIcons(bool state)
	{
		if (this.dragging)
		{
			state = false;
		}
		if (!this.character)
		{
			state = false;
			this.leaderJoy.gameObject.SetActive(false);
			this.autoCastJoy.gameObject.SetActive(false);
			return;
		}
		this.autoCastJoy.gameObject.SetActive(false);
		if (this.character.stats.defendMainInCombat && Links.x.joy)
		{
			this.leaderJoy.gameObject.SetActive(state);
			return;
		}
		this.leaderJoy.gameObject.SetActive(false);
	}

	// Token: 0x04001717 RID: 5911
	public int index;

	// Token: 0x04001718 RID: 5912
	public Character character;

	// Token: 0x04001719 RID: 5913
	public bool dragging;

	// Token: 0x0400171A RID: 5914
	public RawImage companionPortrait;

	// Token: 0x0400171B RID: 5915
	public Image border;

	// Token: 0x0400171C RID: 5916
	public Button btn;

	// Token: 0x0400171D RID: 5917
	public RenderTexture texturesIdle;

	// Token: 0x0400171E RID: 5918
	public bool hovering;

	// Token: 0x0400171F RID: 5919
	private Transform tr;

	// Token: 0x04001720 RID: 5920
	private Vector3 offset;

	// Token: 0x04001721 RID: 5921
	private List<CompanionIcon> allCompanionIcons;

	// Token: 0x04001722 RID: 5922
	private Vector3 savedPos;

	// Token: 0x04001723 RID: 5923
	private float updateTime;

	// Token: 0x04001724 RID: 5924
	private bool waitToEndDrag;

	// Token: 0x04001725 RID: 5925
	private CompanionDrop prevBox;

	// Token: 0x04001726 RID: 5926
	public RectTransform rt;

	// Token: 0x04001727 RID: 5927
	private Color normal;

	// Token: 0x04001728 RID: 5928
	public Toggle useItems;

	// Token: 0x04001729 RID: 5929
	public Toggle autoCast;

	// Token: 0x0400172A RID: 5930
	public Toggle leader;

	// Token: 0x0400172B RID: 5931
	public Toggle useItemsJoy;

	// Token: 0x0400172C RID: 5932
	public Toggle autoCastJoy;

	// Token: 0x0400172D RID: 5933
	public Toggle leaderJoy;

	// Token: 0x0400172E RID: 5934
	private Vector3 useItemOffset;

	// Token: 0x0400172F RID: 5935
	private Vector3 autoCastOffset;

	// Token: 0x04001730 RID: 5936
	private Vector3 leaderOffset;

	// Token: 0x04001731 RID: 5937
	public RectTransform useItemsRT;

	// Token: 0x04001732 RID: 5938
	public RectTransform autoCastRT;

	// Token: 0x04001733 RID: 5939
	public RectTransform leaderRT;

	// Token: 0x04001734 RID: 5940
	public Transform useItemTr;

	// Token: 0x04001735 RID: 5941
	public Transform autoCastTr;

	// Token: 0x04001736 RID: 5942
	public Transform leaderTr;

	// Token: 0x04001737 RID: 5943
	public bool waitForDrag;

	// Token: 0x04001738 RID: 5944
	public float waitForDragTime;

	// Token: 0x04001739 RID: 5945
	public Vector3 waitForDragPos;

	// Token: 0x0400173A RID: 5946
	public Image selected;
}
