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

namespace SimplifyXR
{
    /// <summary>
    /// For use when externally loading the content of a Template Step
    /// </summary>
    //[DirectiveCategory(DirectiveCategories.Action, DirectiveSubCategory.StepByStep)]
    public class LoadTemplateStepContent : Actions
    {

    #if USING_TMP
            /// <summary>
            /// Text field type to use 
            /// </summary>
    #if UNITY_EDITOR
        [Tooltip("Type of text object")]
    #endif
            public TextType textType;
    #endif

        /// <summary>
        /// Text field for title of the step
        /// </summary>
#if UNITY_EDITOR
        [Tooltip("Text field for title of the step")]
        #endif
        public Text StepTitle;
        /// <summary>
        /// Text field for title of the step instruction
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("Text field for title of the step instruction")]
        #endif
        public Text TitleOfInstruction;
        /// <summary>
        /// Text field for instruction
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("Text field for instruction")]
        #endif
        public Text Instruction;
        /// <summary>
        /// Toggle field to display if there are notifications in step
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("Toggle field to display if there are notifications in step")]
        #endif
        public Toggle NotificationsInStep;
        /// <summary>
        /// Text fields for warnings
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("Text fields for warnings")]
        #endif
        public List<Text> WarningsToDisplay;
        /// <summary>
        /// The step to load
        /// </summary>
        protected string step;
        /// <summary>
        /// Base Step Loader
        /// </summary>
        protected ILoadStepContent stepLoader;

        public override List<KnobKeywords> ReceiveKeywords()
        {
			return new List<KnobKeywords> { new KnobKeywords("StepLabel", typeof(string)) };
        }

        public override List<KnobKeywords> SendKeywords()
        {
			return new List<KnobKeywords> { new KnobKeywords("StepLabel", typeof(string)) };
        }

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

        /// <summary>
        /// Get the step to be loaded if sent.
        /// </summary>
        protected void GetPassedStepName()
        {
            var objectPassed = GetPassableData();
            if (objectPassed == null) return;
            
            if (KeywordInUse == "StepLabel")
            {
                var _name = objectPassed as string;
                if (!string.IsNullOrEmpty(_name))
                    step = _name;
            }
        }

        /// <summary>
        /// Get the Step Loader and load it's content
        /// </summary>
        protected void LoadContent()
        {
            stepLoader = new TemplateStepLoader();
            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>
        /// Display the content in the fields
        /// </summary>
        protected void DisplayContent()
        {
            step = stepLoader.StepLabel;
			// Cast the new step loader to the correct type
			var newLoader = stepLoader as TemplateStepLoader;

            if (StepTitle != null)
            {
                if (!string.IsNullOrEmpty(newLoader.StepTitle))
                    StepTitle.text = newLoader.StepTitle;
                else
                    StepTitle.text = "";
            }

            if (TitleOfInstruction != null)
            {
                if (!string.IsNullOrEmpty(newLoader.TitleOfInstruction))
                    TitleOfInstruction.text = newLoader.TitleOfInstruction;
                else
                    TitleOfInstruction.text = "";
            }

            if (Instruction != null)
            {
                if (!string.IsNullOrEmpty(newLoader.Instructions))
                    Instruction.text = newLoader.Instructions;
                else
                    Instruction.text = "";
            }

            if (NotificationsInStep != null)
            {
                NotificationsInStep.isOn = newLoader.NotificationsInThisStep;
            }
				
            if (WarningsToDisplay != null && WarningsToDisplay.Count > 0)
			{
				var warnings = newLoader.Warnings;
				if (warnings != null && warnings.Count > 0 && WarningsToDisplay.Count >= warnings.Count)
				{
					for (int x = 0; x < WarningsToDisplay.Count; x++)
                    {
                        if (x < warnings.Count)
						    WarningsToDisplay[x].text = warnings[x];
                        else
                            WarningsToDisplay[x].text = "";
                    }
				}
				else
					foreach (var w in WarningsToDisplay)
						w.text = "";
			}
        }

        /// <summary>
        /// Pass along the loaded step name
        /// </summary>
        protected void SendStepName()
		{
            if (!string.IsNullOrEmpty(step))
            {
			    AddPassableData(new List<string> { "StepLabel" }, new List<object> { step });
            }
		}
    }
}