using OnlineMaps;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.GOOGLE)]
    [Tooltip("Gets route step info.")]
    public class GetRouteStepInfo : FsmStateAction
    {
        [RequiredField]
        [Tooltip("Step object.")]
        [UIHint(UIHint.Variable)]
        public FsmObject stepObject;

        [Tooltip("The total distance covered by this step.")]
        [UIHint(UIHint.Variable)]
        public FsmInt storeDistance;

        [Tooltip("The total duration of the passage of this step.")]
        [UIHint(UIHint.Variable)]
        public FsmInt storeDuration;

        [Tooltip("Location of the endpoint of this step (lng, lat).")]
        [UIHint(UIHint.Variable)]
        public FsmVector2 storeEnd;

        [Tooltip("Instructions to the current step.")]
        [UIHint(UIHint.Variable)]
        public FsmString storeInstructions;

        [Tooltip("Maneuver the current step.")]
        [UIHint(UIHint.Variable)]
        public FsmString storeManeuver;

        [Tooltip("Location of the startpoint of this step (lng, lat).")]
        [UIHint(UIHint.Variable)]
        public FsmVector2 storeStart;

        [Tooltip("Count points in step.")]
        [UIHint(UIHint.Variable)]
        public FsmInt storePointsCount;

        public override void Reset()
        {
            stepObject = null;
            storeDistance = null;
            storeDuration = null;
            storeEnd = null;
            storeInstructions = null;
            storeManeuver = null;
            storeStart = null;
            storePointsCount = null;
        }

        public override void OnEnter()
        {
            DoEnter();
            Finish();
        }

        private void DoEnter()
        {
            if (!Map.instance)
            {
                Debug.LogError("Online Maps not found.");
                return;
            }

            if (stepObject.IsNone) return;

            if (stepObject.Value.GetType() != typeof(RouteStepWrapper))
            {
                Debug.Log("Its not RouteStepWrapper instance");
                return;
            }

            RouteStepWrapper wrapper = stepObject.Value as RouteStepWrapper;

            if (wrapper.step != null)
            {
                storeDistance.Value = wrapper.step.distanceMeters;
                storeDuration.Value = wrapper.step.durationSec;
                storeEnd.Value = wrapper.step.endLocation;
                storeInstructions.Value = wrapper.step.navigationInstruction.instructions;
                storeManeuver.Value = wrapper.step.navigationInstruction.maneuver;
                storeStart.Value = wrapper.step.startLocation;
                storePointsCount.Value = wrapper.step.polyline.points.Length;
            }
            else if (wrapper.orsStep != null)
            {
                storeDistance.Value = (int)wrapper.orsStep.distance;
                storeDuration.Value = (int)wrapper.orsStep.duration;
                storeInstructions.Value = wrapper.orsStep.instruction;
            }
        }
    }
}