using CareFusion.Mosaic.Interfaces.Messages.Task;
using CareFusion.Mosaic.Interfaces.Types.Output;
using System;

namespace CareFusion.Mosaic.Converters
{
    /// <summary>
    /// Class which defines protocol related type conversion methods.
    /// </summary>
    public static class InternalTypeConverter
    {
        /// <summary>
        /// Converts the specified state string into a valid state enumeration value.
        /// </summary>
        /// <param name="stateString">The state string to convert.</param>
        /// <returns>The valid state enumeration value</returns>
        public static StockOutputOrderState ConvertOrderState(string stateString)
        {
            StockOutputOrderState result = StockOutputOrderState.Rejected;

            if (string.IsNullOrWhiteSpace(stateString))
            {
                return result;
            }

            if (Enum.TryParse<StockOutputOrderState>(stateString, out result) == false)
            {
                result = StockOutputOrderState.Rejected;
            }

            return result;
        }

        /// <summary>
        /// Converts the specified state string into a valid state enumeration value.
        /// </summary>
        /// <param name="stateString">The state string to convert.</param>
        /// <returns>The valid state enumeration value</returns>
        public static TaskState ConvertTaskState(string stateString)
        {
            TaskState result = TaskState.Unknown;

            if (string.IsNullOrWhiteSpace(stateString))
            {
                return result;
            }

            if (Enum.TryParse<TaskState>(stateString, out result) == false)
            {
                result = TaskState.Unknown;
            }

            return result;
        }

        /// <summary>
        /// OutputPriority list the available output priorities.
        /// </summary>
        internal enum OutputPriority
        {
            Highest = 1,
            High = 2,
            Normal = 3,
            Low = 4,
            Lowest = 5
        }

        /// <summary>
        /// Converts the specified output priority string into an according integer value.
        /// </summary>
        /// <param name="priority">The priority string to convert.</param>
        /// <returns>The converted priority integer value.</returns>
        public static int ConvertOutputPriority(string priority)
        {
            if (Enum.TryParse(priority, out OutputPriority result) == false)
            {
                result = OutputPriority.Normal;
            }

            return (int)result;
        }

        /// <summary>
        /// Converts the specified output priority integer into an according string value.
        /// </summary>
        /// <param name="priority">The priority integer to convert.</param>
        /// <returns>The converted priority string value.</returns>
        public static string ConvertOutputPriority(int priority)
        {
            string result = OutputPriority.Normal.ToString();

            if (Enum.IsDefined(typeof(OutputPriority), priority) == true)
            {
                OutputPriority outputPriority = (OutputPriority)priority;

                result = outputPriority.ToString();
            }

            return result;
        }
    }
}
