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

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.GOOGLE)]
    [Tooltip("Gets points from route.")]
    public class GetRoutePoints : FsmStateAction
    {
        [RequiredField]
        [Tooltip("Route object.")]
        [UIHint(UIHint.Variable)]
        public FsmObject routeObject;

        [Tooltip("Points object.")]
        [UIHint(UIHint.Variable)]
        public FsmObject storePoints;

        [Tooltip("Count points.")]
        [UIHint(UIHint.Variable)]
        public FsmInt storeCountPoints;

        public override void Reset()
        {
            routeObject = null;
            storePoints = null;
            storeCountPoints = null;
        }

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

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

            if (routeObject.IsNone) return;

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

            RouteWrapper wrapper = routeObject.Value as RouteWrapper;
            if (wrapper.steps != null) storePoints.Value = new RoutePointsWrapper(wrapper.steps.SelectMany(s => s.polyline.points).ToList());
            else if (wrapper.orsPoints != null) storePoints.Value = new RoutePointsWrapper(wrapper.orsPoints);
            storeCountPoints.Value = (storePoints.Value as RoutePointsWrapper).count;
        }
    }
}