﻿using System;
using Arch.CMessaging.Client.Net.Core.Filterchain;
using Arch.CMessaging.Client.Net.Core.Future;
using Arch.CMessaging.Client.Net.Core.Session;
using Arch.CMessaging.Client.Net.Util;

namespace Arch.CMessaging.Client.Net.Filter.Codec
{
    public class ProtocolCodecSession : DummySession
    {
        private readonly IWriteFuture _notWrittenFuture;
        private readonly AbstractProtocolEncoderOutput _encoderOutput;
        private readonly AbstractProtocolDecoderOutput _decoderOutput;

        public ProtocolCodecSession()
        { 
            _notWrittenFuture = DefaultWriteFuture.NewNotWrittenFuture(this, new NotImplementedException());
            _encoderOutput = new DummyProtocolEncoderOutput(_notWrittenFuture);
            _decoderOutput = new DummyProtocolDecoderOutput();
        }

        public IProtocolEncoderOutput EncoderOutput
        {
            get { return _encoderOutput; }
        }

        public IQueue<Object> EncoderOutputQueue
        {
            get { return _encoderOutput.MessageQueue; }
        }

        public IProtocolDecoderOutput DecoderOutput
        {
            get { return _decoderOutput; }
        }

        public IQueue<Object> DecoderOutputQueue
        {
            get { return _decoderOutput.MessageQueue; }
        }

        class DummyProtocolEncoderOutput : AbstractProtocolEncoderOutput
        {
            private IWriteFuture _future;

            public DummyProtocolEncoderOutput(IWriteFuture future)
            {
                _future = future;
            }

            public override IWriteFuture Flush()
            {
                return _future;
            }
        }

        class DummyProtocolDecoderOutput : AbstractProtocolDecoderOutput
        {
            public override void Flush(INextFilter nextFilter, IoSession session)
            {
                // Do nothing
            }
        }
    }
}
