using Bolt;
using Ludiq;

namespace InfinityCode.OnlineMapsBolt
{
    [UnitTitle("Subscribe To Tile Events")]
    [UnitSubtitle("(arg - tile or none)")]
    public class SubscribeToTileEvents : OnlineMapsBoltEventBase
    {
        [DoNotSerialize]
        public ValueInput tile { get; private set; }

        [DoNotSerialize]
        [PortLabel("Start Download Tile")]
        public ValueInput OnStartDownloadTile;

        [DoNotSerialize]
        [PortLabel("Tile Downloaded")]
        public ValueInput OnTileDownloaded;

        [DoNotSerialize]
        [PortLabel("All Tiles Loaded")]
        public ValueInput OnAllTilesLoaded;

        [DoNotSerialize]
        [PortLabel("Disposed")]
        public ValueInput OnDisposed;

        protected override void OnDefinition()
        {
            tile = ValueInput("tile", (OnlineMapsTile)null);
            OnStartDownloadTile = ValueInput("OnStartDownloadTile", string.Empty);
            OnTileDownloaded = ValueInput("OnTileDownloaded", string.Empty);
            OnAllTilesLoaded = ValueInput("OnAllTilesLoaded", string.Empty);
            OnDisposed = ValueInput("OnDisposed", string.Empty);
        }

        protected override void OnEnter(Flow flow)
        {
            SubscribeToEvent(flow, ref OnlineMapsTileManager.OnStartDownloadTile, OnStartDownloadTile);
            SubscribeToEvent(flow, ref OnlineMapsTile.OnAllTilesLoaded, OnAllTilesLoaded);
            SubscribeToEvent(flow, ref OnlineMapsTile.OnTileDownloaded, OnTileDownloaded);

            OnlineMapsTile t = flow.GetValue<OnlineMapsTile>(tile);
            if (t != null) SubscribeToEvent(flow, ref t.OnDisposed, OnDisposed);
        }
    }
}