﻿#if UNITY_EDITOR

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

namespace AC.Downloads.TheChamber
{

	public class TheChamberTemplate : Template
	{

		#region Variables

		[SerializeField] private SceneItem closeUpBasePrefab = null;
		[SerializeField] private Texture2D closeUpTexture = null;
		[SerializeField] private ZoomHotspot zoomHotspotPrefab = null;
		[SerializeField] private Texture2D zoomHotspotTexture = null;
		[SerializeField] private ZoomInput zoomInputPrefab = null;
		[SerializeField] private MenuManager menuManager = null;

		#endregion


		#region PublicFunctions

		public override bool CanInstall (ref string errorText)
		{
			if (closeUpBasePrefab == null)
			{
				errorText = "No Close Up prefab assigned";
				return false;
			}

			if (zoomHotspotPrefab == null)
			{
				errorText = "No Zoom Hotspot prefab assigned";
				return false;
			}

			if (zoomInputPrefab == null)
			{
				errorText = "No Zoom Input prefab assigned";
				return false;
			}

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

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

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

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

			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.settingsManager, KickStarter.variablesManager, KickStarter.sceneManager, KickStarter.menuManager }, "");

			// Prefabs
			ZoomHotspot newZoomHotspotPrefab = CopyAsset<ZoomHotspot> (installPath, zoomHotspotPrefab, ".prefab");
			if (newZoomHotspotPrefab == null)
			{
				onFail.Invoke ("Prefab copy failed.");
				return;
			}

			ZoomInput newZoomInputPrefab = CopyAsset<ZoomInput> (installPath, zoomInputPrefab, ".prefab");
			if (newZoomInputPrefab == null)
			{
				onFail.Invoke ("Prefab copy failed.");
				return;
			}

			SceneItem newCloseUpBasePrefab = CopyAsset<SceneItem> (installPath, closeUpBasePrefab, ".prefab");
			if (newCloseUpBasePrefab == null)
			{
				onFail.Invoke ("Prefab copy failed.");
				return;
			}

			// Scene prefabs
			KickStarter.sceneManager.AddPrefab (new SceneManagerPrefabData ("Logic", "Zoom Hotspot", "A Hotspot that double-clicking moves to its own camera.", zoomHotspotTexture, newZoomHotspotPrefab.gameObject));
			KickStarter.sceneManager.AddPrefab (new SceneManagerPrefabData ("Logic", "Close-up base", "A base for Inventory item close-ups.  Create one, make a prefab, and then assign as an item's 'Linked prefab'.", closeUpTexture, newCloseUpBasePrefab.gameObject));

			// Events
			ActionListAsset spawnHelpersActionList = CreateSpawnHelpersActionList ("SpawnHelperScripts", installPath, newZoomInputPrefab);
			ActionListAsset updateInventoryLockActionList = CreateUpdateInventoryLockActionList ("UpdateInventoryLock", installPath);

			EventSceneSwitch newEvent1 = new EventSceneSwitch (KickStarter.settingsManager. GetNextAvailableEventID (), "On begin scene: Spawn helper scripts", spawnHelpersActionList, new int[] { 0 }, EventSceneSwitch.BeforeAfter.After, EventSceneSwitch.DueToLoadingSave.Either);
			KickStarter.settingsManager.events.Add (newEvent1);

			EventSceneSwitch newEvent2 = new EventSceneSwitch (KickStarter.settingsManager. GetNextAvailableEventID (), "On begin scene: Update Inventory lock", updateInventoryLockActionList, new int[] { 0 }, EventSceneSwitch.BeforeAfter.After, EventSceneSwitch.DueToLoadingSave.No);
			KickStarter.settingsManager.events.Add (newEvent2);

			EventInventoryAddRemove newEvent3 = new EventInventoryAddRemove (KickStarter.settingsManager. GetNextAvailableEventID (), "On add item: Update Inventory lock", updateInventoryLockActionList, new int[] { 0 }, EventInventoryAddRemove.AddRemove.Add, -1);
			KickStarter.settingsManager.events.Add (newEvent3);

			EventInventoryAddRemove newEvent4 = new EventInventoryAddRemove (KickStarter.settingsManager. GetNextAvailableEventID (), "On remove item: Update Inventory lock", updateInventoryLockActionList, new int[] { 0 }, EventInventoryAddRemove.AddRemove.Remove, -1);
			KickStarter.settingsManager.events.Add (newEvent4);

			// Settings
			#if UNITY_ANDROID || UNITY_IOS
			KickStarter.settingsManager.inputMethod = InputMethod.TouchScreen;
			#else
			if (KickStarter.settingsManager.inputMethod == InputMethod.KeyboardOrController)
			{
				KickStarter.settingsManager.inputMethod = InputMethod.MouseAndKeyboard;
			}
			#endif
			KickStarter.settingsManager.movementMethod = MovementMethod.None;
			KickStarter.settingsManager.dragThreshold = 0.02f;
			KickStarter.settingsManager.interactionMethod = AC_InteractionMethod.ContextSensitive;
			KickStarter.settingsManager.cameraPerspective = CameraPerspective.ThreeD;
			KickStarter.settingsManager.hotspotDetection = HotspotDetection.MouseOver;
			KickStarter.settingsManager.InventoryDragDrop = true;
			KickStarter.settingsManager.inventoryDropLookNoDrag = true;
			KickStarter.settingsManager.touchScreenHotspotInput = TouchScreenHotspotInput.TouchDown;

			// Menu
			CopyMenus (installPath, menuManager, KickStarter.menuManager, canDeleteOldAssets ? ExistingMenuBehaviour.Delete : ExistingMenuBehaviour.Rename);
			
			EditorUtility.SetDirty (KickStarter.menuManager);

			onComplete.Invoke ();
		}

		#endregion


		#region PrivateFunctions
		
		private ActionListAsset CreateSpawnHelpersActionList (string assetName, string installPath, ZoomInput newZoomInputPrefab)
		{
			List<Action> actions = new List<Action>
			{
				ActionInstantiate.CreateNew_Add (newZoomInputPrefab.gameObject),
			};

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


		private ActionListAsset CreateUpdateInventoryLockActionList (string assetName, string installPath)
		{
			ActionInventoryCheck inventoryCheck = ActionInventoryCheck.CreateNew_NumberOfItemsCarrying (0, AC.ActionInventoryCheck.IntCondition.MoreThan);
			ActionMenuState unlockInventory = ActionMenuState.CreateNew_TurnOnMenu ("Inventory");
			ActionMenuState lockInventory = ActionMenuState.CreateNew_TurnOffMenu ("Inventory", true);

			List<Action> actions = new List<Action>
			{
				inventoryCheck,
				unlockInventory,
				lockInventory,
			};

			inventoryCheck.SetOutputs (new ActionEnd (unlockInventory), new ActionEnd (lockInventory));
			unlockInventory.SetOutput (new ActionEnd (true));
			lockInventory.SetOutput (new ActionEnd (true));

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

		#endregion


		#region GetSet

		public override string Label { get { return "The Chamber"; }}
		public override string PreviewText { get { return "Adds 'The Room' style zoom input and interface"; }}
		public override Type[] AffectedManagerTypes { get { return new Type[] { typeof (SettingsManager), typeof (SceneManager), typeof (MenuManager) }; }}
		public override bool RequiresInstallPath { get { return true; }}
		public override string FolderName { get { return "TheChamber"; }}

		#endregion

	}


	[CustomEditor (typeof (TheChamberTemplate))]
	public class TheChamberTemplateEditor : TemplateEditor
	{}

}

#endif