﻿using Bolt;
using Ludiq;
using UnityEngine;

namespace InfinityCode.OnlineMapsBolt
{
    [UnitTitle("Subscribe To TextWebService Events")]
    public class SubscribeToTextWebServiceEvents : OnlineMapsBoltEventBase
    {
        [DoNotSerialize]
        public ValueInput request { get; private set; }

        [DoNotSerialize]
        [PortLabel("Complete (arg-string)")]
        public ValueInput OnComplete;

        [DoNotSerialize]
        [PortLabel("Success (arg-request)")]
        public ValueInput OnSuccess;

        [DoNotSerialize]
        [PortLabel("Failed (arg-request)")]
        public ValueInput OnFailed;

        protected override void OnDefinition()
        {
            request = ValueInput("request", (OnlineMapsTextWebService)null);
            OnComplete = ValueInput("OnComplete", string.Empty);
            OnSuccess = ValueInput("OnSuccess", string.Empty);
            OnFailed = ValueInput("OnFailed", string.Empty);
        }

        protected override void OnEnter(Flow flow)
        {
            OnlineMapsTextWebService r = flow.GetValue<OnlineMapsTextWebService>(request);
            if (r == null)
            {
                Debug.Log("Request is null");
                return;
            }

            SubscribeToEvent(flow, ref r.OnComplete, OnComplete);
            SubscribeToEvent(flow, ref r.OnSuccess, OnSuccess);
            SubscribeToEvent(flow, ref r.OnFailed, OnFailed);
        }
    }
}