using System.Diagnostics;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace HutongGames.PlayMaker.Actions
{

	[ActionCategory("Profiler")]
	public class StopwatchGetElapsedTime : FsmStateAction
	{
	    [Tooltip("Id of stopwatch to get. Should match Id used in StopwatchStart.")]
	    public FsmString id;

        [Tooltip("Store the elapsed time in a float variable.")]
        [UIHint(UIHint.Variable)]
	    public FsmFloat storeResult;

        [Tooltip("Output result to the Unity Console.")]
	    public FsmBool logResult;

	    public override void Reset()
	    {
	        id = null;
	        storeResult = null;
	        logResult = true;
	    }

	    public override void OnEnter()
		{
		    var stopwatch = StopwatchStart.GetStopwatch(id.Value);
		    if (stopwatch != null)
		    {
		        storeResult.Value = stopwatch.ElapsedMilliseconds;

		        if (logResult.Value)
		        {
		            var output = "Elapsed Time: " + storeResult.Value;
		            if (!string.IsNullOrEmpty(id.Value))
		                output = id.Value + " " + output;

		            Debug.Log(output);
		        }
		    }

            Finish();
		}


	}

}
