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

namespace CareFusion.Mosaic.Converters.Wwks2.Messages.Task
{
    /// <summary>
    /// Class which represents the WWKS 2.0 TaskCancelInfeedInputRequest message envelope.
    /// </summary>
    [XmlType(TypeName = "WWKS")]
    [ExcludeFromCodeCoverage]
    public class TaskCancelInfeedInputRequestEnvelope : EnvelopeBase, IMessageConversion
    {
        [XmlElement]
        public TaskCancelInfeedInputRequest TaskCancelInfeedInputRequest { 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.TaskCancelInfeedInputRequest.ToMosaicMessage(converterStream);
        }
    }

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

        [XmlElement]
        public Types.Task[] Task { get; set; }

        #endregion

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

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

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

            if (request.Tasks.Count > 0)
            {
                this.Task = new Types.Task[request.Tasks.Count];

                for (int i = 0; i < this.Task.Length; ++i)
                {
                    var task = request.Tasks[i];
                    this.Task[i] = new Types.Task() 
                    { 
                        Id = task.ID, 
                        InfeedNumber = task.InfeedNumber.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 request = new Interfaces.Messages.Task.TaskCancelInfeedInputRequest(converterStream);

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

            if (this.Task != null)
            {
                for (int i = 0; i < this.Task.Length; ++i)
                {
                    request.Tasks.Add(new Interfaces.Messages.Task.Task()
                    {
                        ID = this.Task[i].Id,
                        InfeedNumber = TypeConverter.ConvertInt(this.Task[i].InfeedNumber)
                    });
                }
            }

            return request;
        }
    }
}
