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

// Token: 0x020000C2 RID: 194
public class Merchant : MonoBehaviour
{
	// Token: 0x06001243 RID: 4675 RVA: 0x00162C47 File Offset: 0x00160E47
	private void Start()
	{
		this.gridImg = this.bag.gridImg;
		this.gridState = this.bag.gridState;
		this.bag.Setup();
	}

	// Token: 0x06001244 RID: 4676 RVA: 0x00162C78 File Offset: 0x00160E78
	public void RemoveItem(Item currItem)
	{
		if (this.isBoro())
		{
			Records.x.boroWeight -= (float)((int)currItem.invRow._Weight);
			this.SetWeightText();
		}
		Vector4 durability = currItem.durability;
		durability.y = 0f;
		currItem.durability = durability;
		this.items.Remove(currItem);
		this.CheckSlotCosts();
		if (this.onlySelling)
		{
			this.merchantCharacter.stats.savedMerchantTime = Links.x.gameplay.gameDays;
		}
		this.UpdateMerchant();
	}

	// Token: 0x06001245 RID: 4677 RVA: 0x00162D0C File Offset: 0x00160F0C
	public void AddItem(Item currItem, int amt, int max, bool addToBag)
	{
		Links.x.inventory.secondaryDragSave = true;
		this.items.Add(currItem);
		Vector4 durability = currItem.durability;
		durability.y = (float)Links.x.gameplay.gameDays;
		if (durability.y == 0f)
		{
			durability.y = 1f;
		}
		currItem.durability = durability;
		currItem.forSale = true;
		string name = currItem.invRow._Name;
		int num = this.history.IndexOf(name);
		if (currItem.invRow._NeedFullStack)
		{
			amt = 1;
		}
		if (num > -1)
		{
			int num2 = this.historyCounts[num];
			if (amt < 1)
			{
				amt = 1;
			}
			this.historyCounts[num] = num2 + amt;
		}
		else
		{
			this.history.Add(name);
			if (amt < 1)
			{
				amt = 1;
			}
			this.historyCounts.Add(amt);
		}
		if (this.isBoro())
		{
			Records.x.boroWeight += currItem.invRow._Weight;
			this.SetWeightText();
		}
		this.UpdateMerchant();
	}

	// Token: 0x06001246 RID: 4678 RVA: 0x00162E1C File Offset: 0x0016101C
	public float GetItemHistoryCount(Library.Inventory row, float cost)
	{
		int num = this.history.IndexOf(row._Name);
		float num2 = 1f;
		if (num > -1)
		{
			float num3 = (float)this.historyCounts[num];
			if (num3 == 1f)
			{
				num2 = 0.95f;
			}
			if (num3 == 2f)
			{
				num2 = 0.9f;
			}
			if (num3 == 3f)
			{
				num2 = 0.85f;
			}
			if (num3 == 4f)
			{
				num2 = 0.8f;
			}
			if (num3 == 5f)
			{
				num2 = 0.75f;
			}
			if (num3 == 6f)
			{
				num2 = 0.7f;
			}
			if (num3 == 7f)
			{
				num2 = 0.65f;
			}
			if (num3 == 8f)
			{
				num2 = 0.6f;
			}
			if (num3 == 9f)
			{
				num2 = 0.55f;
			}
			if (num3 == 10f)
			{
				num2 = 0.5f;
			}
			if (num3 == 11f)
			{
				num2 = 0.45f;
			}
			if (num3 == 12f)
			{
				num2 = 0.4f;
			}
			if (num3 == 13f)
			{
				num2 = 0.35f;
			}
			if (num3 == 14f)
			{
				num2 = 0.3f;
			}
			if (num3 == 15f)
			{
				num2 = 0.25f;
			}
			if (num3 == 16f)
			{
				num2 = 0.2f;
			}
			if (num3 == 17f)
			{
				num2 = 0.15f;
			}
			if (num3 == 18f)
			{
				num2 = 0.1f;
			}
			if (num3 >= 19f)
			{
				num2 = 0.05f;
			}
		}
		if (cost > 0f)
		{
			cost *= num2;
			if (cost < 1f)
			{
				cost = 1f;
			}
		}
		return Mathf.Round(cost);
	}

	// Token: 0x06001247 RID: 4679 RVA: 0x00162F80 File Offset: 0x00161180
	public void CheckSlotCosts()
	{
		for (int i = 0; i < this.items.Count; i++)
		{
			this.localImg = this.items[i].gridImg;
			float num = 0f;
			int buyPrice = this.items[i].invRow._BuyPrice;
			if (!this.items[i].invRow._NeedFullStack)
			{
				num *= (float)this.items[i].stackSize;
			}
			if (this.items[i].durability.w < 0f)
			{
				num = this.items[i].durability.w * -1f;
			}
			if (this.items[i].socketA.x > 0f)
			{
				num += this.items[i].socketB.y;
			}
			bool flag = num <= (float)Links.x.inventory.emeralds;
			this.gridImg = this.bag.gridImg;
			this.gridState = this.bag.gridState;
			for (int j = 0; j < this.localImg.Count; j++)
			{
				if (flag)
				{
					this.gridImg[this.localImg[j]].sprite = Links.x.inventory.filledSprite;
				}
				else
				{
					this.gridImg[this.localImg[j]].sprite = Links.x.inventory.filledUnavailableSprite;
				}
				this.gridState[this.localImg[j]] = true;
			}
		}
	}

	// Token: 0x06001248 RID: 4680 RVA: 0x00163144 File Offset: 0x00161344
	public string GetMerchantText(string t)
	{
		string text;
		if (t == "Sell")
		{
			text = "My thanks";
		}
		else if (t == "Buy")
		{
			int num = Random.Range(0, 8);
			if (num == 0)
			{
				text = "A good specimen";
			}
			else if (num == 1)
			{
				text = "Thanks for your business";
			}
			else if (num == 2)
			{
				text = "I'll take that off your hands";
			}
			else if (num == 3)
			{
				text = "A fine piece";
			}
			else if (num == 4)
			{
				text = "My thanks";
			}
			else if (num == 5)
			{
				text = "An easy resell";
			}
			else if (num == 6)
			{
				text = "I can find a buyer for this";
			}
			else
			{
				text = "Why not?";
			}
		}
		else if (t == "Contraband")
		{
			text = "I'll not buy ill-gained wares";
		}
		else if (t == "PropertyLazure")
		{
			text = "I cannot buy property of House Lazure";
		}
		else if (t == "MerchantGoods")
		{
			text = "Maybe something more unique?";
		}
		else if (t == "Book")
		{
			text = "I don't buy these";
		}
		else if (t == "Full Stack")
		{
			text = "I'll only buy a full set";
		}
		else
		{
			text = "You'll need more emeralds for that";
		}
		return text;
	}

	// Token: 0x06001249 RID: 4681 RVA: 0x00163264 File Offset: 0x00161464
	public void UpdateMerchant()
	{
		if (!this.setup)
		{
			if (this.merchantCharacter)
			{
				if (!this.onlySelling)
				{
					this.merchantCharacter.stats.savedMerchantTime = Links.x.gameplay.gameDays;
				}
				this.str.Clear();
				int num = this.items.Count;
				for (int i = 0; i < num; i++)
				{
					Item item = this.items[i];
					if (item)
					{
						if (i != 0)
						{
							this.str.Append(",");
						}
						this.str.Append(item.invRow._Name);
						this.str.Append(",");
						this.str.Append(item.stackSize);
						this.str.Append(",");
						this.str.Append(item.socketA.x);
						this.str.Append(",");
						this.str.Append(item.socketA.y);
						this.str.Append(",");
						this.str.Append(item.socketA.z);
						this.str.Append(",");
						this.str.Append(item.socketB.x);
						this.str.Append(",");
						this.str.Append(item.socketB.y);
						this.str.Append(",");
						this.str.Append(item.socketB.z);
						this.str.Append(",");
						this.str.Append(item.durability.x);
						this.str.Append(",");
						if (item.durability.y == 0f)
						{
							item.durability.y = (float)Links.x.gameplay.gameDays;
						}
						this.str.Append(item.durability.y);
						this.str.Append(",");
						this.str.Append(item.durability.z);
					}
				}
				this.savedMerchantList = this.str.ToString();
				this.merchantCharacter.stats.savedMerchantList = this.savedMerchantList;
				this.str.Clear();
				num = this.history.Count;
				for (int j = 0; j < num; j++)
				{
					if (j > 0)
					{
						this.str.Append(",");
					}
					this.str.Append(this.history[j]);
					this.str.Append(",");
					this.str.Append(this.historyCounts[j]);
				}
				this.savedMerchantHistory = this.str.ToString();
				this.merchantCharacter.stats.savedMerchantHistory = this.savedMerchantHistory;
				if (this.isBoro())
				{
					Records.x.boroBag = this.savedMerchantList;
					return;
				}
			}
			else if (this.isBoat)
			{
				this.creature.UpdateItems();
			}
		}
	}

	// Token: 0x0600124A RID: 4682 RVA: 0x001635D0 File Offset: 0x001617D0
	public void CloseBag()
	{
		this.UpdateMerchant();
		int num = this.items.Count;
		for (int i = 0; i < num; i++)
		{
			if (this.items[i])
			{
				this.items[i].DestroyItem(false, false);
			}
		}
		if (this.gridImg == null)
		{
			this.gridImg = this.bag.gridImg;
			this.gridState = this.bag.gridState;
		}
		num = this.gridImg.Count;
		for (int j = 0; j < this.gridImg.Count; j++)
		{
			this.gridImg[j].sprite = this.bag.emptySprite;
			this.gridState[j] = false;
		}
		this.items.Clear();
		this.isBoat = false;
	}

	// Token: 0x0600124B RID: 4683 RVA: 0x001636A8 File Offset: 0x001618A8
	public bool CanSellContraband()
	{
		return this.merchantCharacter && this.merchantCharacter.stats.xmlName == "RozafirUndergroundMerchant";
	}

	// Token: 0x0600124C RID: 4684 RVA: 0x001636D8 File Offset: 0x001618D8
	public void Unload()
	{
		this.merchantCharacter = Links.x.gaia.boro;
		this.SetUpItems();
		List<Item> list = new List<Item>();
		List<bool> list2 = new List<bool>();
		for (int i = 0; i < this.items.Count; i++)
		{
			list2.Add(false);
		}
		for (int j = 0; j < this.items.Count; j++)
		{
			float num = 0f;
			int num2 = -1;
			for (int k = 0; k < this.items.Count; k++)
			{
				if (!list2[k])
				{
					float weight = this.items[k].invRow._Weight;
					if (weight > num)
					{
						num = weight;
						num2 = k;
					}
				}
			}
			if (num2 > -1)
			{
				list2[num2] = true;
				list.Add(this.items[num2]);
			}
		}
		bool flag = false;
		int count = list.Count;
		int num3 = -1;
		if (Links.x.main.node != null)
		{
			num3 = (int)Links.x.main.node.Area;
		}
		for (int l = 0; l < count; l++)
		{
			if (!flag)
			{
				for (int m = 0; m < list.Count; m++)
				{
					if (!flag && list[m])
					{
						Item item = list[m];
						if (Records.x.editor)
						{
							string text = "Removing ";
							Item item2 = item;
							Debug.Log(text + ((item2 != null) ? item2.ToString() : null));
						}
						this.RemoveItem(item);
						Item item3 = Links.x.inventory.groundBag.DropLoot(item.ID, item.stackSize, item.socketA, item.socketB, item.durability, Links.x.main, Links.x.main.tr.position + new Vector3(Random.Range(-1f, 1f), 0f, Random.Range(-1f, 1f)), true, false, num3);
						if (item3)
						{
							Vector3 vector = Links.x.main.tr.position + new Vector3(Random.Range(-2f, 2f), 0f, Random.Range(-2f, 2f));
							RaycastHit raycastHit;
							if (Physics.Linecast(vector + new Vector3(0f, 10f, 0f), vector - new Vector3(0f, 20f, 0f), out raycastHit, 4194305))
							{
								item3.tr.position = raycastHit.point;
							}
							else
							{
								item3.tr.position = new Vector3(Random.Range(-1f, 1f), 0f, Random.Range(-1f, 1f));
							}
						}
						item.DestroyItem(false, false);
						Records.x.boroWeight = 0f;
						int count2 = this.items.Count;
						for (int n = 0; n < count2; n++)
						{
							Item item4 = this.items[n];
							if (item4)
							{
								Records.x.boroWeight += item4.invRow._Weight;
							}
						}
						if (Records.x.boroWeight < Records.x.BoroMaxWeight())
						{
							flag = true;
						}
					}
				}
			}
		}
		this.CloseBag();
		this.merchantCharacter = null;
	}

	// Token: 0x0600124D RID: 4685 RVA: 0x00163A78 File Offset: 0x00161C78
	public void SetUpItems()
	{
		this.bag.End();
		this.setup = true;
		this.bag.Setup();
		int num;
		if (this.merchantCharacter)
		{
			this.savedMerchantList = this.merchantCharacter.stats.savedMerchantList;
			num = this.merchantCharacter.stats.savedMerchantTime;
			this.savedMerchantHistory = this.merchantCharacter.stats.savedMerchantHistory;
			if (!this.merchantCharacter.stats.xmlName.Contains("Peddler") && !this.merchantCharacter.stats.xmlName.Contains("Merchant") && !this.merchantCharacter.isBoro)
			{
				this.onlySelling = true;
			}
		}
		else
		{
			this.savedMerchantList = this.creature.savedItems;
			num = 0;
			this.savedMerchantHistory = "";
		}
		this.itemIDs.Clear();
		if (this.isBoro())
		{
			Records.x.boroWeight = 0f;
		}
		if (this.onlySelling && this.merchantCharacter.stats.xmlName == "RozafirGardenerChanter" && (this.savedMerchantList == "" || this.savedMerchantList == null || Links.x.gameplay.gameDays > num || num <= 0))
		{
			this.savedMerchantList = "GhostScrollCirclet,1,0,0,0,0,0,0,0,0,0,GhostScrollCirclet,1,0,0,0,0,0,0,0,0,0,GhostScrollCirclet,1,0,0,0,0,0,0,0,0,0,GhostScrollCirclet,1,0,0,0,0,0,0,0,0,0,GhostScrollCirclet,1,0,0,0,0,0,0,0,0,0,GhostScrollCirclet,1,0,0,0,0,0,0,0,0,0,GhostScrollCirclet,1,0,0,0,0,0,0,0,0,0,GhostScrollCirclet,1,0,0,0,0,0,0,0,0,0";
		}
		if (this.savedMerchantList != "" && this.savedMerchantList != null)
		{
			char c = ',';
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			int num5 = 0;
			int num6 = 0;
			int num7 = 0;
			int num8 = 0;
			int num9 = -1;
			int num10 = 0;
			int num11 = 0;
			string[] array = new string[0];
			array = this.savedMerchantList.Split(c, StringSplitOptions.None);
			int num12 = array.Length;
			int num13 = 0;
			for (int i = 0; i < num12; i++)
			{
				if (num13 == 0)
				{
					int num14 = 0;
					if (array[i] != "" && array[i] != null)
					{
						bool flag = true;
						string text = array[0];
						if (text == "0" || text == "1" || text == "2" || text == "3" || text == "4" || text == "5" || text == "6" || text == "7" || text == "8" || text == "9")
						{
							flag = false;
						}
						else
						{
							string text2 = array[i];
							Library.Inventory inventory = Links.x.library.GetInvRowFromName(text2);
							if (inventory != null)
							{
								num14 = inventory._ID;
							}
						}
						if (num14 > 2)
						{
							Library.Inventory inventory = Links.x.library.GetInvRowByIndex(num14);
							if (Links.x.gameplay.gameDays > num + 2 && !Links.x.merchant.isBoro() && !this.onlySelling)
							{
								flag = inventory._NeverSell;
							}
							int num15;
							int.TryParse(array[i + 9], out num15);
							if (Links.x.gameplay.gameDays >= num15 + 1 && !Links.x.merchant.isBoro() && num15 >= 1 && !this.onlySelling && !this.isBoat)
							{
								flag = false;
							}
							if (flag)
							{
								int.TryParse(array[i + 1], out num2);
								int.TryParse(array[i + 2], out num3);
								int.TryParse(array[i + 3], out num4);
								int.TryParse(array[i + 4], out num5);
								int.TryParse(array[i + 5], out num6);
								int.TryParse(array[i + 6], out num7);
								int.TryParse(array[i + 7], out num8);
								int.TryParse(array[i + 8], out num9);
								int.TryParse(array[i + 9], out num10);
								int.TryParse(array[i + 10], out num11);
								this.itemIDs.Add(num14);
								this.itemIDs.Add(num2);
								this.itemIDs.Add(num3);
								this.itemIDs.Add(num4);
								this.itemIDs.Add(num5);
								this.itemIDs.Add(num6);
								this.itemIDs.Add(num7);
								this.itemIDs.Add(num8);
								this.itemIDs.Add(num9);
								this.itemIDs.Add(num10);
								this.itemIDs.Add(num11);
							}
						}
					}
				}
				if (num13 == 10)
				{
					num13 = 0;
				}
				else
				{
					num13++;
				}
			}
			num12 = this.itemIDs.Count;
			num13 = 0;
			for (int j = 0; j < num12; j++)
			{
				if (num13 == 0)
				{
					Item item = Links.x.inventory.SpawnItem(null, false, this.itemIDs[j], this.itemIDs[j + 1], new Vector3((float)this.itemIDs[j + 2], (float)this.itemIDs[j + 3], (float)this.itemIDs[j + 4]), new Vector3((float)this.itemIDs[j + 5], (float)this.itemIDs[j + 6], (float)this.itemIDs[j + 7]), new Vector4((float)this.itemIDs[j + 8], (float)this.itemIDs[j + 9], (float)this.itemIDs[j + 10], 0f), Vector3.zero, this.bag.transform);
					item.forSale = true;
					item.backImage.gameObject.transform.SetParent(Links.x.inventory.merchantBag.transform);
					item.transform.SetAsLastSibling();
					int mainBagX = this.bag.mainBagX;
					int mainBagY = this.bag.mainBagY;
					if (Links.x.inventory.FindOpenSpace(this.bag.transform.position, item, mainBagX, mainBagY, "Secondary", Links.x.inventory.merchantBag))
					{
						this.items.Add(item);
					}
					else
					{
						item.DestroyItem(false, false);
						Debug.Log("no spot found for " + item.ID.ToString());
					}
					if (this.isBoro())
					{
						Records.x.boroWeight += item.invRow._Weight;
					}
					item.CheckStackSize();
				}
				if (num13 == 10)
				{
					num13 = 0;
				}
				else
				{
					num13++;
				}
			}
		}
		if (this.savedMerchantHistory != "" && this.savedMerchantHistory != null && !this.merchantCharacter.isBoro)
		{
			this.history.Clear();
			this.historyCounts.Clear();
			char c2 = ',';
			string[] array2 = new string[0];
			array2 = this.savedMerchantHistory.Split(c2, StringSplitOptions.None);
			int num16 = array2.Length;
			int num17 = 0;
			for (int k = 0; k < num16; k++)
			{
				if (num17 == 0)
				{
					string text3 = array2[k];
					int num18 = 0;
					if (text3 != "")
					{
						int.TryParse(array2[k + 1], out num18);
						this.historyCounts.Add(num18);
						this.history.Add(text3);
					}
				}
				if (num17 == 1)
				{
					num17 = 0;
				}
				else
				{
					num17++;
				}
			}
		}
		else
		{
			this.history.Clear();
			this.historyCounts.Clear();
			this.savedMerchantHistory = "";
		}
		this.setup = false;
		if (this.isBoro())
		{
			if (!this.boat)
			{
				this.SetWeightText();
			}
			else
			{
				Links.x.inventory.peddlerHeader.text = "Loot";
			}
			Links.x.inventory.peddlerFabric.enabled = false;
			Links.x.inventory.peddlerEmeraldBackground.sprite = Links.x.inventory.emeraldsNone;
			return;
		}
		Links.x.inventory.peddlerFabric.enabled = true;
		Links.x.inventory.peddlerEmeraldBackground.sprite = Links.x.inventory.emeraldsHigh;
		Links.x.inventory.peddlerHeader.text = "<font=Bold>" + this.merchantCharacter.stats.GetName() + "'s Cart</font>";
	}

	// Token: 0x0600124E RID: 4686 RVA: 0x00164308 File Offset: 0x00162508
	public void ReturnFromDrag(Item newItem)
	{
		int mainBagX = this.bag.mainBagX;
		int mainBagY = this.bag.mainBagY;
		bool flag = Links.x.inventory.FindOpenSpace(this.bag.transform.position, newItem, mainBagX, mainBagY, "Secondary", Links.x.inventory.merchantBag);
		Debug.Log("Found spot " + flag.ToString());
		if (!flag)
		{
			newItem.DestroyItem(false, false);
			Debug.Log("no spot found for " + newItem.ID.ToString());
		}
	}

	// Token: 0x0600124F RID: 4687 RVA: 0x001643A0 File Offset: 0x001625A0
	private void SetWeightText()
	{
		if (this.isBoro() && !this.boat)
		{
			Records.x.boroWeight = 0f;
			int count = this.items.Count;
			for (int i = 0; i < count; i++)
			{
				Item item = this.items[i];
				if (item)
				{
					Records.x.boroWeight += item.invRow._Weight;
				}
			}
			float num = Mathf.Round(Records.x.boroWeight);
			float num2 = Records.x.BoroMaxWeight();
			Links.x.inventory.peddlerHeader.text = string.Concat(new string[]
			{
				"Pack weight: <font=Bold>",
				num.ToString(),
				"/",
				num2.ToString(),
				"</font>"
			});
		}
	}

	// Token: 0x06001250 RID: 4688 RVA: 0x0016448D File Offset: 0x0016268D
	public bool isBoro()
	{
		return (!this.merchantCharacter && this.boat) || this.merchantCharacter.isBoro;
	}

	// Token: 0x04001E1C RID: 7708
	public Character merchantCharacter;

	// Token: 0x04001E1D RID: 7709
	public List<Item> items = new List<Item>();

	// Token: 0x04001E1E RID: 7710
	public List<int> itemIDs = new List<int>();

	// Token: 0x04001E1F RID: 7711
	public Bag bag;

	// Token: 0x04001E20 RID: 7712
	public List<string> history = new List<string>();

	// Token: 0x04001E21 RID: 7713
	public List<int> historyCounts = new List<int>();

	// Token: 0x04001E22 RID: 7714
	private List<int> localImg;

	// Token: 0x04001E23 RID: 7715
	private List<Image> gridImg;

	// Token: 0x04001E24 RID: 7716
	private List<bool> gridState;

	// Token: 0x04001E25 RID: 7717
	public string savedMerchantList;

	// Token: 0x04001E26 RID: 7718
	public string savedMerchantHistory;

	// Token: 0x04001E27 RID: 7719
	private StringFast str = new StringFast(64);

	// Token: 0x04001E28 RID: 7720
	private bool setup;

	// Token: 0x04001E29 RID: 7721
	public bool isBoat;

	// Token: 0x04001E2A RID: 7722
	public bool onlySelling;

	// Token: 0x04001E2B RID: 7723
	public Boat boat;

	// Token: 0x04001E2C RID: 7724
	public CreatureActions creature;
}
