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

// Token: 0x020000D7 RID: 215
public class SelectionBox : MonoBehaviour
{
	// Token: 0x06001403 RID: 5123 RVA: 0x00183C3C File Offset: 0x00181E3C
	private void Start()
	{
		this.tr = base.transform;
		this.party = Links.x.party;
		this.selectionBox.gameObject.SetActive(true);
		if (Records.x.turnBased || !this.useBox)
		{
			base.enabled = false;
		}
	}

	// Token: 0x06001404 RID: 5124 RVA: 0x00183C94 File Offset: 0x00181E94
	private void LateUpdate()
	{
		if (Records.x.turnBased || !this.useBox)
		{
			return;
		}
		if (!Records.x.removeControls && !Records.x.atMerchant && !Links.x.inventory.open && !Records.x.inOverworldMap)
		{
			if (Input.GetMouseButtonDown(0) && !Links.x.gameplay.uiHover)
			{
				this.charactersInBox.Clear();
				this.drawing = true;
				this.startPos = Input.mousePosition;
				this.canvasWidth = this.canvasRt.sizeDelta.x;
				this.resolutionMultiplierX = this.canvasWidth / (float)Screen.width;
				this.startPos.x = this.startPos.x * this.resolutionMultiplierX;
				this.resolutionMultiplierY = this.ScreenYModifier();
				this.startPos.y = this.startPos.y * this.resolutionMultiplierY;
				this.holdTime = Time.timeSinceLevelLoad;
				if (this.coroutine != null)
				{
					base.StopCoroutine(this.coroutine);
				}
				this.selectionBox.SetAsLastSibling();
				Links.x.hudControl.ToggleCanvasGroup(false);
			}
			if (Input.GetMouseButtonUp(0) && this.drawing)
			{
				this.ReleaseSelectionBox();
				if (Links.x.fellowship.drawingSquare)
				{
					bool flag = false;
					using (List<Character>.Enumerator enumerator = this.party.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							if (enumerator.Current.selected)
							{
								flag = true;
							}
						}
					}
					if (!flag)
					{
						this.party[0].AddToSelected(true, true);
						this.party[0].portrait.SetSelection();
						this.party[0].SetAsLeader();
						this.party[0].HoverIn();
					}
					else
					{
						Links.x.fellowship.SetFirstAsSelected(true);
					}
					if (Links.x.hasMain && Links.x.main.summoned)
					{
						for (int i = 0; i < this.party.Count; i++)
						{
							if (this.party[i].selected && !this.party[i].summoned)
							{
								this.party[i].SetAsLeader();
								break;
							}
						}
					}
					foreach (Character character in this.party)
					{
						character.HoverOut(false);
					}
				}
				Links.x.fellowship.drawingSquare = false;
				Links.x.hudControl.ToggleCanvasGroup(true);
			}
			if (Input.GetMouseButton(0) && this.drawing)
			{
				this.mousePos = Input.mousePosition;
				this.resolutionMultiplierX = this.canvasWidth / (float)Screen.width;
				this.mousePos.x = this.mousePos.x * this.resolutionMultiplierX;
				this.resolutionMultiplierY = this.ScreenYModifier();
				this.mousePos.y = this.mousePos.y * this.resolutionMultiplierY;
				this.UpdateSelectionBox();
				return;
			}
		}
		else if (Records.x.removeControls && (Links.x.fellowship.drawingSquare || this.drawing))
		{
			this.ReleaseSelectionBox();
			Links.x.fellowship.drawingSquare = false;
		}
	}

	// Token: 0x06001405 RID: 5125 RVA: 0x00184024 File Offset: 0x00182224
	private IEnumerator HoverOff()
	{
		foreach (Character character in this.party)
		{
			character.HoverOut(false);
		}
		yield return this.delayShort;
		foreach (Character character2 in this.party)
		{
			if (character2.selected)
			{
				character2.HoverIn();
			}
		}
		yield return this.delayShort;
		foreach (Character character3 in this.party)
		{
			character3.HoverOut(false);
		}
		this.coroutine = null;
		yield break;
	}

	// Token: 0x06001406 RID: 5126 RVA: 0x00184034 File Offset: 0x00182234
	private void UpdateSelectionBox()
	{
		if (!this.selectionBox.gameObject.activeInHierarchy)
		{
			this.selectionBox.gameObject.SetActive(true);
		}
		float num = this.mousePos.x - this.startPos.x;
		float num2 = this.mousePos.y - this.startPos.y;
		this.selectionBox.sizeDelta = new Vector2(Mathf.Abs(num), Mathf.Abs(num2));
		this.selectionBox.anchoredPosition = this.startPos + new Vector2(num / 2f, num2 / 2f);
		if (num < 0f)
		{
			num *= -1f;
		}
		if (num2 < 0f)
		{
			num2 *= -1f;
		}
		if (num > 15f || num2 > 15f)
		{
			Links.x.fellowship.drawingSquare = true;
		}
		if (Links.x.fellowship.drawingSquare)
		{
			this.CheckCharacters();
		}
	}

	// Token: 0x06001407 RID: 5127 RVA: 0x00184130 File Offset: 0x00182330
	private void ReleaseSelectionBox()
	{
		this.drawing = false;
		this.selectionBox.gameObject.SetActive(false);
	}

	// Token: 0x06001408 RID: 5128 RVA: 0x0018414C File Offset: 0x0018234C
	public void CheckCharacters()
	{
		Vector2 vector = this.selectionBox.anchoredPosition - this.selectionBox.sizeDelta / 2f;
		Vector2 vector2 = this.selectionBox.anchoredPosition + this.selectionBox.sizeDelta / 2f;
		int num = 0;
		bool flag = false;
		this.tempParty.Clear();
		for (int i = 0; i < this.party.Count; i++)
		{
			if (this.party[i] && !this.party[i].isBoro && this.party[i].tr)
			{
				this.tempParty.Add(this.party[i]);
			}
		}
		foreach (Character character in this.tempParty)
		{
			Vector3 vector3 = Links.x.worldCamera.WorldToScreenPoint(character.tr.position);
			this.resolutionMultiplierX = this.canvasWidth / (float)Screen.width;
			vector3.x *= this.resolutionMultiplierX;
			this.resolutionMultiplierY = this.ScreenYModifier();
			vector3.y *= this.resolutionMultiplierY;
			if (vector3.x > vector.x && vector3.x < vector2.x && vector3.y > vector.y && vector3.y < vector2.y)
			{
				if (character.CanChooseActions() && !character.outsideDome && !character.waitingToJoinDome && !character.selected)
				{
					character.AddToSelected(true, false);
					if (character.portrait)
					{
						character.portrait.SetSelection();
					}
					character.HoverIn();
				}
				num++;
			}
			else if (character.selected)
			{
				character.RemoveFromSelected(false);
				if (character.portrait)
				{
					character.portrait.SetSelection();
				}
				character.HoverOut(false);
			}
		}
		if (flag)
		{
			this.SetFirstInBoxAsSelected();
		}
		if (num == 0 && Records.x.InCombat(false))
		{
			Links.x.main = null;
			Links.x.hasMain = false;
		}
	}

	// Token: 0x06001409 RID: 5129 RVA: 0x001843D0 File Offset: 0x001825D0
	public void SetFirstInBoxAsSelected()
	{
		if (this.charactersInBox.Count == 0)
		{
			if (!this.tempParty[0].mainSelected)
			{
				this.tempParty[0].SetAsLeader();
				return;
			}
		}
		else if (!this.charactersInBox[0].mainSelected)
		{
			this.charactersInBox[0].SetAsLeader();
		}
	}

	// Token: 0x0600140A RID: 5130 RVA: 0x00184434 File Offset: 0x00182634
	private float ScreenYModifier()
	{
		if ((float)Screen.width / (float)Screen.height / 1.7777778f > 1f)
		{
			float num = (float)Screen.width / (float)Screen.height;
			float num2 = (float)Screen.height / (float)Screen.width;
			float num3 = (float)Screen.width * 1080f / 1920f;
			float num4 = 1080f / num3 * 1080f / (num2 * (float)Screen.width);
			return 1080f / (num2 * (float)Screen.width);
		}
		return 1080f / this.GetScreenHeight();
	}

	// Token: 0x0600140B RID: 5131 RVA: 0x001844BE File Offset: 0x001826BE
	private float GetScreenHeight()
	{
		return (float)Mathf.RoundToInt((float)Screen.width * 0.5625f);
	}

	// Token: 0x0400225B RID: 8795
	public RectTransform selectionBox;

	// Token: 0x0400225C RID: 8796
	public RectTransform canvasRt;

	// Token: 0x0400225D RID: 8797
	private Vector2 startPos;

	// Token: 0x0400225E RID: 8798
	private Vector3 mousePos;

	// Token: 0x0400225F RID: 8799
	private Transform tr;

	// Token: 0x04002260 RID: 8800
	private float resolutionMultiplierX;

	// Token: 0x04002261 RID: 8801
	private float resolutionMultiplierY;

	// Token: 0x04002262 RID: 8802
	private float holdTime;

	// Token: 0x04002263 RID: 8803
	public bool testing;

	// Token: 0x04002264 RID: 8804
	private List<Character> party;

	// Token: 0x04002265 RID: 8805
	private float canvasWidth;

	// Token: 0x04002266 RID: 8806
	private IEnumerator coroutine;

	// Token: 0x04002267 RID: 8807
	private bool drawing;

	// Token: 0x04002268 RID: 8808
	private WaitForSecondsRealtime delayShort = new WaitForSecondsRealtime(0.12f);

	// Token: 0x04002269 RID: 8809
	public List<Character> charactersInBox = new List<Character>();

	// Token: 0x0400226A RID: 8810
	private List<Character> tempParty = new List<Character>();

	// Token: 0x0400226B RID: 8811
	public bool useBox;
}
