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

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

        private enum InfeedInputStatus
        { 
            Rejected,
            Accepted
        }

        private enum Reasons
        {
            UnknownPackDimensions,
            InputBroken,
            NoSpaceInMachine,
            NoSpaceOnBelt,
            UnknownTenant,
            UnknownStockLocation,
            TaskCancelled,
            SystemNotActive,
            InputProcessInProgress
        }

        #endregion

        #region Properties

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

        #endregion

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

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

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

            this.Details = new Types.Details()
            {
                InfeedNumber = response.InfeedNumber.ToString()
            };

            if ((response.Status == Interfaces.Messages.Input.InfeedInputState.InQueue) ||
                (response.Status == Interfaces.Messages.Input.InfeedInputState.InProgress))
            {
                this.Details.Status = InfeedInputStatus.Accepted.ToString();               
            }
            else
            {
                this.Details.Status = InfeedInputStatus.Rejected.ToString();
                this.Details.Reason = GetRejectReason(response.RejectReason);
                this.Details.Description = response.RejectReasonText;
            }
        }

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

            response.ID = this.Id;
            response.Destination = this.Destination;
            response.Source = this.Source;
            response.Status = InfeedInputState.None;
            response.RejectReason = GetRejectReason();

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

                if (string.Compare(this.Details.Status, InfeedInputStatus.Accepted.ToString(), true) == 0)
                    response.Status = Interfaces.Messages.Input.InfeedInputState.InQueue;
                else
                    response.Status = Interfaces.Messages.Input.InfeedInputState.RejectedByMachine;

                if (!string.IsNullOrEmpty(this.Details.Description))
                    response.RejectReasonText = this.Details.Description;
            }

            return response;
        }

        /// <summary>
        /// Translates the Mosaic internal reject reason into a WWK2 compatible one.
        /// </summary>
        /// <param name="rejectReason">The reject reason to translate.</param>
        /// <returns>
        /// WWKS2 compatible reject reason as string.
        /// </returns>
        private string GetRejectReason(InfeedInputRejectReason rejectReason)
        {
            switch (rejectReason)
            {
                case InfeedInputRejectReason.None: return string.Empty;
                case InfeedInputRejectReason.UnknownPackDimensions: return Reasons.UnknownPackDimensions.ToString();
                case InfeedInputRejectReason.InputBroken: return Reasons.InputBroken.ToString();
                case InfeedInputRejectReason.NoSpaceInMachine: return Reasons.NoSpaceInMachine.ToString();
                case InfeedInputRejectReason.NoSpaceOnBelt: return Reasons.NoSpaceOnBelt.ToString();
                case InfeedInputRejectReason.UnknownTenant: return Reasons.UnknownTenant.ToString();
                case InfeedInputRejectReason.UnknownStockLocation: return Reasons.UnknownStockLocation.ToString();
                case InfeedInputRejectReason.RejectedInvalidStockLocation:
                case InfeedInputRejectReason.SystemNotActive: return Reasons.SystemNotActive.ToString();
                case InfeedInputRejectReason.InputProcessInProgress: return Reasons.InputProcessInProgress.ToString();
            }

            return Reasons.InputBroken.ToString();
        }

        /// <summary>
        /// Translates the current TIOSysMan compatible reject reason into a Mosaic internal one.
        /// </summary>
        /// <returns>
        /// Mosaic internal reject reason.
        /// </returns>
        private InfeedInputRejectReason GetRejectReason()
        {
            if (string.IsNullOrEmpty(this.Details?.Reason))
                return InfeedInputRejectReason.None;

            var rejectReason = TypeConverter.ConvertEnum<Reasons>(this.Details.Reason, Reasons.InputBroken);

            switch (rejectReason)
            {
                case Reasons.TaskCancelled: return InfeedInputRejectReason.TaskCancelled;
                case Reasons.UnknownPackDimensions: return InfeedInputRejectReason.UnknownPackDimensions;
                case Reasons.InputBroken: return InfeedInputRejectReason.InputBroken;
                case Reasons.NoSpaceInMachine: return InfeedInputRejectReason.NoSpaceInMachine;
                case Reasons.NoSpaceOnBelt: return InfeedInputRejectReason.NoSpaceOnBelt;
                case Reasons.UnknownTenant: return InfeedInputRejectReason.UnknownTenant;
                case Reasons.UnknownStockLocation: return InfeedInputRejectReason.UnknownStockLocation;
            }

            return InfeedInputRejectReason.InputBroken;
        }
    }
}
