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

// Token: 0x020000E1 RID: 225
public class UndergroundMap : MonoBehaviour
{
	// Token: 0x060014DA RID: 5338 RVA: 0x00192880 File Offset: 0x00190A80
	public void Setup()
	{
		this.Load();
		for (int i = 0; i < this.pieces.Length; i++)
		{
			UndergroundMapPiece.MapPieceType type = this.pieces[i].type;
			if (type == UndergroundMapPiece.MapPieceType.Map || type == UndergroundMapPiece.MapPieceType.Passage || type == UndergroundMapPiece.MapPieceType.Exit)
			{
				if (type == UndergroundMapPiece.MapPieceType.Map)
				{
					Image component = this.pieces[i].gameObject.transform.GetChild(0).gameObject.GetComponent<Image>();
					this.imgHighlight.Add(component);
					component.enabled = false;
				}
				else
				{
					this.imgHighlight.Add(null);
				}
				if (this.states[i] == 0)
				{
					this.pieces[i].gameObject.SetActive(false);
				}
				else
				{
					this.pieces[i].gameObject.SetActive(true);
				}
			}
			else
			{
				this.imgHighlight.Add(null);
				if (this.states[i] == 1)
				{
					if (type == UndergroundMapPiece.MapPieceType.QuestOnUntilState)
					{
						this.pieces[i].gameObject.SetActive(false);
					}
					if (type == UndergroundMapPiece.MapPieceType.QuestOffUntilState)
					{
						this.pieces[i].gameObject.SetActive(true);
					}
				}
				else
				{
					this.CheckQuest(i);
				}
			}
		}
	}

	// Token: 0x060014DB RID: 5339 RVA: 0x00192998 File Offset: 0x00190B98
	private void CheckQuest(int i)
	{
		if (this.pieces[i].type == UndergroundMapPiece.MapPieceType.QuestOnUntilState && this.pieces[i].quest1 != Creatures.Adventures.None)
		{
			QuestState questState = QuestLog.GetQuestState(this.pieces[i].quest1.ToString());
			QuestState questState2 = Records.x.StringToQuestState(this.pieces[i].turnOffWhenThisState.ToString());
			if (questState == questState2)
			{
				this.states[i] = 1;
				this.pieces[i].gameObject.SetActive(false);
			}
			else
			{
				this.pieces[i].gameObject.SetActive(true);
			}
		}
		if (this.pieces[i].type == UndergroundMapPiece.MapPieceType.QuestOffUntilState && this.pieces[i].quest2 != Creatures.Adventures.None)
		{
			QuestState questState3 = QuestLog.GetQuestState(this.pieces[i].quest2.ToString());
			QuestState questState4 = Records.x.StringToQuestState(this.pieces[i].turnOnWhenThisState.ToString());
			if (questState3 == questState4)
			{
				this.states[i] = 1;
				this.pieces[i].gameObject.SetActive(true);
				return;
			}
			this.pieces[i].gameObject.SetActive(false);
		}
	}

	// Token: 0x060014DC RID: 5340 RVA: 0x00192AD8 File Offset: 0x00190CD8
	public void ChangedArea(string areaName)
	{
		for (int i = 0; i < this.pieces.Length; i++)
		{
			if (this.pieces[i].type == UndergroundMapPiece.MapPieceType.Map)
			{
				Image image = this.imgHighlight[i];
				if (areaName == this.pieces[i].gameObject.name)
				{
					image.enabled = false;
					if (this.pieces[i].useSoundMapAlpha)
					{
						RaycastHit raycastHit;
						if (Links.x.main && Physics.Raycast(Links.x.main.tr.position, Links.x.rtsCamera.tr.forward, out raycastHit, 600f, 16777216))
						{
							Vector2 textureCoord = raycastHit.textureCoord;
							textureCoord.x *= (float)Links.x.sensory.terrainTextureWidthInt;
							textureCoord.y *= (float)Links.x.sensory.terrainTextureHeightInt;
							float num = (float)Links.x.sensory.GetPixelColorForUndergroundMap((int)textureCoord.x, (int)textureCoord.y).a / 255f;
							float num2 = this.pieces[i].matchingAlphaValue / 100f;
							if (num < num2 + 0.01f && num > num2 - 0.01f)
							{
								this.states[i] = 1;
								this.pieces[i].gameObject.SetActive(true);
								image.enabled = true;
							}
						}
					}
					else
					{
						this.states[i] = 1;
						this.pieces[i].gameObject.SetActive(true);
						image.enabled = true;
					}
				}
				else
				{
					image.enabled = false;
				}
			}
		}
	}

	// Token: 0x060014DD RID: 5341 RVA: 0x00192C94 File Offset: 0x00190E94
	public void UsedPreviousDoor(string mapName, string subExit)
	{
		for (int i = 0; i < this.pieces.Length; i++)
		{
			if (this.pieces[i].type == UndergroundMapPiece.MapPieceType.Exit && mapName == this.pieces[i].exitToName.ToString() && subExit == this.pieces[i].subExitName.ToString())
			{
				this.states[i] = 1;
				this.pieces[i].gameObject.SetActive(true);
			}
		}
	}

	// Token: 0x060014DE RID: 5342 RVA: 0x00192D24 File Offset: 0x00190F24
	public void UsedDoor(MapExit exit)
	{
		bool flag = false;
		if (exit.exitTo != MapExit.exitToNames.Interiors && exit.exitTo != MapExit.exitToNames.Exterior)
		{
			flag = true;
		}
		for (int i = 0; i < this.pieces.Length; i++)
		{
			if (this.pieces[i].type == UndergroundMapPiece.MapPieceType.Passage && !flag && exit.interiorConnection == this.pieces[i].interiorExitName)
			{
				this.states[i] = 1;
				this.pieces[i].gameObject.SetActive(true);
			}
			if (this.pieces[i].type == UndergroundMapPiece.MapPieceType.Exit && flag && exit.exitTo == this.pieces[i].exitToName && exit.subExit == this.pieces[i].subExitName)
			{
				this.states[i] = 1;
				this.pieces[i].gameObject.SetActive(true);
			}
		}
	}

	// Token: 0x060014DF RID: 5343 RVA: 0x00192E08 File Offset: 0x00191008
	public void UpdateQuests()
	{
		for (int i = 0; i < this.pieces.Length; i++)
		{
			if (this.pieces[i].type == UndergroundMapPiece.MapPieceType.QuestOnUntilState)
			{
				this.CheckQuest(i);
			}
			if (this.pieces[i].type == UndergroundMapPiece.MapPieceType.QuestOffUntilState)
			{
				this.CheckQuest(i);
			}
		}
	}

	// Token: 0x060014E0 RID: 5344 RVA: 0x00192E58 File Offset: 0x00191058
	public void Save()
	{
		Records.x.openBook + "/UndergroundMaps.es3";
		this.saveFile.Save<List<int>>("States " + Links.x.diorama.sceneName, this.states);
		this.saveFile.Sync();
	}

	// Token: 0x060014E1 RID: 5345 RVA: 0x00192EB0 File Offset: 0x001910B0
	public void Load()
	{
		string text = Records.x.openBook + "/UndergroundMaps.es3";
		this.saveFile = new ES3File(text);
		if (ES3.FileExists(text))
		{
			string text2 = "States " + Links.x.diorama.sceneName;
			if (this.saveFile.KeyExists(text2))
			{
				this.states = this.saveFile.Load<List<int>>(text2);
			}
		}
		if (this.states.Count != this.pieces.Length)
		{
			for (int i = 0; i < this.pieces.Length; i++)
			{
				if (i >= this.states.Count)
				{
					this.states.Add(0);
				}
			}
		}
	}

	// Token: 0x04002437 RID: 9271
	public UndergroundMapPiece[] pieces;

	// Token: 0x04002438 RID: 9272
	public List<int> states = new List<int>();

	// Token: 0x04002439 RID: 9273
	private List<Image> imgHighlight = new List<Image>();

	// Token: 0x0400243A RID: 9274
	private ES3File saveFile;
}
