﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

namespace SimplifyXR
{
    /// <summary>
    /// For use when externally loading the content of an S1000D Step
    /// </summary>
    [DirectiveCategory(DirectiveCategories.Action, DirectiveSubCategory.StepByStep, 
		prettyName ="Load S1000D Step Content",
		directiveInfo="This action will load <b>S1000D Step</b> content into <b>Text</b> or " +
		" <b>Text Mesh Pro</b> components.")]
	public class LoadS1000DStepContent : LoadBasicStepContent
	{
        /// <summary>
        /// Toggle field to display if there is AR in step
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("Toggle field to display if there is AR in step")]
        #endif
        public Toggle aRInThisStep;
        /// <summary>
        /// Text fields for tools
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("Text fields for tools")]
        #endif
        public Text[] ToolsInStep;

		public override void Execute()
		{
            GetPassedStepName();
            LoadContent();
			SendStepName();
            step = null;
            ThisActionCompleted();
		}

    	/// <summary>
        /// Get the Step Loader and load it's content
        /// </summary>
		protected new void LoadContent()
        {
			stepLoader = new S1000DStepLoader();
            if (stepLoader.StepTypeCheck())
            {
				if (!string.IsNullOrEmpty(step))
				{
					if (stepLoader.LoadStepContent(step))
						SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.DeveloperDeepDebug, "Loading content from Step Loader for step: {0}", SimplifyXRDebug.Args(step));
					else
					{
						SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.Warning, "Could not load content from Step Loader for step: {0}", SimplifyXRDebug.Args(step));
						return;
					}
				}
				else
				{
					if (stepLoader.LoadStepContent())
						SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.DeveloperDeepDebug, "Loading content from Step Loader for the current step in the Step Manager.");
					else
					{
						SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.Warning, "Could not load content from Step Loader for the current step in the Step Manager.");
						return;
					}
				}

				DisplayContent();
            }
            else
                SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.DeveloperDeepDebug, "Not using this Step Content Loader Action. No step content will be loaded with {0}.", SimplifyXRDebug.Args(this));
        }

		/// <summary>
		/// Show the base Step Content and the specific content for this type of Step Loader
		/// </summary>
		protected new void DisplayContent()
		{
			step = stepLoader.StepLabel;
			
			// Cast the new step loader to the correct type
			var newLoader = stepLoader as S1000DStepLoader;

			DisplayContentAbstract<S1000DStepLoader>();

			if (aRInThisStep != null)
                aRInThisStep.isOn = newLoader.ARInStep;

            if (ToolsInStep != null)
			{
				var Tools = newLoader.Tools;
				if (Tools != null && Tools.Length > 0 && ToolsInStep.Length >= Tools.Length)
				{
					for (int x = 0; x < Tools.Length; x++)
						ToolsInStep[x].text = Tools[x];
				}
				else
					foreach (var tool in ToolsInStep)
						tool.text = "";
			}
		}
	}
}