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

// Token: 0x020000A4 RID: 164
public class EnchantItem : MonoBehaviour
{
	// Token: 0x06000F4A RID: 3914 RVA: 0x00128658 File Offset: 0x00126858
	private void Awake()
	{
		base.gameObject.SetActive(false);
		this.camera3D.SetActive(false);
		this.camTr = this.camera3D.transform;
		foreach (object obj in this.camTr)
		{
			Transform transform = (Transform)obj;
			if (transform.gameObject.name != "Camera" && transform != this.camTr)
			{
				transform.gameObject.SetActive(false);
			}
		}
	}

	// Token: 0x06000F4B RID: 3915 RVA: 0x00128704 File Offset: 0x00126904
	private void Update()
	{
		if (Links.x.characterSheet.open && this.open && Links.x.characterSheet.character != this.character)
		{
			this.OpenMenu(Links.x.characterSheet.character);
		}
	}

	// Token: 0x06000F4C RID: 3916 RVA: 0x0012875C File Offset: 0x0012695C
	public void UpdateSlider()
	{
		GameObject currentEventObject = Links.x.gameplay.currentEventObject;
		if (currentEventObject)
		{
			float x = Links.x.fellowship.direction.x;
			Slider slider = null;
			if (currentEventObject == this.slider1.gameObject)
			{
				slider = this.slider1;
			}
			if (currentEventObject == this.slider2.gameObject)
			{
				slider = this.slider2;
			}
			if (currentEventObject == this.slider3.gameObject)
			{
				slider = this.slider3;
			}
			float num = 1f;
			if (!slider)
			{
				Links.x.inventory.inSlider = false;
				return;
			}
			Links.x.inventory.inSlider = true;
			float num2 = 0.3f;
			if (Time.realtimeSinceStartup > this.lastSliderTime + num2 || this.lastSliderTime == 0f)
			{
				if (x > 0f)
				{
					slider.value += num;
				}
				else if (x < 0f)
				{
					slider.value -= num;
				}
				this.lastSliderTime = Time.realtimeSinceStartup;
				return;
			}
		}
		else
		{
			Links.x.inventory.inSlider = false;
		}
	}

	// Token: 0x06000F4D RID: 3917 RVA: 0x00128888 File Offset: 0x00126A88
	public void OpenMenu(Character c)
	{
		this.spellcraftingMenu = Links.x.spellcrafting;
		this.ClearSpells();
		this.character = c;
		this.buttonText.text = "Select Weapon or Jewelry";
		this.selectedSpell = "";
		this.selected.SetActive(false);
		this.description.text = "";
		this.noSpells.text = "";
		this.itemImage.enabled = false;
		this.slider1Obj.SetActive(false);
		this.slider2Obj.SetActive(false);
		this.slider3Obj.SetActive(false);
		this.SetInteractableState(false);
		Links.x.gameplay.SetInteractableSelected(EventSystem.current, this.confirmButton.gameObject);
		this.confirmButton.gameObject.GetComponent<UITriggerButton>().selectNext = null;
		if (!this.open)
		{
			Records.x.AddMenu(base.gameObject);
			this.open = true;
			base.gameObject.SetActive(true);
			Links.x.characterSheet.anim.Play("OpenEnchantMenu");
			this.camera3D.SetActive(true);
			string text = "SemizenStatue_C6_FortenLazure";
			if (Links.x.diorama.sceneName == "C5_AbandonedFarm")
			{
				text = "SemizenStatue_C5_AbandonedFarm";
			}
			if (Links.x.diorama.sceneName == "D6_Rozafir")
			{
				text = "SemizenStatue_D6_Rozafir";
			}
			foreach (object obj in this.camTr)
			{
				Transform transform = (Transform)obj;
				if (transform.gameObject.name == text)
				{
					transform.gameObject.SetActive(true);
				}
				else if (transform.gameObject.name != "Camera" && transform != this.camTr)
				{
					transform.gameObject.SetActive(false);
				}
			}
		}
	}

	// Token: 0x06000F4E RID: 3918 RVA: 0x00128A98 File Offset: 0x00126C98
	public void SwitchCharacter(Character c)
	{
		this.item = null;
		this.itemEquipped = null;
		this.OpenMenu(c);
	}

	// Token: 0x06000F4F RID: 3919 RVA: 0x00128AB0 File Offset: 0x00126CB0
	public void SelectedItem(Item itemToEnchant, ItemEquipped itemEquippedToEnchant)
	{
		this.item = itemToEnchant;
		this.itemEquipped = null;
		if (itemEquippedToEnchant)
		{
			this.itemEquipped = itemEquippedToEnchant;
			this.item = null;
		}
		this.SetupGrid();
		if (this.item)
		{
			this.itemImage.sprite = Links.x.archives.GetItem(this.item.invRow._UIModel);
		}
		else
		{
			this.itemImage.sprite = Links.x.archives.GetItem(this.itemEquipped.invRow._UIModel);
		}
		this.itemImage.enabled = true;
		if (Links.x.joy)
		{
			Links.x.gameplay.SetInteractableSelected(EventSystem.current, this.usedSpells[0]);
			this.confirmButton.gameObject.GetComponent<UITriggerButton>().selectNext = this.usedSpells[0];
			Navigation navigation = this.slider1.navigation;
			navigation.selectOnUp = this.usedSpells[0].GetComponent<Button>();
			if (this.slider2.interactable)
			{
				navigation.selectOnDown = this.slider2;
			}
			else
			{
				navigation.selectOnDown = this.confirmButton;
			}
			this.slider1.navigation = navigation;
			navigation = this.slider2.navigation;
			if (this.slider3.interactable)
			{
				navigation.selectOnDown = this.slider3;
			}
			else
			{
				navigation.selectOnDown = this.confirmButton;
			}
			this.slider2.navigation = navigation;
			navigation = this.confirmButton.navigation;
			if (this.slider3.interactable)
			{
				navigation.selectOnUp = this.slider3;
			}
			else if (this.slider2.interactable)
			{
				navigation.selectOnUp = this.slider2;
			}
			else
			{
				navigation.selectOnUp = this.slider1;
			}
			this.confirmButton.navigation = navigation;
		}
	}

	// Token: 0x06000F50 RID: 3920 RVA: 0x00128CA0 File Offset: 0x00126EA0
	public void SetupGrid()
	{
		this.ClearSpells();
		this.isWeapon = false;
		if (this.item && this.item.invRow._Tag == "Weapon")
		{
			this.isWeapon = true;
		}
		if (this.itemEquipped && this.itemEquipped.invRow._Tag == "Weapon")
		{
			this.isWeapon = true;
		}
		if (Links.x.diorama.sceneName == "C6_FortenLazure")
		{
			this.weaponBindingSpells = new string[] { "BlindingLight", "KnockbackLight", "SunStrafe" };
			this.jewelryBindingSpells = new string[] { "SunShield", "DaylightPower" };
		}
		if (Links.x.diorama.sceneName == "C5_AbandonedFarm")
		{
			this.weaponBindingSpells = new string[] { "HarvestHealth", "HarvestGluttony" };
			this.jewelryBindingSpells = new string[] { "HarvestStamina", "HarvestOverflowing" };
		}
		if (Links.x.diorama.sceneName == "D6_Rozafir")
		{
			this.jewelryBindingSpells = new string[] { "ChoraFluteBuff", "ChoraMusicResist" };
			this.weaponBindingSpells = new string[] { "ChoraFormUpRally", "ChoraFormUpSpeed", "ChoraHitHornTarget" };
		}
		int num = this.weaponBindingSpells.Length;
		if (!this.isWeapon)
		{
			num = this.jewelryBindingSpells.Length;
		}
		for (int i = 0; i < num; i++)
		{
			string text;
			if (this.isWeapon)
			{
				text = this.weaponBindingSpells[i];
			}
			else
			{
				text = this.jewelryBindingSpells[i];
			}
			GameObject gameObject;
			if (this.openSpells.Count > 0)
			{
				gameObject = this.openSpells[0];
				this.openSpells.RemoveAt(0);
			}
			else
			{
				gameObject = Object.Instantiate<GameObject>(this.spellPrefab, Vector3.zero, Quaternion.identity);
				gameObject.transform.SetParent(this.grid, true);
				gameObject.transform.localPosition = Vector3.zero;
				gameObject.transform.localRotation = Quaternion.identity;
				gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
			}
			gameObject.name = text;
			this.usedSpells.Add(gameObject);
			TMP_Text component = gameObject.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>();
			this.paganRow = Links.x.library.GetPaganRow(text);
			component.text = this.paganRow._DisplayName;
			gameObject.SetActive(true);
			if (i == 0)
			{
				this.SetSpell(gameObject);
			}
		}
	}

	// Token: 0x06000F51 RID: 3921 RVA: 0x00128F64 File Offset: 0x00127164
	public void CloseMenu()
	{
		Links.x.characterSheet.anim.Play("CloseEnchantMenu");
		this.camera3D.SetActive(false);
		Records.x.RemoveMenu(base.gameObject);
		this.ClearSpells();
		this.usedSpells.Clear();
		this.spellcraftingMenu.fromEnchantMenu = false;
		base.gameObject.GetComponent<Animator>().Play("CloseSubMenu");
		this.open = false;
		this.selected.SetActive(false);
		Links.x.inventory.inSlider = false;
	}

	// Token: 0x06000F52 RID: 3922 RVA: 0x00128FFC File Offset: 0x001271FC
	private void ClearSpells()
	{
		for (int i = 0; i < this.usedSpells.Count; i++)
		{
			this.openSpells.Add(this.usedSpells[i]);
			this.usedSpells[i].SetActive(false);
		}
		this.usedSpells.Clear();
	}

	// Token: 0x06000F53 RID: 3923 RVA: 0x00129053 File Offset: 0x00127253
	public void FinishCloseMenu()
	{
	}

	// Token: 0x06000F54 RID: 3924 RVA: 0x00129058 File Offset: 0x00127258
	public void SetSpell(GameObject go)
	{
		this.selectedSpell = go.name;
		this.selected.transform.SetParent(go.transform, false);
		this.selected.transform.SetAsLastSibling();
		this.selected.SetActive(true);
		this.selected.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(0f, 0f, 0f);
		this.paganRow = Links.x.library.GetPaganRow(this.selectedSpell);
		this.spellcraftingMenu.fromEnchantMenu = true;
		this.spellcraftingMenu.enchant = this;
		this.spellcraftingMenu.selectedPagan = this.paganRow;
		this.spellcraftingMenu.characterSkill = 0f;
		this.spellcraftingMenu.SelectLearningButton(-1);
		this.slider1Obj.SetActive(true);
		this.SetDesc();
		Links.x.characterSheet.PlayRandomClick();
		this.spellCost = this.SpellCost();
		if (this.spellcraftingMenu.selectedPagan._Slider2 != "")
		{
			this.slider2Obj.SetActive(true);
		}
		else
		{
			this.slider2Obj.SetActive(false);
		}
		if (this.spellcraftingMenu.selectedPagan._Slider3 != "")
		{
			this.slider3Obj.SetActive(true);
		}
		else
		{
			this.slider3Obj.SetActive(false);
		}
		if (Links.x.joy)
		{
			Links.x.gameplay.SetInteractableSelected(EventSystem.current, this.slider1.gameObject);
			this.confirmButton.gameObject.GetComponent<UITriggerButton>().selectNext = this.usedSpells[0];
		}
	}

	// Token: 0x06000F55 RID: 3925 RVA: 0x0012920D File Offset: 0x0012740D
	public void UpdateVariables()
	{
		this.spellcraftingMenu.UpdateVariablesFromEnchantMenu();
		this.SetDesc();
		if (this.open)
		{
			Links.x.characterSheet.PlayRandomClick();
		}
		this.spellCost = this.SpellCost();
	}

	// Token: 0x06000F56 RID: 3926 RVA: 0x00129244 File Offset: 0x00127444
	private void SetDesc()
	{
		this.str.Clear();
		this.str.Append("<font=BoldOutline><size=1.6em>");
		this.str.Append(this.paganRow._DisplayName);
		this.str.Append("</font></size>\n");
		this.str.Append(this.spellcraftingMenu.SetDescription());
		float num = this.Difficulty();
		float num2 = this.character.stats.EnchantWearingAbility();
		if (num2 < 0f)
		{
			num2 = 0f;
		}
		this.str.Append("\nDifficulty is ");
		this.str.Append(this.Difficulty());
		if (num2 >= num)
		{
			this.str.Append(" vs. <color=#CA8C1E>");
		}
		else
		{
			this.str.Append(" vs. <color=#F3482F>");
		}
		this.str.Append(this.character.stats.GetName());
		this.str.Append("'s ");
		this.str.Append(num2);
		this.str.Append("</color>");
		this.description.text = this.str.ToString();
	}

	// Token: 0x06000F57 RID: 3927 RVA: 0x00129380 File Offset: 0x00127580
	private int SpellCost()
	{
		int num = this.spellcraftingMenu.selectedPagan._LearningCost;
		if (this.slider1.interactable)
		{
			num += Mathf.RoundToInt((this.slider1.value - this.slider1.minValue) * Records.x.EnchantCostWeight(this.spellcraftingMenu.selectedPagan._Slider1));
		}
		if (this.slider2.interactable)
		{
			num += Mathf.RoundToInt((this.slider2.value - this.slider2.minValue) * Records.x.EnchantCostWeight(this.spellcraftingMenu.selectedPagan._Slider2));
		}
		if (this.slider3.interactable)
		{
			num += Mathf.RoundToInt((this.slider3.value - this.slider3.minValue) * Records.x.EnchantCostWeight(this.spellcraftingMenu.selectedPagan._Slider3));
		}
		this.cost.text = "Donation: " + num.ToString() + " emeralds";
		if (num <= Links.x.inventory.emeralds)
		{
			if (this.isWeapon)
			{
				this.buttonText.text = "Bless Weapon";
			}
			else
			{
				this.buttonText.text = "Bless Jewelry";
			}
		}
		else
		{
			this.buttonText.text = "Too few emeralds";
		}
		return num;
	}

	// Token: 0x06000F58 RID: 3928 RVA: 0x001294E3 File Offset: 0x001276E3
	private float R1(float s)
	{
		return Mathf.Round(s * 10f) / 10f;
	}

	// Token: 0x06000F59 RID: 3929 RVA: 0x001294F8 File Offset: 0x001276F8
	private float Difficulty()
	{
		float num = (float)this.spellcraftingMenu.selectedPagan._Difficulty;
		if (this.slider1.interactable)
		{
			num += (this.slider1.value - this.slider1.minValue) * Records.x.EnchantCostWeight(this.spellcraftingMenu.selectedPagan._Slider1);
		}
		if (this.slider2.interactable)
		{
			num += (this.slider2.value - this.slider2.minValue) * Records.x.EnchantCostWeight(this.spellcraftingMenu.selectedPagan._Slider2);
		}
		if (this.slider3.interactable)
		{
			num += (this.slider3.value - this.slider3.minValue) * Records.x.EnchantCostWeight(this.spellcraftingMenu.selectedPagan._Slider3);
		}
		num /= 4f;
		return Mathf.Round(num * 10f) / 10f;
	}

	// Token: 0x06000F5A RID: 3930 RVA: 0x001295F8 File Offset: 0x001277F8
	public void Enchant()
	{
		if (this.spellCost <= Links.x.inventory.emeralds && (this.item || this.itemEquipped) && this.selectedSpell != "")
		{
			this.paganRow = Links.x.library.GetPaganRow(this.selectedSpell);
			int number = this.paganRow._Number;
			float num = this.slider1.value;
			float num2 = this.slider2.value;
			float num3 = this.slider3.value;
			string text;
			if (this.item)
			{
				this.item.socketA = new Vector3((float)number, num, num2);
				this.item.socketB = new Vector3(num3, (float)this.spellCost, this.Difficulty());
				this.item.SetDurabilityVisual();
				text = this.item.invRow._DisplayName;
			}
			else
			{
				this.itemEquipped.socketA = new Vector3((float)number, num, num2);
				this.itemEquipped.socketB = new Vector3(num3, (float)this.spellCost, this.Difficulty());
				this.itemEquipped.SetDurabilityVisual();
				text = this.itemEquipped.invRow._DisplayName;
			}
			this.str.Clear();
			this.str.Append(Links.x.gameFeed.GetPartyColorText(this.character));
			this.str.Append(this.character.stats.GetName());
			this.str.Append("</color>");
			this.str.Append(" blessed ");
			this.str.Append(text);
			this.str.Append(" with ");
			this.str.Append(this.paganRow._DisplayName);
			if (Records.x.showRulesetInfo)
			{
				this.str.Append(" <color #857C6C>... ");
				this.str.Append(this.character.stats.IntercessionCalculation());
				this.str.Append("</color>");
			}
			Links.x.gameFeed.AddFeed(this.str.ToString());
			this.character.stats.AddSkillProgress(16, this.Difficulty(), null);
			if (this.item)
			{
				Links.x.inventory.SetBagSockets(this.item);
			}
			else
			{
				Links.x.inventory.SetBagSocketsEquipped(this.itemEquipped);
			}
			if (Records.x.InCombat(false))
			{
				Links.x.inventory.AddCombatTime(Links.x.characterSheet.character, "ExtraLong");
			}
			int num4 = Random.Range(0, 3);
			if (num4 == 0)
			{
				MasterAudio.PlaySoundAndForget("Feedback", Random.Range(0.5f, 0.6f), new float?(1f), 0f, "IntercessionA", null);
			}
			if (num4 == 1)
			{
				MasterAudio.PlaySoundAndForget("Feedback", Random.Range(0.5f, 0.6f), new float?(1f), 0f, "IntercessionB", null);
			}
			if (num4 == 2)
			{
				MasterAudio.PlaySoundAndForget("Feedback", Random.Range(0.5f, 0.6f), new float?(1f), 0f, "IntercessionC", null);
			}
			Links.x.itemPickupFX.gameObject.SetActive(true);
			Links.x.itemPickupFX.gameObject.GetComponent<HannahAnimator>().Play("Idle");
			Links.x.itemPickupFX.position = this.itemImage.gameObject.transform.position;
			Links.x.inventory.fx.gameObject.SetActive(true);
			Links.x.inventory.fx.gameObject.GetComponent<HannahAnimator>().Play("Idle");
			if (this.item)
			{
				Links.x.inventory.fx.gameObject.transform.position = this.item.gameObject.transform.position;
			}
			else
			{
				Links.x.inventory.fx.gameObject.transform.position = this.itemEquipped.gameObject.transform.position;
			}
			this.character.UpdateListsFromInv(true);
			Links.x.inventory.emeralds -= this.spellCost;
			Links.x.inventory.UpdateMisc();
			if (this.itemEquipped && !this.isWeapon)
			{
				this.character.stats.EnchantJewelryEffects(Links.x.inventory.GetEquippedID(this.itemEquipped) * 2, true);
			}
			this.OpenMenu(this.character);
		}
	}

	// Token: 0x06000F5B RID: 3931 RVA: 0x00129B1C File Offset: 0x00127D1C
	private void SetInteractableState(bool state)
	{
		this.slider1.interactable = state;
		this.slider2.interactable = state;
		this.slider3.interactable = state;
		this.tetherSlider.interactable = state;
		this.slider1Image.enabled = state;
		this.slider2Image.enabled = state;
		this.slider3Image.enabled = state;
		this.tetherImage.enabled = state;
		this.slider1label.enabled = state;
		this.slider2label.enabled = state;
		this.slider3label.enabled = state;
		this.slider1Min.enabled = state;
		this.slider1Max.enabled = state;
		this.slider2Min.enabled = state;
		this.slider2Max.enabled = state;
		this.slider3Min.enabled = state;
		this.slider3Max.enabled = state;
		this.tetherMin.enabled = state;
		this.tetherMax.enabled = state;
		this.tetherLabel.enabled = state;
		if (!state)
		{
			this.description.text = "";
		}
	}

	// Token: 0x04001913 RID: 6419
	public bool open;

	// Token: 0x04001914 RID: 6420
	public Image itemImage;

	// Token: 0x04001915 RID: 6421
	public Item item;

	// Token: 0x04001916 RID: 6422
	public ItemEquipped itemEquipped;

	// Token: 0x04001917 RID: 6423
	public Character character;

	// Token: 0x04001918 RID: 6424
	public GameObject spellPrefab;

	// Token: 0x04001919 RID: 6425
	public GameObject camera3D;

	// Token: 0x0400191A RID: 6426
	public List<GameObject> openSpells = new List<GameObject>();

	// Token: 0x0400191B RID: 6427
	public List<GameObject> usedSpells = new List<GameObject>();

	// Token: 0x0400191C RID: 6428
	public TextMeshProUGUI noSpells;

	// Token: 0x0400191D RID: 6429
	public TextMeshProUGUI description;

	// Token: 0x0400191E RID: 6430
	public TextMeshProUGUI buttonText;

	// Token: 0x0400191F RID: 6431
	public ButtonMultiTargets confirmButton;

	// Token: 0x04001920 RID: 6432
	public Transform grid;

	// Token: 0x04001921 RID: 6433
	private Library.Pagan paganRow;

	// Token: 0x04001922 RID: 6434
	private string selectedSpell;

	// Token: 0x04001923 RID: 6435
	public string[] weaponBindingSpells;

	// Token: 0x04001924 RID: 6436
	public string[] jewelryBindingSpells;

	// Token: 0x04001925 RID: 6437
	public GameObject selected;

	// Token: 0x04001926 RID: 6438
	public GameObject slider1Obj;

	// Token: 0x04001927 RID: 6439
	public GameObject slider2Obj;

	// Token: 0x04001928 RID: 6440
	public GameObject slider3Obj;

	// Token: 0x04001929 RID: 6441
	private StringFast str = new StringFast(64);

	// Token: 0x0400192A RID: 6442
	public Slider slider1;

	// Token: 0x0400192B RID: 6443
	public Slider slider2;

	// Token: 0x0400192C RID: 6444
	public Slider slider3;

	// Token: 0x0400192D RID: 6445
	public Slider tetherSlider;

	// Token: 0x0400192E RID: 6446
	public TextMeshProUGUI slider1label;

	// Token: 0x0400192F RID: 6447
	public TextMeshProUGUI slider2label;

	// Token: 0x04001930 RID: 6448
	public TextMeshProUGUI slider3label;

	// Token: 0x04001931 RID: 6449
	public TextMeshProUGUI tetherLabel;

	// Token: 0x04001932 RID: 6450
	public TextMeshProUGUI tetherMin;

	// Token: 0x04001933 RID: 6451
	public TextMeshProUGUI tetherMax;

	// Token: 0x04001934 RID: 6452
	public TextMeshProUGUI slider1Min;

	// Token: 0x04001935 RID: 6453
	public TextMeshProUGUI slider1Max;

	// Token: 0x04001936 RID: 6454
	public TextMeshProUGUI slider2Min;

	// Token: 0x04001937 RID: 6455
	public TextMeshProUGUI slider2Max;

	// Token: 0x04001938 RID: 6456
	public TextMeshProUGUI slider3Min;

	// Token: 0x04001939 RID: 6457
	public TextMeshProUGUI slider3Max;

	// Token: 0x0400193A RID: 6458
	public TextMeshProUGUI cost;

	// Token: 0x0400193B RID: 6459
	public Image slider1Image;

	// Token: 0x0400193C RID: 6460
	public Image slider2Image;

	// Token: 0x0400193D RID: 6461
	public Image slider3Image;

	// Token: 0x0400193E RID: 6462
	public Image tetherImage;

	// Token: 0x0400193F RID: 6463
	private Spellcrafting spellcraftingMenu;

	// Token: 0x04001940 RID: 6464
	public int spellCost;

	// Token: 0x04001941 RID: 6465
	private bool isWeapon;

	// Token: 0x04001942 RID: 6466
	private Transform camTr;

	// Token: 0x04001943 RID: 6467
	private float lastSliderTime;
}
