﻿using Bolt;
using Ludiq;
using UnityEngine;

namespace InfinityCode.OnlineMapsBolt
{
    public class SubscribeToLocationServiceEvents : OnlineMapsBoltEventBase
    {
        [DoNotSerialize]
        [PortLabel("Compass Changed")]
        public ValueInput OnCompassChanged;

        [DoNotSerialize]
        [PortLabel("Find Location By IP Complete")]
        public ValueInput OnFindLocationByIPComplete;

        [DoNotSerialize]
        [PortLabel("Location Changed")]
        public ValueInput OnLocationChanged;

        [DoNotSerialize]
        [PortLabel("Location Inited")]
        public ValueInput OnLocationInited;

        [DoNotSerialize]
        [PortLabel("Position Restored")]
        public ValueInput OnPositionRestored;

        protected override void OnDefinition()
        {
            OnCompassChanged = ValueInput("OnCompassChanged", string.Empty);
            OnFindLocationByIPComplete = ValueInput("OnFindLocationByIPComplete", string.Empty);
            OnLocationChanged = ValueInput("OnLocationChanged", string.Empty);
            OnLocationInited = ValueInput("OnLocationInited", string.Empty);
            OnPositionRestored = ValueInput("OnPositionRestored", string.Empty);
        }

        protected override void OnEnter(Flow flow)
        {
            OnlineMapsLocationServiceBase ls = OnlineMapsLocationServiceBase.baseInstance;

            if (ls == null)
            {
                Debug.Log("Online Maps Location Service is null");
                return;
            }

            SubscribeToEvent(flow, ref ls.OnCompassChanged, OnCompassChanged);
            SubscribeToEvent(flow, ref ls.OnFindLocationByIPComplete, OnFindLocationByIPComplete);
            SubscribeToEvent(flow, ref ls.OnLocationChanged, OnLocationChanged);
            SubscribeToEvent(flow, ref ls.OnLocationInited, OnLocationInited);
            SubscribeToEvent(flow, ref ls.OnPositionRestored, OnPositionRestored);
        }
    }
}