using System;
using System.Collections;
using System.Collections.Generic;
using DarkTonic.MasterAudio;
using Pathfinding;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x020000D2 RID: 210
public class Rest : MonoBehaviour
{
	// Token: 0x0600135B RID: 4955 RVA: 0x00174C69 File Offset: 0x00172E69
	private void Start()
	{
		this.anim.enabled = false;
	}

	// Token: 0x0600135C RID: 4956 RVA: 0x00174C77 File Offset: 0x00172E77
	public void ConfirmRest()
	{
		base.StartCoroutine(this.StartRest(true, 0, false));
	}

	// Token: 0x0600135D RID: 4957 RVA: 0x00174C8C File Offset: 0x00172E8C
	public void ConfirmRealRest()
	{
		Library.Inventory invRowFromName = Links.x.library.GetInvRowFromName("Blanket");
		this.partyWithItem.RemoveItem(invRowFromName._ID, 1);
		base.StartCoroutine(this.StartRest(true, 0, true));
	}

	// Token: 0x0600135E RID: 4958 RVA: 0x00174CD4 File Offset: 0x00172ED4
	public void UpdateSlider()
	{
		GameObject currentEventObject = Links.x.gameplay.currentEventObject;
		if (currentEventObject)
		{
			float x = Links.x.fellowship.direction.x;
			Slider slider = null;
			if (currentEventObject == this.slider.gameObject)
			{
				slider = this.slider;
			}
			float num = 1f;
			if (slider && (Time.realtimeSinceStartup > this.lastSliderTime + 0.3f || this.lastSliderTime == 0f))
			{
				if (x > 0f)
				{
					slider.value += num;
				}
				else if (x < 0f)
				{
					slider.value -= num;
				}
				this.lastSliderTime = Time.realtimeSinceStartup;
			}
		}
	}

	// Token: 0x0600135F RID: 4959 RVA: 0x00174D94 File Offset: 0x00172F94
	public void SliderValueChanged()
	{
		int num = (int)Mathf.Floor(Links.x.gameplay.gameHours) + (int)this.slider.value;
		if (num >= 24)
		{
			num -= 24;
		}
		if ((num >= 0 && num < 4) || num >= 22)
		{
			this.timeOfDay = "Morning";
			this.timeImage.sprite = this.morningImage;
		}
		if (num >= 4 && num < 10)
		{
			this.timeOfDay = "Afternoon";
			this.timeImage.sprite = this.afternoonImage;
		}
		if (num >= 10 && num < 16)
		{
			this.timeOfDay = "Evening";
			this.timeImage.sprite = this.eveningImage;
		}
		if (num >= 16 && num < 22)
		{
			this.timeOfDay = "Night";
			this.timeImage.sprite = this.nightImage;
		}
		this.hourCount.text = this.slider.value.ToString();
		if (this.itemCount > 0)
		{
			this.restHealButtonText.text = "Rest & Heal (" + Mathf.Round(this.HealAmount(this.slider.value) * 100f).ToString() + "%)";
			this.restInBedText.text = this.restHealButtonText.text;
			return;
		}
		this.restInBedText.text = "Rest & Heal (" + Mathf.Round(this.HealAmount(this.slider.value) * 100f).ToString() + "%)";
		this.restHealButtonText.text = "Rest & Heal";
	}

	// Token: 0x06001360 RID: 4960 RVA: 0x00174F34 File Offset: 0x00173134
	public float HealAmount(float f)
	{
		float num = f / 10f;
		if (num > 1f)
		{
			num = 1f;
		}
		return num;
	}

	// Token: 0x06001361 RID: 4961 RVA: 0x00174F58 File Offset: 0x00173158
	private IEnumerator StartRest(bool fromMenu, int numberOfHours, bool healWithBlanket)
	{
		if (fromMenu)
		{
			this.CloseMenu(true);
		}
		else if (!Records.x.paused)
		{
			Links.x.gameplay.TogglePause(true);
		}
		Links.x.gaia.PlayFade("Out", "Unscaled");
		Records.x.RemoveControls(true);
		Records.x.partyCamping = true;
		yield return new WaitForSecondsRealtime(0.65f);
		Links.x.cameraEffects.immediateTransition = true;
		if (!fromMenu)
		{
			yield return new WaitForSecondsRealtime(2f);
		}
		Links.x.sensory.SetMagicaClothState(false);
		this.party = Links.x.party;
		this.portraitOrder = Links.x.portraitOrder;
		List<float> partyHealth = new List<float>();
		List<int> partyTotal = new List<int>();
		yield return new WaitForSecondsRealtime(1f);
		int v = (int)this.slider.value;
		if (!fromMenu)
		{
			v = numberOfHours;
		}
		for (int j = 0; j < this.party.Count; j++)
		{
			int num = 0;
			if (healWithBlanket || !this.waiting)
			{
				num = Mathf.RoundToInt((float)this.party[j].stats.HealthMax() * this.HealAmount(this.slider.value));
			}
			int num2 = v;
			if (v > 10)
			{
				num2 = 10;
			}
			if (this.party[j])
			{
				partyHealth.Add((float)num / (float)num2);
				partyTotal.Add(this.party[j].stats.HealthCurrent() + num);
			}
		}
		bool flag = false;
		int num3;
		for (int i = 0; i < v; i = num3 + 1)
		{
			if (!flag)
			{
				Links.x.gameplay.AddHours();
				if (fromMenu)
				{
					for (int k = 0; k < this.party.Count; k++)
					{
						if (this.party[k])
						{
							if ((healWithBlanket || !this.waiting) && this.party[k].stats.HealthCurrent() < partyTotal[k])
							{
								this.party[k].stats.AddHealth((int)partyHealth[k]);
							}
							if (this.party[k].inSporeForest)
							{
								this.party[k].stats.AddSpiritFromKill(30f);
							}
						}
					}
					yield return new WaitForSecondsRealtime(0.1f);
					flag = Links.x.diorama.SeenWhileResting();
					if (flag)
					{
						break;
					}
				}
			}
			num3 = i;
		}
		if (!flag)
		{
			yield return new WaitForSecondsRealtime(0.5f);
			Links.x.sensory.SetMagicaClothState(true);
			Links.x.gaia.PlayFade("In", "Unscaled");
			yield return new WaitForSecondsRealtime(0.5f);
			Records.x.RemoveControls(false);
			Links.x.gameplay.TogglePause(false);
			for (int l = 0; l < this.party.Count; l++)
			{
				if (this.party[l] && this.party[l].stunned)
				{
					this.party[l].EndStun();
					this.party[l].AddToSelected(true, true);
				}
			}
			this.anim.enabled = false;
			Records.x.partyCamping = false;
		}
		else
		{
			this.previousNodes.Clear();
			Links.x.sensory.SetMagicaClothState(true);
			Records.x.StartUIFade(true, true);
			List<Character> list = Links.x.portraitOrder;
			List<Transform> restingBoatPoints = Links.x.gaia.restingBoatPoints;
			int num4 = Random.Range(0, list.Count);
			for (int m = 0; m < 4; m++)
			{
				int num5 = Random.Range(0, list.Count);
				if (num4 != num5)
				{
					break;
				}
			}
			Links.x.gameplay.TogglePause(false);
			Links.x.gaia.PlayFade("In", "Unscaled");
			Links.x.gameplay.TogglePause(false);
			Records.x.RemoveControls(false);
			Records.x.partyCamping = false;
			Links.x.main.StartQuip("Enemies afoot! Look to your weapons, friends!");
			MasterAudio.PlaySoundAndForget("Enraged", 1f, new float?(1f), 0f, "", null);
			Links.x.rtsCamera.StartShake("");
			this.anim.enabled = false;
		}
		yield break;
	}

	// Token: 0x06001362 RID: 4962 RVA: 0x00174F7C File Offset: 0x0017317C
	public void JustRest(int numberOfHours)
	{
		base.StartCoroutine(this.StartRest(false, numberOfHours, false));
	}

	// Token: 0x06001363 RID: 4963 RVA: 0x00174F90 File Offset: 0x00173190
	public void StartMenu(bool justWait)
	{
		this.waiting = justWait;
		if (Records.x.InCombat(false))
		{
			Links.x.gameFeed.ShowNotice("Cannot rest now");
			Links.x.gameFeed.AddFeed("Cannot rest at this time.");
			return;
		}
		if (this.waiting)
		{
			this.waitButtons.SetActive(true);
			this.restButtons.SetActive(false);
			this.partyWithItem = null;
			this.itemCount = 0;
			List<Character> list = Links.x.portraitOrder;
			Library.Inventory invRowFromName = Links.x.library.GetInvRowFromName("Blanket");
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i])
				{
					int num = list[i].HowManyItemsInBag(invRowFromName._ID, 18);
					if (num > 0)
					{
						this.itemCount += num;
						if (!this.partyWithItem)
						{
							this.partyWithItem = list[i];
						}
					}
				}
			}
			if (this.itemCount == 0)
			{
				this.restAndHealButton.interactable = false;
			}
			else
			{
				this.restAndHealButton.interactable = true;
			}
		}
		else
		{
			this.itemCount = 0;
			this.waitButtons.SetActive(false);
			this.restButtons.SetActive(true);
			this.partyWithItem = null;
		}
		if (this.contents1.activeSelf)
		{
			return;
		}
		Links.x.gameplay.lastMenu = "Rest";
		Links.x.gameplay.openingAnotherMenu = true;
		Links.x.gameplay.CloseAllMenus();
		if (!Records.x.inMenus && Links.x.gaia.sceneLoaded)
		{
			Links.x.gameplay.OpenDock();
		}
		this.on = true;
		Records.x.AddMenu(base.gameObject);
		this.anim.enabled = true;
		bool flag = false;
		if (Links.x.joy && (Links.x.gameplay.dockStart || Links.x.gameplay.itemsStart))
		{
			flag = true;
		}
		if (flag)
		{
			this.anim.Play("RestOpen", 0, 1f);
		}
		else
		{
			this.anim.Play("RestOpen");
		}
		this.contents1.SetActive(true);
		this.slider.value = 1f;
		this.SliderValueChanged();
		if (Links.x.joy)
		{
			Links.x.hudControl.UpdateMenuSwitcher("Rest");
			if (this.waiting)
			{
				Navigation navigation = this.slider.navigation;
				navigation.selectOnDown = this.waitButtons.transform.GetChild(0).gameObject.GetComponent<Button>();
				this.slider.navigation = navigation;
			}
			else
			{
				Navigation navigation2 = this.slider.navigation;
				navigation2.selectOnDown = this.restButtons.transform.GetChild(0).gameObject.GetComponent<Button>();
				this.slider.navigation = navigation2;
			}
			Links.x.gameplay.SetInteractableSelected(EventSystem.current, this.slider.gameObject);
		}
		this.bedRollCount.text = this.itemCount.ToString();
	}

	// Token: 0x06001364 RID: 4964 RVA: 0x001752C4 File Offset: 0x001734C4
	public void CloseMenu(bool fromCloseButton)
	{
		this.on = false;
		Records.x.RemoveMenu(base.gameObject);
		bool flag = false;
		if (Links.x.joy && (Links.x.gameplay.dockStart || Links.x.gameplay.itemsStart))
		{
			flag = true;
		}
		if (flag)
		{
			this.anim.Play("RestClose", 0, 1f);
		}
		else
		{
			this.anim.Play("RestClose");
		}
		Links.x.hudControl.JoyBox(false);
		if (fromCloseButton)
		{
			Links.x.gameplay.CloseDock();
		}
	}

	// Token: 0x06001365 RID: 4965 RVA: 0x00175367 File Offset: 0x00173567
	public void EndMenu()
	{
		this.contents1.SetActive(false);
	}

	// Token: 0x04002094 RID: 8340
	public Animator anim;

	// Token: 0x04002095 RID: 8341
	public int hoursToRest;

	// Token: 0x04002096 RID: 8342
	public Slider slider;

	// Token: 0x04002097 RID: 8343
	public GameObject contents1;

	// Token: 0x04002098 RID: 8344
	public Image timeImage;

	// Token: 0x04002099 RID: 8345
	public Sprite morningImage;

	// Token: 0x0400209A RID: 8346
	public Sprite afternoonImage;

	// Token: 0x0400209B RID: 8347
	public Sprite eveningImage;

	// Token: 0x0400209C RID: 8348
	public Sprite nightImage;

	// Token: 0x0400209D RID: 8349
	public string timeOfDay;

	// Token: 0x0400209E RID: 8350
	public TextMeshProUGUI restHealButtonText;

	// Token: 0x0400209F RID: 8351
	public TextMeshProUGUI restInBedText;

	// Token: 0x040020A0 RID: 8352
	public TextMeshProUGUI hourCount;

	// Token: 0x040020A1 RID: 8353
	public TextMeshProUGUI bedRollCount;

	// Token: 0x040020A2 RID: 8354
	public bool on;

	// Token: 0x040020A3 RID: 8355
	public bool waiting;

	// Token: 0x040020A4 RID: 8356
	private List<Character> party;

	// Token: 0x040020A5 RID: 8357
	private List<Character> portraitOrder;

	// Token: 0x040020A6 RID: 8358
	public List<GraphNode> previousNodes = new List<GraphNode>();

	// Token: 0x040020A7 RID: 8359
	public GameObject restButtons;

	// Token: 0x040020A8 RID: 8360
	public GameObject waitButtons;

	// Token: 0x040020A9 RID: 8361
	public Button restAndHealButton;

	// Token: 0x040020AA RID: 8362
	private int itemCount;

	// Token: 0x040020AB RID: 8363
	public Character partyWithItem;

	// Token: 0x040020AC RID: 8364
	private float lastSliderTime;
}
