using OnlineMaps;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.LOCATION)]
    [Tooltip("Handle Location Service events.")]
    public class HandleLocationEvent : FsmStateAction
    {
        [Tooltip("On compass true heading changed event.")]
        public FsmEvent OnCompassChanged;

        [Tooltip("On GPS location changed event.")]
        public FsmEvent OnLocationChanged;

        [Tooltip("On GPS location inited event.")]
        public FsmEvent OnLocationInited;

        public override void Reset()
        {
            OnCompassChanged = null;
            OnLocationChanged = null;
            OnLocationInited = null;
        }

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

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

            if (!UserLocation.instance)
            {
                Debug.LogError("Online Maps Location Service not found.");
                return;
            }

            if (OnCompassChanged != null)
            {
                UserLocation.instance.OnCompassChanged += v => Fsm.Event(OnCompassChanged);
            }
            if (OnLocationChanged != null)
            {
                UserLocation.instance.OnLocationChanged += obj => Fsm.Event(OnLocationChanged);
            }
            if (OnLocationInited != null)
            {
                UserLocation.instance.OnLocationInited += () => Fsm.Event(OnLocationInited);
            }
        }
    }
}