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

// Token: 0x02000097 RID: 151
public class Books : MonoBehaviour
{
	// Token: 0x06000E67 RID: 3687 RVA: 0x0011471A File Offset: 0x0011291A
	private void Start()
	{
		this.tr = base.gameObject.transform;
	}

	// Token: 0x06000E68 RID: 3688 RVA: 0x00114730 File Offset: 0x00112930
	public void CloseMenu()
	{
		if (Links.x.characterSheet.open)
		{
			MasterAudio.PlaySoundAndForget("Paper Open 2", 1f, new float?(1f), 0f, "", null);
		}
		this.ReturnSelected();
		for (int i = 0; i < this.spells.Count; i++)
		{
			if (this.spells[i].star)
			{
				this.spells[i].star = null;
			}
			Links.x.cellar.ReturnPooledUIObject(49, this.spells[i].gameObject);
		}
		this.RemoveStars();
		this.spells.Clear();
		this.open = false;
		this.anim.Play("CloseSubMenu");
		Links.x.characterSheet.CheckLevelUp();
		if (Links.x.joy)
		{
			Links.x.characterSheet.closeText.SetActive(true);
		}
		if (Links.x.joy)
		{
			Links.x.characterSheet.DraggingButtons(false);
		}
		if (Links.x.joy)
		{
			if (this.clickedOpen)
			{
				if (this.clickedOpen.activeSelf && this.clickedOpen.activeInHierarchy)
				{
					Links.x.gameplay.SetInteractableSelected(EventSystem.current, this.clickedOpen);
					return;
				}
				Links.x.gameplay.SetInteractableSelected(EventSystem.current, Links.x.characterSheet.character.characterSheetPortrait.gameObject);
				return;
			}
			else
			{
				Links.x.gameplay.SetInteractableSelected(EventSystem.current, Links.x.characterSheet.character.characterSheetPortrait.gameObject);
			}
		}
	}

	// Token: 0x06000E69 RID: 3689 RVA: 0x00114903 File Offset: 0x00112B03
	public void OpenWords()
	{
		this.currentType = "Words";
		this.SetGrid("Words", Links.x.characterSheet.character, false);
	}

	// Token: 0x06000E6A RID: 3690 RVA: 0x0011492C File Offset: 0x00112B2C
	public void OpenSpells()
	{
		this.favoriteMessage.SetActive(false);
		this.RemoveStars();
		MasterAudio.PlaySoundAndForget("Paper Open", 1f, new float?(1f), 0f, "", null);
		if (Links.x.arrowKit.open)
		{
			Links.x.arrowKit.CloseMenu();
		}
		if (Links.x.characterSheet.note.activeSelf)
		{
			Links.x.characterSheet.CloseNote();
		}
		this.currentType = "Spells";
		this.SetGrid("Spells", Links.x.characterSheet.character, false);
		Links.x.characterSheet.levelUpButton.SetActive(false);
		if (Links.x.joy)
		{
			Links.x.characterSheet.closeText.SetActive(false);
		}
		if (Links.x.joy)
		{
			Links.x.characterSheet.DraggingButtons(true);
			if (this.spells.Count > 0)
			{
				Links.x.gameplay.SetInteractableSelected(EventSystem.current, this.spells[0].gameObject);
			}
		}
		if (Links.x.characterSheet.minstrel)
		{
			this.closeButton.SetActive(false);
		}
		else
		{
			this.closeButton.SetActive(true);
		}
		Links.x.characterSheet.SetPortraitNav(false);
		if (Links.x.joy)
		{
			this.controllerSpellDelete.spriteAsset = Links.x.GetSpriteSheetAsset(Links.x.gameplay.currentSpriteSheetAsset);
			this.str.Clear();
			this.str.Append("<voffset=.4em><size=40><sprite=");
			int num = Links.x.gameplay.GetControllerSprite("Action", Links.x.gameplay.controllerName);
			this.str.Append(num);
			this.str.Append("></size></voffset>Toggle Favorite    ");
			this.str.Append("<voffset=.4em><size=40><sprite=");
			num = Links.x.gameplay.GetControllerSprite("Interact", Links.x.gameplay.controllerName);
			this.str.Append(num);
			this.str.Append("></size></voffset>Delete Spell ");
			this.controllerSpellDelete.text = this.str.ToString();
			return;
		}
		this.controllerSpellDelete.text = "";
	}

	// Token: 0x06000E6B RID: 3691 RVA: 0x00114BB0 File Offset: 0x00112DB0
	public void OpenSongs()
	{
		this.controllerSpellDelete.text = "";
		this.RemoveStars();
		this.favoriteMessage.SetActive(false);
		MasterAudio.PlaySoundAndForget("Paper Open", 1f, new float?(1f), 0f, "", null);
		if (Links.x.arrowKit.open)
		{
			Links.x.arrowKit.CloseMenu();
		}
		if (Links.x.characterSheet.note.activeSelf)
		{
			Links.x.characterSheet.CloseNote();
		}
		this.autoCast.SetActive(false);
		this.currentType = "Songs";
		this.SetGrid("Songs", Links.x.characterSheet.character, false);
		Links.x.characterSheet.levelUpButton.SetActive(false);
		if (Links.x.joy)
		{
			Links.x.characterSheet.closeText.SetActive(false);
		}
		if (Links.x.joy)
		{
			Links.x.characterSheet.DraggingButtons(true);
			if (this.spells.Count > 0 && !Links.x.characterSheet.minstrel)
			{
				Links.x.gameplay.SetInteractableSelected(EventSystem.current, this.spells[0].gameObject);
			}
		}
		if (Links.x.characterSheet.minstrel)
		{
			this.closeButton.SetActive(false);
		}
		else
		{
			this.closeButton.SetActive(true);
		}
		this.knowledgeText.enabled = false;
		this.krt.gameObject.SetActive(false);
		Links.x.characterSheet.SetPortraitNav(false);
	}

	// Token: 0x06000E6C RID: 3692 RVA: 0x00114D74 File Offset: 0x00112F74
	private void Update()
	{
		if (Links.x.characterSheet.open && (Links.x.characterSheet.character != this.character || this.doUpdate))
		{
			this.favoriteMessage.SetActive(false);
			this.RemoveStars();
			this.SetGrid(this.currentType, Links.x.characterSheet.character, false);
			this.doUpdate = false;
		}
	}

	// Token: 0x06000E6D RID: 3693 RVA: 0x00114DEB File Offset: 0x00112FEB
	public void UpdateAbilityTriggers()
	{
	}

	// Token: 0x06000E6E RID: 3694 RVA: 0x00114DF0 File Offset: 0x00112FF0
	public void Equipped()
	{
		if (this.savedCharacter != Links.x.characterSheet.character)
		{
			this.abilityNames = Links.x.characterSheet.character.stats.abilityNames;
			this.uniqueNames = Links.x.characterSheet.character.stats.uniqueNames;
			this.spellLevels = Links.x.characterSheet.character.stats.abilityLevels;
			this.abilityCosts = Links.x.characterSheet.character.stats.abilityCost;
			this.abilityRows = Links.x.characterSheet.character.stats.abilityRows;
		}
		this.savedCharacter = Links.x.characterSheet.character;
		this.songPointsText.text = Links.x.characterSheet.character.stats.songPoints.ToString();
	}

	// Token: 0x06000E6F RID: 3695 RVA: 0x00114EF8 File Offset: 0x001130F8
	public void SetGrid(string type, Character c, bool allSpells)
	{
		this.ReturnSelected();
		base.gameObject.SetActive(true);
		this.open = true;
		this.character = c;
		if (this.savedCharacter != Links.x.characterSheet.character)
		{
			this.abilityNames = Links.x.characterSheet.character.stats.abilityNames;
			this.uniqueNames = Links.x.characterSheet.character.stats.uniqueNames;
			this.spellLevels = Links.x.characterSheet.character.stats.abilityLevels;
			this.abilityCosts = Links.x.characterSheet.character.stats.abilityCost;
			this.abilityRows = Links.x.characterSheet.character.stats.abilityRows;
		}
		this.savedCharacter = Links.x.characterSheet.character;
		int count = this.abilityNames.Count;
		if (this.spells.Count > 0)
		{
			for (int i = 0; i < this.spells.Count; i++)
			{
				if (this.spells[i].star)
				{
					this.spells[i].star = null;
				}
				Links.x.cellar.ReturnPooledUIObject(49, this.spells[i].gameObject);
			}
			this.spells.Clear();
		}
		if (allSpells)
		{
			this.headerText.text = c.stats.GetName() + "'s ";
			TextMeshProUGUI textMeshProUGUI = this.headerText;
			textMeshProUGUI.text += " Spell Variants";
		}
		if (type == "Spells")
		{
			this.pointsObject.SetActive(false);
			this.knowledgeText.enabled = true;
			this.knowledgeText.text = "";
			this.knowledge = this.character.stats.knowledge;
			this.str.Clear();
			this.str.Append("\n\n<align=center><size=1.5em><font=Bold>Learned Effects</font></size></align>\n");
			for (int j = 0; j < this.knowledge.Count; j++)
			{
				this.str.Append("<size=1.2em><font=Bold>");
				Library.Pagan paganRow = Links.x.library.GetPaganRow(this.knowledge[j]);
				this.str.Append(paganRow._DisplayName);
				this.str.Append("</font></size>\n");
				this.str.Append(paganRow._Description);
				this.str.Append("\n\n");
			}
			if (this.knowledge.Count == 0)
			{
				this.str.Append("None\n");
			}
			this.str.Append("\n\n");
			this.knowledgeText.text = this.str.ToString();
			this.krt.sizeDelta = new Vector2(this.krt.sizeDelta.x, this.knowledgeText.preferredHeight);
		}
		else
		{
			this.pointsObject.SetActive(true);
		}
		if ((type == "Spells" && c.stats.HasSpellbook()) || (type == "Songs" && c.stats.HasSongbook()))
		{
			for (int k = 0; k < count; k++)
			{
				bool flag = false;
				if (this.abilityRows[k]._IsSpell && allSpells)
				{
					flag = true;
				}
				string mainSkill = this.abilityRows[k]._MainSkill;
				if (this.abilityRows[k]._IsSpell && type == "Spells" && !allSpells)
				{
					flag = true;
				}
				if (!this.abilityRows[k]._IsSpell && !this.abilityRows[k]._IsSong && !allSpells && type == "Words")
				{
					flag = true;
				}
				if (this.abilityRows[k]._IsSong && type == "Songs" && !allSpells)
				{
					flag = true;
				}
				if (flag)
				{
					GameObject pooledGameObject = Links.x.cellar.GetPooledGameObject(49);
					pooledGameObject.SetActive(true);
					pooledGameObject.transform.SetParent(this.gridTransform, false);
					SkillButton component = pooledGameObject.GetComponent<SkillButton>();
					component.inSpellMenu = false;
					if (allSpells || type == "Spells")
					{
						component.abilityName = this.abilityNames[k];
						component.uniqueName = this.uniqueNames[k];
						component.index = k;
						component.abilityCost = (float)((int)this.abilityCosts[k].x);
						component.character = c;
						component.spellStats = this.spellLevels[k];
						if (allSpells)
						{
							component.inSpellMenu = true;
						}
						else
						{
							component.inSpellMenu = false;
						}
						if (this.character.stats.IsFavoriteSpell(component.uniqueName) && !component.star)
						{
							this.AddStar(component);
						}
					}
					if (type == "Songs")
					{
						component.abilityName = this.abilityNames[k];
						component.index = k;
						component.character = c;
						component.componentID = -1;
						component.uniqueName = this.uniqueNames[k];
						component.inSpellMenu = false;
					}
					if (type == "Words")
					{
						component.abilityName = this.abilityNames[k];
						component.index = k;
						component.character = c;
						component.componentID = -1;
						component.uniqueName = this.uniqueNames[k];
					}
					component.inWindow = false;
					component.Setup(true, true);
					this.spells.Add(component);
					pooledGameObject.transform.localPosition = new Vector3(pooledGameObject.transform.localPosition.x, pooledGameObject.transform.localPosition.y, 0f);
				}
			}
		}
		if (this.spells.Count > 0)
		{
			this.noAbilitiesText.text = "";
			this.headerText.text = c.stats.GetName() + "'s ";
			if (type == "Spells")
			{
				TextMeshProUGUI textMeshProUGUI2 = this.headerText;
				textMeshProUGUI2.text += "Spellbook";
			}
			if (type == "Songs")
			{
				TextMeshProUGUI textMeshProUGUI3 = this.headerText;
				textMeshProUGUI3.text += "Songbook";
			}
			if (type == "Words")
			{
				TextMeshProUGUI textMeshProUGUI4 = this.headerText;
				textMeshProUGUI4.text += " Words";
			}
			if (this.autoCast && !allSpells && this.currentType == "Spells")
			{
				this.autoCast.SetActive(true);
			}
			if (type == "Spells")
			{
				this.favoriteMessage.SetActive(true);
			}
		}
		else
		{
			this.str.Clear();
			this.str.Append(c.stats.GetName());
			if (type == "Spells")
			{
				if (!c.stats.HasSpellbook())
				{
					this.str.Append(" doesn't have a spellbook");
				}
				else
				{
					this.str.Append(" has not crafted any spells yet");
				}
				this.headerText.text = "";
			}
			if (type == "Songs")
			{
				if (!c.stats.HasSongbook())
				{
					this.str.Append(" doesn't have a songbook");
				}
				else
				{
					this.str.Append(" hasn't learned any songs yet");
				}
				this.headerText.text = "";
			}
			if (type == "Words")
			{
				this.str.Append("Words of Power");
			}
			this.str.Append(".");
			this.noAbilitiesText.text = this.str.ToString();
			if (this.autoCast)
			{
				this.autoCast.SetActive(false);
			}
		}
		if (this.empty && this.krt)
		{
			if (type == "Spells")
			{
				this.autoCast.transform.SetAsFirstSibling();
				this.empty.gameObject.transform.SetAsLastSibling();
				this.krt.gameObject.transform.SetAsLastSibling();
				this.empty.SetActive(true);
				this.krt.gameObject.SetActive(true);
				this.favoriteMessage.transform.SetAsLastSibling();
			}
			else
			{
				this.krt.gameObject.SetActive(false);
				this.empty.SetActive(false);
			}
		}
		this.UpdateSelected();
	}

	// Token: 0x06000E70 RID: 3696 RVA: 0x00115818 File Offset: 0x00113A18
	public void RemoveStars()
	{
		for (int i = 0; i < this.stars.Count; i++)
		{
			this.stars[i].SetActive(false);
			this.stars[i].transform.SetParent(this.tr);
		}
	}

	// Token: 0x06000E71 RID: 3697 RVA: 0x0011586C File Offset: 0x00113A6C
	public void AddStar(SkillButton b)
	{
		GameObject gameObject = null;
		bool flag = false;
		for (int i = 0; i < this.stars.Count; i++)
		{
			if (!this.stars[i])
			{
				flag = true;
			}
			else if (!this.stars[i].activeSelf)
			{
				gameObject = this.stars[i];
				break;
			}
		}
		if (flag)
		{
			this.stars.RemoveAll((GameObject item) => item == null);
		}
		if (!gameObject)
		{
			gameObject = Object.Instantiate<GameObject>(this.stars[0], Vector3.zero, Quaternion.Euler(Vector3.zero), null);
			this.stars.Add(gameObject);
		}
		b.star = gameObject;
		b.star.SetActive(true);
		b.star.transform.SetParent(b.transform);
		b.star.transform.position = b.transform.position;
		gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
		b.star.transform.localPosition = new Vector3(b.star.transform.localPosition.x, b.star.transform.localPosition.y, 0f);
	}

	// Token: 0x06000E72 RID: 3698 RVA: 0x001159D8 File Offset: 0x00113BD8
	public void AddFavoriteSpell(SkillButton b)
	{
		if (!this.open)
		{
			return;
		}
		if (this.character.stats.IsFavoriteSpell(b.uniqueName))
		{
			this.RemoveFavoriteSpell(b);
			return;
		}
		this.character.stats.AddFavoriteSpell(b.uniqueName);
		if (!b.star)
		{
			this.AddStar(b);
		}
	}

	// Token: 0x06000E73 RID: 3699 RVA: 0x00115A38 File Offset: 0x00113C38
	public void RemoveFavoriteSpell(SkillButton b)
	{
		this.character.stats.RemoveFavoriteSpell(b.uniqueName);
		if (b.star)
		{
			b.star.SetActive(false);
			b.star = null;
		}
	}

	// Token: 0x06000E74 RID: 3700 RVA: 0x00115A70 File Offset: 0x00113C70
	public void UpdateSelected()
	{
		foreach (Image image in this.selectedImages)
		{
			image.enabled = false;
		}
		if (this.currentType == "Songs")
		{
			this.equippedList = this.character.stats.equippedSongList;
		}
		if (this.currentType == "Songs")
		{
			int num = 0;
			int count = this.spells.Count;
			for (int i = 0; i < count; i++)
			{
				if (this.equippedList.Contains(this.spells[i].uniqueName))
				{
					this.selectedImages[num].enabled = true;
					this.selectedImages[num].gameObject.transform.SetParent(this.spells[i].iconBG.gameObject.transform, false);
					this.selectedImages[num].gameObject.transform.localPosition = new Vector3(-6f, 4f, 0f);
					num++;
				}
			}
		}
		this.Equipped();
	}

	// Token: 0x06000E75 RID: 3701 RVA: 0x00115BC0 File Offset: 0x00113DC0
	public void ReturnSelected()
	{
	}

	// Token: 0x06000E76 RID: 3702 RVA: 0x00115BC4 File Offset: 0x00113DC4
	public void CloseSpellDelete()
	{
		this.deletePopup.SetActive(false);
		if (Links.x.joy)
		{
			if (this.spells.Count > 0)
			{
				Links.x.gameplay.SetInteractableSelected(EventSystem.current, this.spells[0].gameObject);
				return;
			}
			Links.x.gameplay.SetInteractableSelected(EventSystem.current, Links.x.characterSheet.character.characterSheetPortrait.gameObject);
		}
	}

	// Token: 0x06000E77 RID: 3703 RVA: 0x00115C4A File Offset: 0x00113E4A
	public void OpenSpellDelete(string spellname)
	{
		this.toDelete = spellname;
		this.deletePopup.SetActive(true);
		this.deletePopupText.text = "Are you sure you want to delete " + spellname + "?";
	}

	// Token: 0x06000E78 RID: 3704 RVA: 0x00115C7A File Offset: 0x00113E7A
	public void Delete()
	{
		this.character.stats.RemoveSpell(this.toDelete);
		this.SetGrid("Spells", this.character, false);
		this.CloseSpellDelete();
	}

	// Token: 0x040016E9 RID: 5865
	public List<Library.Abilities> abilityRows;

	// Token: 0x040016EA RID: 5866
	private List<string> uniqueNames;

	// Token: 0x040016EB RID: 5867
	private List<string> abilityNames;

	// Token: 0x040016EC RID: 5868
	private List<Vector2> abilityCosts;

	// Token: 0x040016ED RID: 5869
	private List<Vector4> spellLevels;

	// Token: 0x040016EE RID: 5870
	public List<SkillButton> spells = new List<SkillButton>();

	// Token: 0x040016EF RID: 5871
	public TextMeshProUGUI deletePopupText;

	// Token: 0x040016F0 RID: 5872
	public TextMeshProUGUI noAbilitiesText;

	// Token: 0x040016F1 RID: 5873
	public TextMeshProUGUI headerText;

	// Token: 0x040016F2 RID: 5874
	public TextMeshProUGUI songPointsText;

	// Token: 0x040016F3 RID: 5875
	public TextMeshProUGUI knowledgeText;

	// Token: 0x040016F4 RID: 5876
	public TextMeshProUGUI controllerSpellDelete;

	// Token: 0x040016F5 RID: 5877
	public GameObject deletePopup;

	// Token: 0x040016F6 RID: 5878
	private StringFast str = new StringFast(64);

	// Token: 0x040016F7 RID: 5879
	public string toDelete;

	// Token: 0x040016F8 RID: 5880
	public Character character;

	// Token: 0x040016F9 RID: 5881
	public Character savedCharacter;

	// Token: 0x040016FA RID: 5882
	public Transform gridTransform;

	// Token: 0x040016FB RID: 5883
	public Animator anim;

	// Token: 0x040016FC RID: 5884
	public string currentType;

	// Token: 0x040016FD RID: 5885
	public bool open;

	// Token: 0x040016FE RID: 5886
	public List<Image> selectedImages = new List<Image>();

	// Token: 0x040016FF RID: 5887
	private List<string> equippedList;

	// Token: 0x04001700 RID: 5888
	public GameObject pointsObject;

	// Token: 0x04001701 RID: 5889
	public bool doUpdate;

	// Token: 0x04001702 RID: 5890
	public GameObject clickedOpen;

	// Token: 0x04001703 RID: 5891
	public GameObject closeButton;

	// Token: 0x04001704 RID: 5892
	private List<string> knowledge;

	// Token: 0x04001705 RID: 5893
	public RectTransform krt;

	// Token: 0x04001706 RID: 5894
	public GameObject empty;

	// Token: 0x04001707 RID: 5895
	public GameObject autoCast;

	// Token: 0x04001708 RID: 5896
	public GameObject favoriteMessage;

	// Token: 0x04001709 RID: 5897
	public List<GameObject> stars = new List<GameObject>();

	// Token: 0x0400170A RID: 5898
	private Transform tr;
}
