using OnlineMaps;
using OnlineMaps.Webservices;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.GOOGLE)]
    [Tooltip("Gets location info by index from FindLocation response.")]
    public class GetLocationByIndex : FsmStateAction
    {
        [RequiredField]
        [UIHint(UIHint.Variable)]
        [Tooltip("FindLocation response object")]
        public FsmObject responseObject;

        [RequiredField]
        [Tooltip("Location index")]
        public FsmInt locationIndex = 0;

        [UIHint(UIHint.Variable)]
        [Tooltip("Count of address components.")]
        public FsmInt storeAddressComponentsCount;

        [UIHint(UIHint.Variable)]
        [Tooltip("Count of types.")]
        public FsmInt storeTypesCount;

        [UIHint(UIHint.Variable)]
        [Tooltip("String containing the human-readable address of this location.")]
        public FsmString storeFormattedAddress;

        [UIHint(UIHint.Variable)]
        [Tooltip("Count of postcode localities.")]
        public FsmInt storePostcodeLocalitiesCount;

        [UIHint(UIHint.Variable)]
        [Tooltip("Geocoded latitude, longitude value.")]
        public FsmVector2 storeGeometryLocation;

        [UIHint(UIHint.Variable)]
        [Tooltip("Additional data about the specified location.")]
        public FsmString storeGeometryLocationType;

        [UIHint(UIHint.Variable)]
        [Tooltip("Recommended viewport for displaying the returned result, specified as latitude,longitude values defining the northeast corner of the viewport bounding box.")]
        public FsmVector2 storeGeometryViewportNortheast;

        [UIHint(UIHint.Variable)]
        [Tooltip("Recommended viewport for displaying the returned result, specified as latitude,longitude values defining the southwest corner of the viewport bounding box.")]
        public FsmVector2 storeGeometryViewportSouthwest;

        [UIHint(UIHint.Variable)]
        [Tooltip("Unique identifier that can be used with other Google APIs.")]
        public FsmString storePlaceID;

        [UIHint(UIHint.Variable)]
        [Tooltip("(optionally returned) Stores latitude,longitude values defining the northeast corner the bounding box which can fully contain the returned result.")]
        public FsmVector2 storeGeometryBoundsNortheast;

        [UIHint(UIHint.Variable)]
        [Tooltip("(optionally returned) Stores latitude,longitude values defining the southwest corner the bounding box which can fully contain the returned result.")]
        public FsmVector2 storeGeometryBoundsSouthwest;

        [UIHint(UIHint.Variable)]
        [Tooltip("Indicates that the geocoder did not return an exact match for the original request, though it was able to match part of the requested address.")]
        public FsmBool storePartialMatch;

        public override void Reset()
        {
            responseObject = null;
            locationIndex = 0;
            storeAddressComponentsCount = null;
            storeTypesCount = null;
            storeFormattedAddress = null;
            storePostcodeLocalitiesCount = null;
            storeGeometryLocation = null;
            storeGeometryLocationType = null;
            storeGeometryViewportNortheast = null;
            storeGeometryViewportSouthwest = null;
            storePlaceID = null;
            storeGeometryBoundsNortheast = null;
            storeGeometryBoundsSouthwest = null;
            storePartialMatch = null;
        }

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

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

            if (responseObject.IsNone) return;

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

            FindLocationResponseWrapper wrapper = responseObject.Value as FindLocationResponseWrapper;
            if (locationIndex.Value < 0 || locationIndex.Value >= wrapper.count) return;

            GoogleGeocodingResult result = wrapper.results[locationIndex.Value];
            storeAddressComponentsCount.Value = result.address_components.Length;
            storeTypesCount.Value = result.types.Length;
            storeFormattedAddress.Value = result.formatted_address;
            storePostcodeLocalitiesCount.Value = result.postcode_localities.Length;
            storeGeometryLocation.Value = result.geometry_location;
            storeGeometryLocationType.Value = result.geometry_location_type;
            storeGeometryViewportNortheast.Value = result.geometry_viewport_northeast;
            storeGeometryViewportSouthwest.Value = result.geometry_viewport_southwest;
            storePlaceID.Value = result.place_id;
            storeGeometryBoundsNortheast.Value = result.geometry_bounds_northeast;
            storeGeometryBoundsSouthwest.Value = result.geometry_bounds_southwest;
            storePartialMatch.Value = result.partial_match;
        }
    }
}