﻿#if UNITY_EDITOR

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace AC.Downloads.TextAdventure
{

	public class TextParserTemplate : Template
	{

		#region Variables

		[SerializeField] private MenuManager menuManager = null;
		[SerializeField] private TextParser textParserPrefab = null;
		[SerializeField] private UnityEngine.Object actionsFolderObj = null;
		
		#endregion


		#region PublicFunctions

		public override bool CanInstall (ref string errorText)
		{
			if (KickStarter.menuManager == null)
			{
				errorText = "No Menu Manager assigned";
				return false;
			}

			if (KickStarter.menuManager == menuManager)
			{
				errorText = "Wrong Menu Manager assigned";
				return false;
			}

			if (KickStarter.settingsManager == null)
			{
				errorText = "No Settings Manager assigned";
				return false;
			}

			if (KickStarter.actionsManager == null)
			{
				errorText = "No Actions Manager assigned";
				return false;
			}

			if (textParserPrefab == null)
			{
				errorText = "No Text Parser prefab assigned";
				return false;
			}

			if (actionsFolderObj == null)
			{
				errorText = "No Actions folder assigned";
				return false;
			}

			return true;
		}


		public override bool CanSuggest (NGWData data)
		{
			return true;
		}

		#endregion


		#region ProtectedFunctions

		protected override void MakeChanges (string installPath, bool canDeleteOldAssets, System.Action onComplete, System.Action<string> onFail)
		{
			Undo.RecordObjects (new UnityEngine.Object[] { KickStarter.menuManager }, "");

			// Actions
			InstallActionsFolder (actionsFolderObj);

			// Prefab
			TextParser newTextParserPrefab = CopyAsset<TextParser> (installPath, textParserPrefab, ".prefab");
			if (newTextParserPrefab == null)
			{
				onFail.Invoke ("Prefab copy failed.");
				return;
			}

			// ActionLists
			ActionListAsset quitActionList = CreateQuitActionList ("Command_Quit", installPath);
			newTextParserPrefab.AddCommand ("quit", quitActionList);
			ActionListAsset listInventoryActionList = CreateListInventoryActionList ("Command_ListInventory", installPath);
			newTextParserPrefab.AddCommand ("inventory", listInventoryActionList);
			ActionListAsset actionListOnMisunderstand = CreateMisunderstandActionList ("TextParser_OnMisunderstand", installPath);
			newTextParserPrefab.actionListOnMisunderstand = actionListOnMisunderstand;
			ActionListAsset actionListOnTooFar = CreateTooFarActionList ("TextParser_OnTooFar", installPath);
			newTextParserPrefab.actionListOnTooFar = actionListOnTooFar;
			EditorUtility.SetDirty (newTextParserPrefab);

			// Menu
			CopyMenus (installPath, menuManager, KickStarter.menuManager);

			KickStarter.settingsManager.interactionMethod = AC_InteractionMethod.CustomScript;
		
			// Event
			ActionListAsset spawnTextParserActionList = CreateSpawnTextParserActionList ("SpawnTextParser", installPath, newTextParserPrefab);
			EventSceneSwitch newEvent = new EventSceneSwitch (KickStarter.settingsManager. GetNextAvailableEventID (), "On begin scene: Spawn text parser", spawnTextParserActionList, new int[] { 0 }, EventSceneSwitch.BeforeAfter.After, EventSceneSwitch.DueToLoadingSave.Either);
			KickStarter.settingsManager.events.Add (newEvent);

			EditorUtility.SetDirty (KickStarter.menuManager);
			EditorUtility.SetDirty (KickStarter.settingsManager);

			onComplete.Invoke ();
		}

		#endregion


		#region PrivateFunctions

		private ActionListAsset CreateSpawnTextParserActionList (string assetName, string installPath, TextParser newTextParserPrefab)
		{
			List<Action> actions = new List<Action>
			{
				ActionInstantiate.CreateNew_Add (newTextParserPrefab.gameObject),
			};

			ActionListAsset newAsset = ActionListAsset.CreateFromActions (assetName, installPath, actions, ActionListType.RunInBackground);
			return newAsset;
		}


		private ActionListAsset CreateQuitActionList (string assetName, string installPath)
		{
			List<Action> actions = new List<Action>
			{
				ActionEndGame.CreateNew_QuitGame (),
			};

			ActionListAsset newAsset = ActionListAsset.CreateFromActions (assetName, installPath, actions, ActionListType.RunInBackground);
			return newAsset;
		}


		private ActionListAsset CreateListInventoryActionList (string assetName, string installPath)
		{
			List<Action> actions = new List<Action>
			{
				ActionInventoryList.CreateNew (0),
				ActionSpeech.CreateNew (null, "You are carrying: [param:0].")
			};

			ActionParameter stringParameter = new ActionParameter (0);
			stringParameter.parameterType = ParameterType.String;
			stringParameter.label = "Inventory list";

			ActionListAsset newAsset = ActionListAsset.CreateFromActions (assetName, installPath, actions, ActionListType.RunInBackground);
			newAsset.useParameters = true;
			newAsset.DefaultParameters.Add (stringParameter);
			return newAsset;
		}


		private ActionListAsset CreateMisunderstandActionList (string assetName, string installPath)
		{
			List<Action> actions = new List<Action>
			{
				ActionSpeech.CreateNew (null, "Sorry, I didn't understand that."),
			};

			ActionListAsset newAsset = ActionListAsset.CreateFromActions (assetName, installPath, actions, ActionListType.RunInBackground);
			return newAsset;
		}


		private ActionListAsset CreateTooFarActionList (string assetName, string installPath)
		{
			List<Action> actions = new List<Action>
			{
				ActionSpeech.CreateNew (null, "You need to get closer."),
			};

			ActionListAsset newAsset = ActionListAsset.CreateFromActions (assetName, installPath, actions, ActionListType.RunInBackground);
			return newAsset;
		}

		#endregion


		#region GetSet

		public override string Label { get { return "Text parser"; }}
		public override string PreviewText { get { return "Adds a text parser, allowing the player to run Hotspot and Inventory interactions by typing."; }}
		public override Type[] AffectedManagerTypes { get { return new Type[] { typeof (MenuManager), typeof (SettingsManager), typeof (ActionsManager) }; }}
		public override TemplateCategory Category { get { return TemplateCategory.Interface; }}
		public override bool RequiresInstallPath { get { return true; }}
		public override string FolderName { get { return "TextParser"; }}

		#endregion

	}


	[CustomEditor (typeof (TextParserTemplate))]
	public class TextParserTemplateEditor : TemplateEditor
	{}

}

#endif