using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.GOOGLE)]
    [Tooltip("Find directions between locations.")]
    public class FindDirection : FsmStateAction
    {
        [Tooltip("Coordinates of the route begins.")]
        public FsmVector2 fromCoordinates;

        [Tooltip("Title of the route begins.")]
        public FsmString fromLocation;

        [Tooltip("Coordinates of the route ends.")]
        public FsmVector2 toCoordinates;

        [Tooltip("Title of the route ends.")]
        public FsmString toLocation;

        [Tooltip("Search alternative routes.")]
        public FsmBool alternativeRoutes = false;

        [Tooltip("On request complete event.")]
        public FsmEvent OnComplete;

        [Tooltip("Response as string.")]
        [UIHint(UIHint.Variable)]
        public FsmString storeResponseString;

        [Tooltip("Response as object.")]
        [UIHint(UIHint.Variable)]
        public FsmObject storeResponseObject;

        [Tooltip("Route count.")]
        [UIHint(UIHint.Variable)]
        public FsmInt storeResponseRoutesCount;

        public override void Reset()
        {
            fromCoordinates = new FsmVector2 { UseVariable = true };
            fromLocation = new FsmString{ UseVariable = true };
            toCoordinates = new FsmVector2 { UseVariable = true };
            toLocation = new FsmString { UseVariable = true };
            alternativeRoutes = false;
            OnComplete = null;
            storeResponseString = null;
            storeResponseObject = null;
            storeResponseRoutesCount = null;
        }

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

        private void DoEnter()
        {
            if (OnlineMaps.instance == null)
            {
                Debug.LogError("Online Maps not found.");
                return;
            }

            OnlineMapsGoogleDirections.Params p = null;
            if (!fromCoordinates.IsNone && !toCoordinates.IsNone)
            {
                p = new OnlineMapsGoogleDirections.Params(
                    fromCoordinates.Value, 
                    toCoordinates.Value)
                {
                    alternatives = alternativeRoutes.Value
                };
                
            }
            else if (!fromCoordinates.IsNone && !toLocation.IsNone)
            {
                p = new OnlineMapsGoogleDirections.Params(
                    fromCoordinates.Value, 
                    toLocation.Value)
                {
                    alternatives = alternativeRoutes.Value
                };
            }
            else if (!fromLocation.IsNone && !toCoordinates.IsNone)
            {
                p = new OnlineMapsGoogleDirections.Params(
                    fromLocation.Value,
                    toCoordinates.Value)
                {
                    alternatives = alternativeRoutes.Value
                };
            }
            else if (!fromLocation.IsNone && !toLocation.IsNone)
            {
                p = new OnlineMapsGoogleDirections.Params(
                    fromLocation.Value, 
                    toLocation.Value)
                {
                    alternatives = alternativeRoutes.Value
                };
            }

            if (p == null) return;

            OnlineMapsGoogleDirections.Find(p).OnComplete += OnRequestComplete;
        }

        private void OnRequestComplete(string response)
        {
            Debug.Log(response);
            storeResponseString.Value = response;
            FindDirectionResponseWrapper wrapper = new FindDirectionResponseWrapper(OnlineMapsGoogleDirections.GetResult(response));
            storeResponseObject.Value = wrapper;
            storeResponseRoutesCount.Value = wrapper.count;
            if (OnComplete != null) Fsm.Event(OnComplete);
        }
    }
}