﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC.Downloads.TextAdventure
{

	public class ActionInventoryList : Action, ITranslatable
	{

		public int passToParameterID;
		public string emptyText = "nothing";
		public int emptyTextLineID = -1;
		protected ActionParameter stringParameter;


		public override ActionCategory Category { get { return ActionCategory.Custom; }}
		public override string Title { get { return "Template"; }}
		public override string Description { get { return "This is a blank Action template."; }}


		public override void AssignValues (List<ActionParameter> parameters)
		{
			stringParameter = GetParameterWithID (parameters, passToParameterID);
		}


		public override float Run ()
		{
			if (stringParameter != null)
			{
				var items = KickStarter.runtimeInventory.PlayerInvCollection.InvItems;
				if (items.Count == 0)
				{
					stringParameter.SetValue (emptyText);
					return 0f;
				}

				string listText = string.Empty;
				for (int i = 0; i < items.Count; i++)
				{
					listText += items[i].GetLabel (Options.GetLanguage ());
					if (i == items.Count - 1)
					{
						listText += ".";
					}
					else
					{
						listText += ", ";
					}
				}
				stringParameter.SetValue (listText);
				return 0f;
			}
			LogWarning ("No String parameter found");
			return 0f;
		}


		#if UNITY_EDITOR

		public override void ShowGUI (List<ActionParameter> parameters)
		{
			emptyText = EditorGUILayout.TextField ("Empty text:", emptyText);

			passToParameterID = ChooseParameterGUI ("Send to parameter:", parameters, passToParameterID, new ParameterType[] { ParameterType.String });
			if (parameters.Count == 0)
			{
				EditorGUILayout.HelpBox ("No parameters found.", MessageType.Warning);
			}
		}

		#endif


		public static ActionInventoryList CreateNew (int passToParameterID)
		{
			ActionInventoryList newAction = CreateNew<ActionInventoryList> ();
			newAction.passToParameterID = passToParameterID;
			return newAction;
		}


		#region ITranslatable

		public string GetTranslatableString (int index)
		{
			return emptyText;
		}


		public int GetTranslationID (int index)
		{
			return emptyTextLineID;
		}


		#if UNITY_EDITOR

		public void UpdateTranslatableString (int index, string updatedText)
		{
			emptyText = updatedText;
		}


		public int GetNumTranslatables ()
		{
			return 1;
		}


		public bool HasExistingTranslation (int index)
		{
			return emptyTextLineID > 0;
		}


		public void SetTranslationID (int index, int _lineID)
		{
			emptyTextLineID = _lineID;
		}


		public string GetOwner (int index)
		{
			return string.Empty;
		}


		public bool OwnerIsPlayer (int index)
		{
			return false;
		}


		public AC_TextType GetTranslationType (int index)
		{
			return AC_TextType.Custom;
		}


		public bool CanTranslate (int index)
		{
			return !string.IsNullOrEmpty (emptyText);
		}

		#endif

		#endregion

	}

}