using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class TaskAdderGame : Minigame
{
	public TextRenderer PathText;

	public TaskFolder RootFolderPrefab;

	public TaskAddButton TaskPrefab;

	public Transform TaskParent;

	public List<TaskFolder> Heirarchy = new List<TaskFolder>();

	public List<Transform> ActiveItems = new List<Transform>();

	public TaskAddButton InfectedButton;

	public float folderWidth;

	public float fileWidth;

	public float lineWidth;

	public float lineHeight;

	private TaskFolder Root;

	public override void Begin(PlayerTask t)
	{
		base.Begin(t);
		this.Root = UnityEngine.Object.Instantiate<TaskFolder>(this.RootFolderPrefab, base.transform);
		this.Root.gameObject.SetActive(false);
		Dictionary<SystemTypes, TaskFolder> folders = new Dictionary<SystemTypes, TaskFolder>();
		this.PopulateRoot(this.Root, folders, ShipStatus.Instance.CommonTasks);
		this.PopulateRoot(this.Root, folders, ShipStatus.Instance.LongTasks);
		this.PopulateRoot(this.Root, folders, ShipStatus.Instance.NormalTasks);
		this.Root.SubFolders = (from f in this.Root.SubFolders
		orderby f.FolderName
		select f).ToList<TaskFolder>();
		this.ShowFolder(this.Root);
	}

	private void PopulateRoot(TaskFolder rootFolder, Dictionary<SystemTypes, TaskFolder> folders, NormalPlayerTask[] taskList)
	{
		foreach (NormalPlayerTask normalPlayerTask in taskList)
		{
			SystemTypes systemTypes = normalPlayerTask.StartAt;
			if (normalPlayerTask is DivertPowerTask)
			{
				systemTypes = ((DivertPowerTask)normalPlayerTask).TargetSystem;
			}
			if (systemTypes == SystemTypes.LowerEngine)
			{
				systemTypes = SystemTypes.UpperEngine;
			}
			TaskFolder taskFolder;
			if (!folders.TryGetValue(systemTypes, out taskFolder))
			{
				taskFolder = (folders[systemTypes] = UnityEngine.Object.Instantiate<TaskFolder>(this.RootFolderPrefab, base.transform));
				taskFolder.gameObject.SetActive(false);
				if (systemTypes == SystemTypes.UpperEngine)
				{
					taskFolder.FolderName = "Engines";
				}
				else
				{
					taskFolder.FolderName = DestroyableSingleton<TranslationController>.Instance.GetString(systemTypes);
				}
				rootFolder.SubFolders.Add(taskFolder);
			}
			taskFolder.Children.Add(normalPlayerTask);
		}
	}

	public void GoToRoot()
	{
		this.Heirarchy.Clear();
		this.ShowFolder(this.Root);
	}

	public void GoUpOne()
	{
		if (this.Heirarchy.Count > 1)
		{
			TaskFolder taskFolder = this.Heirarchy[this.Heirarchy.Count - 2];
			this.Heirarchy.RemoveAt(this.Heirarchy.Count - 1);
			this.Heirarchy.RemoveAt(this.Heirarchy.Count - 1);
			this.ShowFolder(taskFolder);
		}
	}

	public void ShowFolder(TaskFolder taskFolder)
	{
		StringBuilder stringBuilder = new StringBuilder(64);
		this.Heirarchy.Add(taskFolder);
		for (int i = 0; i < this.Heirarchy.Count; i++)
		{
			stringBuilder.Append(this.Heirarchy[i].FolderName);
			stringBuilder.Append("\\");
		}
		this.PathText.Text = stringBuilder.ToString();
		for (int j = 0; j < this.ActiveItems.Count; j++)
		{
			UnityEngine.Object.Destroy(this.ActiveItems[j].gameObject);
		}
		this.ActiveItems.Clear();
		float num = 0f;
		float num2 = 0f;
		for (int k = 0; k < taskFolder.SubFolders.Count; k++)
		{
			TaskFolder taskFolder2 = UnityEngine.Object.Instantiate<TaskFolder>(taskFolder.SubFolders[k], this.TaskParent);
			taskFolder2.gameObject.SetActive(true);
			taskFolder2.Parent = this;
			taskFolder2.transform.localPosition = new Vector3(num, num2, 0f);
			taskFolder2.transform.localScale = Vector3.one;
			num += this.folderWidth;
			if (num > this.lineWidth)
			{
				num = 0f;
				num2 += this.lineHeight;
			}
			this.ActiveItems.Add(taskFolder2.transform);
		}
		List<PlayerTask> list = (from t in taskFolder.Children
		orderby t.TaskType.ToString()
		select t).ToList<PlayerTask>();
		for (int l = 0; l < list.Count; l++)
		{
			TaskAddButton taskAddButton = UnityEngine.Object.Instantiate<TaskAddButton>(this.TaskPrefab);
			taskAddButton.MyTask = list[l];
			if (taskAddButton.MyTask.TaskType == TaskTypes.DivertPower)
			{
				SystemTypes targetSystem = ((DivertPowerTask)taskAddButton.MyTask).TargetSystem;
				taskAddButton.Text.Text = DestroyableSingleton<TranslationController>.Instance.GetString(StringNames.DivertPowerTo, Array.Empty<object>()) + " " + DestroyableSingleton<TranslationController>.Instance.GetString(targetSystem);
			}
			else
			{
				taskAddButton.Text.Text = DestroyableSingleton<TranslationController>.Instance.GetString(taskAddButton.MyTask.TaskType);
			}
			this.AddFileAsChild(taskAddButton, ref num, ref num2);
		}
		if (this.Heirarchy.Count == 1)
		{
			TaskAddButton taskAddButton2 = UnityEngine.Object.Instantiate<TaskAddButton>(this.InfectedButton);
			taskAddButton2.Text.Text = "Be_Impostor.exe";
			this.AddFileAsChild(taskAddButton2, ref num, ref num2);
		}
	}

	private void AddFileAsChild(TaskAddButton item, ref float xCursor, ref float yCursor)
	{
		item.transform.SetParent(this.TaskParent);
		item.transform.localPosition = new Vector3(xCursor, yCursor, 0f);
		item.transform.localScale = Vector3.one;
		xCursor += this.fileWidth;
		if (xCursor > this.lineWidth)
		{
			xCursor = 0f;
			yCursor -= this.lineHeight;
		}
		this.ActiveItems.Add(item.transform);
	}
}