using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace AIProofread.Util
{
    public class HostHelper
    {
        public static string ReplaceSpecialChars(string text, bool isReplaceMultSpaceLine = false)
        {
            Regex regex = new Regex("[\r\f\a\v]{1}");
            if (!string.IsNullOrEmpty(text))
            {
                text = regex.Replace(text, "\n").Replace("\u001e", "-");
                if (isReplaceMultSpaceLine)
                {
                    bool flag = text.Last() == '\n';
                    text = string.Join("\n", text.Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries));
                    if (flag)
                    {
                        text += "\n";
                    }
                }
            }

            return text;
        }

        public static string RemoveInvisibleCharas(string text, bool containTable = false)
        {
            List<char> list = new List<char> { '\r', '\a', '\n', '\f', '\v' };
            if (containTable)
            {
                list.Add('\t');
            }

            return string.IsNullOrEmpty(text) ? string.Empty : text.Trim(list.ToArray());
        }

        public static bool HasValidCharacters(string text)
        {
            List<char> list = new List<char>{
                '\0', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\a', '\b', '\t',
                '\n', '\v', '\f', '\r', '\u000e', '\u000f', '\u0010', '\u0011', '\u0012', '\u0013',
                '\u0014', '\u0015', '\u0016', '\u0017', '\u0018', '\u0019', '\u001a', '\u001b', '\u001c', '\u001d',
                '\u001e', '\u001f', ' ', '/'
            };
            return text.Trim(list.ToArray()).Any();
        }
    }
}
