﻿#if UNITY_EDITOR

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

namespace AC.Downloads.ClickFX
{

	public class ClickFXTemplate : Template
	{

		#region Variables

		[SerializeField] private ClickFXSpawner clickSpawnerPrefab = null;
		[SerializeField] private AnimatorController clickAnimator = null;
		[SerializeField] private Animator clickPrefab = null;
		private const string EventName = "Spawn ClickFX";

		#endregion


		#region PublicFunctions

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

			if (clickSpawnerPrefab == null || clickAnimator == null || clickPrefab == null)
			{
				errorText = "No prefab 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 }, "");

			// Animaot
			AnimatorController newAnimator = CopyAsset<AnimatorController> (installPath, clickAnimator, ".controller");
			if (newAnimator == null)
			{
				onFail.Invoke ("Prefab copy failed.");
				return;
			}

			// Click Prefab
			Animator newClickPrefab = CopyAsset<Animator> (installPath, clickPrefab, ".prefab");
			if (newClickPrefab == null)
			{
				onFail.Invoke ("Prefab copy failed.");
				return;
			}
			newClickPrefab.runtimeAnimatorController = newAnimator;
			EditorUtility.SetDirty (newClickPrefab);

			// Spawner prefab
			ClickFXSpawner newPrefab = CopyAsset<ClickFXSpawner> (installPath, clickSpawnerPrefab, ".prefab");
			if (newPrefab == null)
			{
				onFail.Invoke ("Prefab copy failed.");
				return;
			}
			newPrefab.clickPrefab = newClickPrefab;
			EditorUtility.SetDirty (newPrefab);

			ActionListAsset spawnClickPrefabActionList = CreateActionList ("Spawn ClickFX", installPath, newPrefab);

			EventSceneSwitch newEvent = new EventSceneSwitch (KickStarter.settingsManager. GetNextAvailableEventID (), EventName, spawnClickPrefabActionList, new int[] { 0 }, EventSceneSwitch.BeforeAfter.After, EventSceneSwitch.DueToLoadingSave.Either);
			KickStarter.settingsManager.events.Add (newEvent);

			EditorUtility.SetDirty (KickStarter.settingsManager);

			onComplete.Invoke ();
		}

		#endregion


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

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

		#endregion


		#region GetSet

		public override string Label { get { return "Click FX"; }}
		public override string PreviewText { get { return "Adds simple animation effects when clicking in the scene, to help give feedback to the player."; }}
		public override Type[] AffectedManagerTypes { get { return new Type[] { typeof (SettingsManager) }; }}
		public override bool RequiresInstallPath { get { return true; }}
		public override string FolderName { get { return "Click FX"; }}

		#endregion

	}


	[CustomEditor (typeof (ClickFXTemplate))]
	public class ClickFXTemplateEditor : TemplateEditor
	{}

}

#endif