using System;
using Positron.Services.Mensageria.Interfaces.Observer;
using Positron.Services.Mensageria.Model.Positions;

namespace Positron.Services.Mensageria.Observer
{
    abstract class PositionMonitorBase : IObserver<Position>
    {
        private readonly int _intentEvent;
        private IDisposable _cancellation;
        protected PositionMonitorBase(int intentEvent)
        {
            this._intentEvent = intentEvent;
        }


        protected abstract void ReceivedPosition(Position position);


        public virtual void Subscribe(IObservable<Position> provider)
        {
            this._cancellation = provider.Subscribe(this);
        }

        public virtual void Unsubscribe()
        {
            this._cancellation.Dispose();
        }

        public void OnNext(Position value)
        {
            if (value.Pack.EventType != this._intentEvent)
                return;

            this.ReceivedPosition(value);
        }

        public void OnError(Exception error)
        {
            throw new NotImplementedException();
        }

        public void OnCompleted()
        {
            throw new NotImplementedException();
        }
    }
}
