//******************************************************************************//
// StringExtensions.cs
// 12/4/2023
//
// 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.
//
// THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. GOVERNMENT
//
// UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
//
// WARNING: THIS DOCUMENT CONTAINS TECHNICAL DATA AND / OR TECHNOLOGY WHOSE
// EXPORT OR DISCLOSURE TO NON-U.S. PERSONS, WHEREVER LOCATED, IS RESTRICTED
// BY THE INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) (22 C.F.R. SECTION
// 120-130) OR THE EXPORT ADMINISTRATION REGULATIONS (EAR) (15 C.F.R. SECTION
// 730-774). THIS DOCUMENT CANNOT BE EXPORTED (E.G., PROVIDED TO A SUPPLIER
// OUTSIDE OF THE UNITED STATES) OR DISCLOSED TO A NON-U.S. PERSON, WHEREVER
// LOCATED, UNTIL A FINAL JURISDICTION AND CLASSIFICATION DETERMINATION HAS
// BEEN COMPLETED AND APPROVED BY RAYTHEON, AND ANY REQUIRED U.S. GOVERNMENT
// APPROVALS HAVE BEEN OBTAINED. VIOLATIONS ARE SUBJECT TO SEVERE CRIMINAL
// PENALTIES.
//
// DOD 5220.22-M, INDUSTRIAL SECURITY MANUAL, CHAPTER 5, SECTION 1 THROUGH 9 :
// FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN OR DOD 5200.1-R,
// INFORMATION SECURITY PROGRAM, CHAPTER 6. FOR UNCLASSIFIED, LIMITED DOCUMENTS
// DESTROY BY ANY  METHOD THAT WILL PREVENT DISCLOSURE OF CONTENTS OR
// RECONSTRUCTION OF THE DOCUMENT.
// POC: Alex Kravchenko (1118268)
//******************************************************************************//
using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Raytheon.LogDashboard.Helpers
{

    public static class StringExtensions
    {
        /// <summary>
        /// takes the left of the string from given index
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static string Left(this string value, int maxLength)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            maxLength = Math.Abs(maxLength);

            return (value.Length <= maxLength ? value : value.Substring(0, maxLength));
        }

        /// <summary>
        /// takes a left of a string from given character
        /// </summary>
        /// <param name="value"></param>
        /// <param name="character"></param>
        /// <returns></returns>
        public static string LeftFrom(this string value, char character)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            int index = value.IndexOf(character);

            if(index == -1)
            {
                return value;
            }

            return value.Substring(0, index);
        }

        /// <summary>
        /// takes a left of a string from given string
        /// </summary>
        /// <param name="value"></param>
        /// <param name="characters"></param>
        /// <returns></returns>
        public static string LeftFrom(this string value, string characters)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            int index = value.IndexOf(characters);

            if(index == -1)
            {
                return value;
            }

            return value.Substring(0, index);
        }

        /// <summary>
        /// takes a left of a string from given regular expression
        /// </summary>
        /// <param name="value"></param>
        /// <param name="regex"></param>
        /// <returns></returns>
        public static string LeftFromRegex(this string value, string regex)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            Match match = Regex.Match(value, regex);

            if(!match.Success)
            {
                return value;
            }

            return value.Substring(0, match.Index);
        }

        /// <summary>
        /// takes a right of a string from given length
        /// </summary>
        /// <param name="value"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Right(this string value, int length)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            return value.Length <= length ? value : value.Substring(value.Length - length);
        }

        /// <summary>
        /// takes a right of a string from given character
        /// </summary>
        /// <param name="value"></param>
        /// <param name="character"></param>
        /// <returns></returns>
        public static string RightFrom(this string value, char character)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            int index = value.LastIndexOf(character);

            if(index == -1)
            {
                return value;
            }

            return value.Substring(index + 1);
        }

        /// <summary>
        /// takes a right of a string from given string including that string
        /// </summary>
        /// <param name="value"></param>
        /// <param name="characters"></param>
        /// <returns></returns>
        public static string RightFrom(this string value, string characters)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            int index = value.LastIndexOf(characters);

            if(index == -1)
            {
                return value;
            }

            return value.Substring(index);
        }

        /// <summary>
        /// takes a right of a string from given string	not including the string
        /// </summary>
        /// <param name="value"></param>
        /// <param name="characters"></param>
        /// <returns></returns>
        public static string RightAfter(this string value, string characters)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            int index = value.LastIndexOf(characters);

            if(index == -1)
            {
                return value;
            }

            if(value.Length > index + characters.Length)
            {
                return value.Substring(index + characters.Length);
            }
            else
            {
                return string.Empty;
            }
        }


        /// <summary>
        /// takes a right of a string form given regular expression
        /// </summary>
        /// <param name="value"></param>
        /// <param name="regex"></param>
        /// <returns></returns>
        public static string RightFromRegex(this string value, string regex)
        {
            if(string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            Match match = Regex.Match(value, regex);

            if(!match.Success)
            {
                return value;
            }

            return value.Substring(match.Index + match.Length);
        }

        /// <summary>
        /// returns the first match of the regular expression
        /// </summary>
        /// <param name="input"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string GetFirstMatch(this string input, string pattern)
        {
            var match = Regex.Match(input, pattern);
            return match.Groups[0].Value;
        }

        /// <summary>
        /// reads the next value after provided identifier
        /// for example: 
        /// in the string RESULT=123 and identifier = RESULT this function returns 123
        /// </summary>
        /// <param name="input"></param>
        /// <param name="identifier"></param>
        /// <returns></returns>
        public static T GetValueAfterIdentifier<T>(this string input, string identifier) => TypeConverter.ChangeType<T>(GetFirstMatch(input, $"(?<={identifier}=)[^ ]+|$"));


        /// <summary>
        /// checks for numeric value
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumeric(this string input)
        {
            if(string.IsNullOrEmpty(input))
            {
                return false;
            }

            return Regex.IsMatch(input.Trim(), @"^[\d.-]+$");
        }

        /// <summary>
        /// converts string to Pascal Case
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ToPascalCase(this string text)
        {
            string yourString = text.ToLower().Replace("_", " ");
            TextInfo info = CultureInfo.CurrentCulture.TextInfo;
            return info.ToTitleCase(yourString).Replace(" ", string.Empty);
        }
    }
}