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 TaskCancelInfeedInputResponse message envelope.
    /// </summary>
    [XmlType(TypeName = "WWKS")]
    [ExcludeFromCodeCoverage]
    public class TaskCancelInfeedInputResponseEnvelope : EnvelopeBase, IMessageConversion
    {
        [XmlElement]
        public TaskCancelInfeedInputResponse TaskCancelInfeedInputResponse { 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.TaskCancelInfeedInputResponse.ToMosaicMessage(converterStream);
        }
    }

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

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

        #endregion

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

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

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

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

                for (int i = 0; i < this.Task.Length; ++i)
                {
                    var task = response.Tasks[i];
                    this.Task[i] = new Types.Task()
                    {
                        Id = task.ID,
                        InfeedNumber = task.InfeedNumber.ToString(),
                        Status = task.State.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.Task.TaskCancelInfeedInputResponse(converterStream);

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

            if (this.Task != null)
            {
                for (int i = 0; i < this.Task.Length; ++i)
                {
                    var task = this.Task[i];

                    response.Tasks.Add(new Interfaces.Messages.Task.Task()
                    {
                        ID = task.Id,
                        State = InternalTypeConverter.ConvertTaskState(task.Status),
                        InfeedNumber = TypeConverter.ConvertInt(task.InfeedNumber)
                    });
                }
            }

            return response;
        }
    }
}
