﻿using System.Linq;
using OnlineMaps;
using OnlineMaps.Webservices;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.GOOGLE)]
    [Tooltip("Find directions between locations using Open Route Service Directions API.")]
    public class OpenRouteService : FsmStateAction
    {
        [Tooltip("Open Route Service API key.")]
        public FsmString apiKey;

        [Tooltip("Coordinates of the route points.")]
        public Vector2[] coordinates;

        [Tooltip("The route profile.")]
        public ORSDirectionsRequest.Profile profile = ORSDirectionsRequest.Profile.drivingCar;

        [Tooltip("Language for the route instructions.")]
        public FsmString language = "en";

        [Tooltip("The distance unit.")]
        public FsmString units = "km";

        [Tooltip("Specifies whether to return instructions.")]
        public FsmBool instructions = true;

        [Tooltip("Specifies whether to return elevation values for points.")]
        public FsmBool elevation = 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()
        {
            coordinates = new Vector2[0];
            elevation = false;
            language = "en";
            units = "km";
            instructions = true;
            profile = ORSDirectionsRequest.Profile.drivingCar;

            OnComplete = null;
            storeResponseString = null;
            storeResponseObject = null;
            storeResponseRoutesCount = null;
        }

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

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

            if (coordinates == null || coordinates.Length < 2) return;

            GeoPoint[] locations = coordinates.Cast<GeoPoint>().ToArray();
            new ORSDirectionsRequest(locations[0], locations.Last())
            {
                elevation = elevation.Value,
                language = language.Value,
                units = units.Value,
                instructions = instructions.Value,
                profile = profile
            }.HandleComplete(s =>
            {
                Debug.Log(s);
                storeResponseString.Value = s;

                OpenRouteServiceDirectionsWrapper wrapper = new OpenRouteServiceDirectionsWrapper(s);
                storeResponseRoutesCount.Value = wrapper.count;

                if (OnComplete != null) Fsm.Event(OnComplete);
            }).Send();
        }
    }
}