using Edge.Core.Parser;
using Edge.Core.Processor.Communicator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Edge.Core.Processor
{
    public interface IContext<TRaw, TMessage> : IDisposable where TMessage : MessageBase
    {
        //event EventHandler OnCommunicatorConnected;
        IDeviceProcessor<TRaw, TMessage> Processor { get; }
        IDeviceHandler<TRaw, TMessage> Handler { get; }
        ICommunicator<TRaw, TMessage> Communicator { get; }

        IIncoming<TMessage> Incoming { get; }
        IOutgoing<TRaw, TMessage> Outgoing { get; }
    }

    //public interface IBinaryContext<TMessage> : IContext<byte[], TMessage>
    //{
    //    IHandler<TMessage> Handler { get; }
    //    IDeviceProcessor<byte[], TMessage> Processor { get; }

    //    Incoming<byte[], TMessage> Incoming { get; }
    //    Outgoing<byte[], TMessage> Outgoing { get; }
    //}

    public class Context<TRaw, TMessage> : IContext<TRaw, TMessage> where TMessage : MessageBase
    {
        //public event EventHandler OnCommunicatorConnected;
        //public event EventHandler OnCommunicatorDisconnected;
        public IDeviceProcessor<TRaw, TMessage> Processor { get; }
        public IDeviceHandler<TRaw, TMessage> Handler { get; }

        public ICommunicator<TRaw, TMessage> Communicator { get; }

        public IIncoming<TMessage> Incoming { get; }

        public IOutgoing<TRaw, TMessage> Outgoing { get; }

        public Context(IDeviceProcessor<TRaw, TMessage> processor, IDeviceHandler<TRaw, TMessage> handler, ICommunicator<TRaw, TMessage> communicator, IIncoming<TMessage> incoming, IOutgoing<TRaw, TMessage> outgoing)
        {
            this.Processor = processor;
            this.Communicator = communicator;
            this.Incoming = incoming;// new HistoryKeepIncoming<TMessage>(10);
            this.Outgoing = outgoing;// new Outgoing<TRaw, TMessage>(this);
            this.Handler = handler;
        }

        public void Dispose()
        {
            if (this.Incoming is IDisposable idp)
                idp.Dispose();
            if (this.Outgoing is IDisposable odp)
                odp.Dispose();
        }
    }

    //public class HttpStringContext<TMessage> : IContext<TMessage> where TMessage : MessageBase
    //{
    //    public event EventHandler OnCommunicatorConnected;
    //    public event EventHandler OnCommunicatorDisconnected;
    //    public IHandler<TMessage> Handler { get; }

    //    //public IDeviceProcessor<byte[], TMessage> Processor { get; }

    //    public Incoming<TMessage> Incoming { get; }

    //    public Outgoing<TMessage> Outgoing { get; }

    //    public HttpStringContext(IHandler<TMessage> handler, ICommunicator<string, TMessage> communicator, IDeviceProcessor<string, TMessage> processor)
    //    {
    //        this.Incoming = new HistoryKeepIncoming<TMessage>(10);
    //        this.Outgoing = new Outgoing<TMessage>(this);
    //        this.Handler = handler;
    //        communicator.OnConnected += (a, b) =>
    //        {
    //            var s = this.OnCommunicatorConnected;
    //            s?.Invoke(this, null);
    //        };
    //        communicator.OnDisconnected += (a, b) =>
    //        {
    //            var s = this.OnCommunicatorDisconnected;
    //            s?.Invoke(this, null);
    //        };
    //    }
    //}
}
