﻿using OnlineMaps;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.MARKERS)]
    [Tooltip("Checks if the marker is on the map view.")]
    public class IsMarkerOnMapView : FsmStateAction
    {
        [RequiredField]
        [UIHint(UIHint.Variable)]
        [Tooltip("Instance of marker.")]
        public FsmObject marker;

        [UIHint(UIHint.Variable)]
        [Tooltip("Marker is on map view.")]
        public FsmBool storeResult;

        public override void Reset()
        {
            marker = null;
            storeResult = null;
        }

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

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

            if (marker.IsNone) return;

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

            Marker m = (marker.Value as MarkerWrapper).marker;
            storeResult.Value = m.inMapView;
        }
    }
}
