using System.Diagnostics.CodeAnalysis;
using System.Xml.Serialization;
using CareFusion.Mosaic.Converters.Wwks2.Types;
using CareFusion.Mosaic.Interfaces.Converters;
using CareFusion.Mosaic.Interfaces.Messages;
using CareFusion.Mosaic.Interfaces.Messages.Input;
using Rowa.Mosaic.Shared.Converters;

namespace CareFusion.Mosaic.Converters.Wwks2.Messages.Input
{
    /// <summary>
    /// Class which represents the WWKS 2.0 InfeedInputPlacePackResponse message envelope.
    /// </summary>
    [XmlType(TypeName = "WWKS")]
    [ExcludeFromCodeCoverage]
    public class InfeedInputPlacePackResponseEnvelope : EnvelopeBase, IMessageConversion
    {
        [XmlElement]
        public InfeedInputPlacePackResponse InfeedInputPlacePackResponse { get; set; }

        /// <summary>
        /// Translates this object instance into a Mosaic message.
        /// </summary>
        /// <param name="converterStream">The converter stream instance which request the message conversion.</param>
        /// <returns>
        /// The Mosaic message representation of this object.
        /// </returns>
        public MosaicMessage ToMosaicMessage(IConverterStream converterStream)
        {
            return this.InfeedInputPlacePackResponse.ToMosaicMessage(converterStream);
        }
    }

    /// <summary>
    /// Class which represents the WWKS 2.0 InfeedInputPlacePackResponse message.
    /// <see cref="https://portalrowa.carefusion.com/Unternehmen/Entwicklung/TechCom/Writing/P-010-044-B-I-388-DEU.docx" />
    /// </summary>
    public class InfeedInputPlacePackResponse : MessageBase, IMessageConversion
    {
        #region Enums

        private enum InfeedPackStatus
        {
            Placed,
            Aborted
        }

        #endregion

        #region Properties

        [XmlElement]
        public Details Details { get; set; }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="InfeedInputPlacePackResponse"/> class.
        /// </summary>
        [ExcludeFromCodeCoverage]
        public InfeedInputPlacePackResponse()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InfeedInputPlacePackResponse"/> class.
        /// </summary>
        /// <param name="message">The message to use for initialization.</param>
        public InfeedInputPlacePackResponse(MosaicMessage message)
        {
            var response = (Interfaces.Messages.Input.InfeedInputPlacePackResponse)message;

            this.Id = response.ID;
            this.Destination = response.Destination;
            this.Source = response.Source;
            this.Details = new Details()
            {
                InfeedNumber = response.InfeedNumber.ToString(),
                Status = ((response.Status == InfeedInputPlacePackState.PacksPlaced) ?
                          InfeedPackStatus.Placed : InfeedPackStatus.Aborted).ToString()
            };
        }

        /// <summary>
        /// Translates this object instance into a Mosaic message.
        /// </summary>
        /// <param name="converterStream">The converter stream instance which request the message conversion.</param>
        /// <returns>
        /// The Mosaic message representation of this object.
        /// </returns>
        public MosaicMessage ToMosaicMessage(IConverterStream converterStream)
        {
            var response = new Interfaces.Messages.Input.InfeedInputPlacePackResponse(converterStream);

            response.ID = this.Id;
            response.Destination = this.Destination;
            response.Source = this.Source;

            if (this.Details != null)
            {
                response.InfeedNumber = TypeConverter.ConvertInt(this.Details.InfeedNumber);

                if (string.Compare(this.Details.Status, InfeedPackStatus.Placed.ToString(), true) == 0)
                    response.Status = InfeedInputPlacePackState.PacksPlaced;
                else
                    response.Status = InfeedInputPlacePackState.AbortInput;
            }

            return response;
        }
    }
}
