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

namespace CareFusion.Mosaic.Converters.Wwks2.Messages.Stock
{
    /// <summary>
    /// Class which represents the WWKS 2.0 StockUpdateResponse message envelope.
    /// </summary>
    [XmlType(TypeName = "WWKS")]

    public class StockUpdateResponseEnvelope : EnvelopeBase, IMessageConversion
    {
        [XmlElement]
        public StockUpdateResponse StockUpdateResponse { 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 StockUpdateResponse.ToMosaicMessage(converterStream);
        }
    }

    /// <summary>
    /// Class which represents the WWKS 2.0 StockUpdateResponse message.
    /// </summary>
    public class StockUpdateResponse: MessageBase, IMessageConversion
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StockUpdateResponse"/> class.
        /// </summary>
        public StockUpdateResponse(){}

        /// <summary>
        /// Initializes a new instance of the <see cref="StockUpdateResponse"/> class.
        /// </summary>
        /// <param name="message">The message to use for initialization.</param>
        public StockUpdateResponse(MosaicMessage mosaicMessage)
        {
            var response = (Interfaces.Messages.Stock.StockUpdateResponse)mosaicMessage;
            Id = response.ID;
            Details = new Details
            {
                Reason = TextConverter.EscapeInvalidXmlChars(response.Details.Reason),
                Status = TextConverter.EscapeInvalidXmlChars(response.Details.Status)
            };
            Destination = response.Destination;
            Source = response.Source;
        }

        /// <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.Stock.StockUpdateResponse(converterStream);

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

            response.Details = new Details
            {
                Reason = TextConverter.EscapeInvalidXmlChars(response.Details.Reason),
                Status = TextConverter.UnescapeInvalidXmlChars(response.Details.Status)
            };

            return response;
        }

        /// <summary>
        /// The Details of the response
        /// </summary>
        [XmlElement]
        public Details Details { get; set; }
    }
}
