using CareFusion.Mosaic.Converters.Wwks2.Messages;
using CareFusion.Mosaic.Interfaces.Converters;
using CareFusion.Mosaic.Interfaces.Messages.OutputDestination;
using System.Collections.Generic;

namespace StorageSystemSimulator.Cores
{
    public class SimulatorOutputDestinationCore
    {
        private int _minOutputDestination = 0;
        private int _maxOutputDestination = 0;
        private string _errorText = "";

        public void ProcessOutputDestinationStateIndicationSetRequest(OutputDestinationStateIndicationSetRequest outputDestinationStateIndicationSetRequest)
        {
            if (outputDestinationStateIndicationSetRequest.State == OutputDestinationState.Invalid)
            {
                SendRejectOutputDestinationStateIndication(outputDestinationStateIndicationSetRequest, "Invalid requested State");
            }
            else
            {
                if (_minOutputDestination == _maxOutputDestination)
                {
                    SendAcceptedOutputDestinationStateIndication(outputDestinationStateIndicationSetRequest);
                }
                else
                {
                    if ((_minOutputDestination <= outputDestinationStateIndicationSetRequest.OutputDestination)
                        && (_maxOutputDestination >= outputDestinationStateIndicationSetRequest.OutputDestination))
                    {
                        SendAcceptedOutputDestinationStateIndication(outputDestinationStateIndicationSetRequest);
                    }
                    else
                    {
                        SendRejectOutputDestinationStateIndication(outputDestinationStateIndicationSetRequest, _errorText);
                    }
                }
            }
        }

        private static void SendAcceptedOutputDestinationStateIndication(OutputDestinationStateIndicationSetRequest outputDestinationStateIndicationSetRequest)
        {
            OutputDestinationStateIndicationSetResponse outputDestinationStateIndicationSetResponse = new OutputDestinationStateIndicationSetResponse()
            {
                OutputDestination = outputDestinationStateIndicationSetRequest.OutputDestination,
                Result = OutputDestinationResult.Ok,
                Text = null
            };
            outputDestinationStateIndicationSetResponse.AdoptHeader(outputDestinationStateIndicationSetRequest);
            outputDestinationStateIndicationSetResponse.ConverterStream.Write(outputDestinationStateIndicationSetResponse);
        }

        private static void SendRejectOutputDestinationStateIndication(OutputDestinationStateIndicationSetRequest outputDestinationStateIndicationSetRequest, string errorText)
        {
            OutputDestinationStateIndicationSetResponse outputDestinationStateIndicationSetResponse = new OutputDestinationStateIndicationSetResponse()
            {
                OutputDestination = outputDestinationStateIndicationSetRequest.OutputDestination,
                Result = OutputDestinationResult.Error,
                Text = errorText
            };
            outputDestinationStateIndicationSetResponse.AdoptHeader(outputDestinationStateIndicationSetRequest);
            outputDestinationStateIndicationSetResponse.ConverterStream.Write(outputDestinationStateIndicationSetResponse);
        }

        internal void SetOutputDestinationStateSetting(int minOutputDestination, int maxOutputDestination, string errorText)
        {
            _minOutputDestination = minOutputDestination;
            _maxOutputDestination = maxOutputDestination;
            _errorText = errorText;
        }

        internal void SendButtonPressed(List<IConverterStream> converterStreamList, int outputDestination)
        {
            OutputDestinationButtonPressedMessage outputDestinationButtonPressedMessage = new OutputDestinationButtonPressedMessage()
            {
                ID = MessageId.Next,
                OutputDestination = outputDestination
            };

            foreach(var converterStream in converterStreamList)
            {
                converterStream.Write(outputDestinationButtonPressedMessage);
            }
        }
    }
}
