// **********************************************************************************************************
//    Parse.cs
//    5/18/2022
//    NGI - Next Generation Interceptor
//    
//    Contract No. HQ0856-21-C-0003/1022000209
//    
//    THIS DOCUMENT DOES NOT CONTAIN TECHNOLOGY OR TECHNICAL DATA CONTROLLED UNDER EITHER THE U.S. 
//    INTERNATIONAL TRAFFIC IN ARMS REGULATIONS OR THE U.S. EXPORT ADMINISTRATION REGULATIONS.
//    
//    RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION PROPRIETARY TO RAYTHEON 
//    COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. 
//    DISCLOSURE TO UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO RAYTHEON 
//    COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS CONTENTS SHALL BE FURNISHED OR DISCLOSED 
//    TO OR COPIED OR USED BY PERSONS OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF 
//    RAYTHEON COMPANY.
//    
//    UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
//    
//    DESTRUCTION NOTICE: FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN DOD 5220.22-M, 
//    NATIONAL INDUSTRIAL SECURITY PROGRAM OPERATING MANUAL, FEBRUARY 2006, 
//    INCORPORATING CHANGE 1, MARCH 28, 2013, CHAPTER 5, SECTION 7, OR DODM 5200.01-VOLUME 3, 
//    DOD INFORMATION SECURITY PROGRAM: PROTECTION OF CLASSIFIED INFORMATION, ENCLOSURE 3, 
//    SECTION 17. FOR CONTROLLED UNCLASSIFIED INFORMATION FOLLOW THE PROCEDURES IN DODM 5200.01-VOLUME 4, 
//    INFORMATION SECURITY PROGRAM: CONTROLLED UNCLASSIFIED INFORMATION.
//    
//    CONTROLLED BY: MISSILE DEFENSE AGENCY
//    CONTROLLED BY: GROUND-BASED MIDCOURSE DEFENSE PROGRAM OFFICE
//    CUI CATEGORY: CTI 
//    DISTRIBUTION/DISSEMINATION CONTROL: F 
//    POC: Alex Kravchenko (1118268)
// **********************************************************************************************************
using System;

namespace Raytheon.Common.Coe
{
    public class Parse
    {
        public static bool Try(string value, out byte result)
        {
            result = 0;
            if (value == null) 
                return false; // Handle bad argument

            bool passed = byte.TryParse(value, out result);
            if (passed == false)
            {
                if (value.StartsWith("0x") == true)
                {
                    value = value.Substring(2, value.Length - 2);
                    passed = byte.TryParse(value, System.Globalization.NumberStyles.HexNumber, null, out result);
                }
                else if (value.EndsWith("b") == true)
                {
                    value = value.TrimEnd("b".ToCharArray());
                    try
                    {
                        result = Convert.ToByte(value, 2);
                        passed = true;
                    }
                    catch
                    {
                        passed = false;
                    }
                }
                else
                {
                    passed = true;  // Handle Boolean TYPES
                    if (value.Trim().ToUpper().Equals("TRUE"))
                        result = 1;
                    else if (value.Trim().ToUpper().Equals("FALSE"))
                        result = 0;
                    else
                        passed = false;
                }
            }

            return passed;
        }

        public static bool Try(string value, out decimal result)
        {
            result = 0;
            if (value == null) 
                return false; // Handle bad argument

            bool passed = decimal.TryParse(value, System.Globalization.NumberStyles.Any, null, out result);
            if (passed == false)
            {
                if (value.StartsWith("0x") == true)
                {
                    value = value.Substring(2, value.Length - 2);
                    passed = int.TryParse(value, System.Globalization.NumberStyles.HexNumber, null, out int hexResult);
                    if (passed)
                    {
                        result = hexResult;
                    }
                }
            }

            return passed;
        }

        public static bool Try(string value, out short result)
        {
            result = 0;
            if (value == null) 
                return false; // Handle bad argument

            bool passed = short.TryParse(value, out result);
            if (passed == false)
            {
                if (value.StartsWith("0x") == true)
                {
                    value = value.Substring(2, value.Length - 2);
                    passed = short.TryParse(value, System.Globalization.NumberStyles.HexNumber, null, out result);
                }
                else if (value.EndsWith("b") == true)
                {
                    value = value.TrimEnd("b".ToCharArray());
                    try
                    {
                        result = Convert.ToInt16(value, 2);
                        passed = true;
                    }
                    catch
                    {
                        passed = false;
                    }
                }
                else
                {
                    passed = true;  // Handle Boolean TYPES
                    if (value.Trim().ToUpper().Equals("TRUE"))
                        result = 1;
                    else if (value.Trim().ToUpper().Equals("FALSE"))
                        result = 0;
                    else
                        passed = false;
                }
            }

            return passed;
        }

        public static bool Try(string value, out ushort result)
        {
            result = 0;
            if (value == null) 
                return false; // Handle bad argument

            bool passed = ushort.TryParse(value, out result);
            if (passed == false)
            {
                if (value.StartsWith("0x") == true)
                {
                    value = value.Substring(2, value.Length - 2);
                    passed = ushort.TryParse(value, System.Globalization.NumberStyles.HexNumber, null, out result);
                }
                else if (value.EndsWith("b") == true)
                {
                    value = value.TrimEnd("b".ToCharArray());
                    try
                    {
                        result = Convert.ToUInt16(value, 2);
                        passed = true;
                    }
                    catch
                    {
                        passed = false;
                    }
                }
                else
                {
                    passed = true;  // Handle Boolean TYPES
                    if (value.Trim().ToUpper().Equals("TRUE"))
                        result = 1;
                    else if (value.Trim().ToUpper().Equals("FALSE"))
                        result = 0;
                    else
                        passed = false;
                }
            }

            return passed;
        }

        public static bool Try(string value, out int result)
        {
            result = 0;
            if (value == null) 
                return false; // Handle bad argument

            bool passed = int.TryParse(value, System.Globalization.NumberStyles.Any, null, out result);
            if (passed == false)
            {
                if (value.StartsWith("0x") == true)
                {
                    value = value.Substring(2, value.Length - 2);
                    passed = int.TryParse(value, System.Globalization.NumberStyles.HexNumber, null, out result);
                }
                else if (value.EndsWith("b") == true)
                {
                    value = value.TrimEnd("b".ToCharArray());
                    try
                    {
                        result = Convert.ToInt32(value, 2);
                        passed = true;
                    }
                    catch
                    {
                        passed = false;
                    }
                }
                else
                {
                    passed = true;  // Handle Boolean TYPES
                    if (value.Trim().ToUpper().Equals("TRUE"))
                        result = 1;
                    else if (value.Trim().ToUpper().Equals("FALSE"))
                        result = 0;
                    else
                        passed = false;
                }
            }

            return passed;
        }

        public static bool Try(string value, out uint result)
        {
            result = 0;
            if (value == null) 
                return false; // Handle bad argument

            bool passed = uint.TryParse(value, out result);
            if (passed == false)
            {
                if (value.StartsWith("0x") == true)
                {
                    value = value.Substring(2, value.Length - 2);
                    passed = uint.TryParse(value, System.Globalization.NumberStyles.HexNumber, null, out result);
                }
                else if (value.EndsWith("b") == true)
                {
                    value = value.TrimEnd("b".ToCharArray());
                    try
                    {
                        result = Convert.ToUInt32(value, 2);
                        passed = true;
                    }
                    catch
                    {
                        passed = false;
                    }
                }
                else
                {
                    passed = true;  // Handle Boolean TYPES
                    if (value.Trim().ToUpper().Equals("TRUE"))
                        result = 1;
                    else if (value.Trim().ToUpper().Equals("FALSE"))
                        result = 0;
                    else
                        passed = false;
                }
            }

            return passed;
        }

        public static bool Try(string value, out long result)
        {
            result = 0;
            if (value == null || value == "") 
                return false; // Handle bad argument

            bool passed = long.TryParse(value, out result);
            if (passed == false)
            {
                if (value.StartsWith("0x") == true)
                {
                    value = value.Substring(2, value.Length - 2);
                    passed = long.TryParse(value, System.Globalization.NumberStyles.HexNumber, null, out result);
                }
                else if (value.EndsWith("b") == true)
                {
                    value = value.TrimEnd("b".ToCharArray());
                    try
                    {
                        result = Convert.ToInt64(value, 2);
                        passed = true;
                    }
                    catch
                    {
                        passed = false;
                    }
                }
                else
                {
                    passed = true;  // Handle Boolean TYPES
                    if (value.Trim().ToUpper().Equals("TRUE"))
                        result = 1;
                    else if (value.Trim().ToUpper().Equals("FALSE"))
                        result = 0;
                    else
                        passed = false;
                }
            }

            return passed;
        }

        public static bool Try(string value, out ulong result)
        {
            result = 0;
            if (value == null) 
                return false; // Handle bad argument

            bool passed = ulong.TryParse(value, out result);
            if (passed == false)
            {
                if (value.StartsWith("0x") == true)
                {
                    value = value.Substring(2, value.Length - 2);
                    passed = ulong.TryParse(value, System.Globalization.NumberStyles.HexNumber, null, out result);
                }
                else if (value.EndsWith("b") == true)
                {
                    value = value.TrimEnd("b".ToCharArray());
                    try
                    {
                        result = Convert.ToUInt64(value, 2);
                        passed = true;
                    }
                    catch
                    {
                        passed = false;
                    }
                }
                else
                {
                    passed = true;  // Handle Boolean TYPES
                    if (value.Trim().ToUpper().Equals("TRUE"))
                        result = 1;
                    else if (value.Trim().ToUpper().Equals("FALSE"))
                        result = 0;
                    else
                        passed = false;
                }
            }

            return passed;
        }

        public static bool Try(string value, out char result)
        {
            return char.TryParse(value, out result);
        }

        public static bool Try(string value, out float result)
        {
            if (value.EndsWith("f") == true)
                value = value.TrimEnd("f".ToCharArray());

            return float.TryParse(value, System.Globalization.NumberStyles.Any, null, out result);
        }

        public static bool Try(string value, out double result)
        {
            if (value.EndsWith("f") == true)
                value = value.TrimEnd("f".ToCharArray());

            return double.TryParse(value, System.Globalization.NumberStyles.Any, null, out result);
        }
    }
}
