using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace C4IT_IAM_Engine
{
    public static class Helper
    {
        public static string ReplaceLoopTag(this string str, int loop)
        {
            return Regex.Replace(str, @"(?<loopTag>{{(?<prefix>[^}]*)(?<loop>LOOP)(?<postfix>[^{]*)}})", loop <= 0 ? "" : "${prefix}" + loop + "${postfix}");
        }
        public static string ReplaceTags(this string str, IDictionary<string, string> dict)
        {
            if (str.Equals(string.Empty) || str == null || dict == null || dict.Count == 0)
                return str;
            return dict.Aggregate(str, (current, value) =>
                        current.Replace("{{" + value.Key + "}}", value.Value));
        }
        public static string ApplyTemplatePlaceholders(string templateValue, bool allowRelativePath, string defaultRelativePath, string[] sanitizedSegments, string folderName)
        {
            if (templateValue == null)
                return string.Empty;

            var result = Regex.Replace(templateValue, @"{{\s*NAME\s*}}", folderName ?? string.Empty, RegexOptions.IgnoreCase);

            if (allowRelativePath)
            {
                result = Regex.Replace(result, @"{{\s*RELATIVEPATH(?:\s*\(\s*(\d+)\s*\))?\s*}}", match =>
                {
                    if (sanitizedSegments == null || sanitizedSegments.Length == 0)
                        return string.Empty;

                    if (!match.Groups[1].Success)
                        return defaultRelativePath;

                    if (!int.TryParse(match.Groups[1].Value, out var segmentIndex) || segmentIndex < 0)
                        return defaultRelativePath;

                    var segmentCount = Math.Min(sanitizedSegments.Length, segmentIndex + 1);
                    var skip = sanitizedSegments.Length - segmentCount;
                    return string.Join("_", sanitizedSegments.Skip(skip));
                }, RegexOptions.IgnoreCase);
            }

            return result;
        }
        public static string SanitizePathSegment(string segment)
        {
            if (string.IsNullOrEmpty(segment))
                return string.Empty;

            return Regex.Replace(segment, @"[\s\-]", "_");
        }
        public static void CreatePathWithWriteAccess(string FilePath)
        {
            try
            {
                var PF = Environment.ExpandEnvironmentVariables(FilePath);
                Directory.CreateDirectory(PF);
            }
            catch { }
        }
        public static string MaskAllButLastAndFirst(this string input, char maskingChar = '*')
        {
            if (input.Length > 3)
            {
                var pattern = @"^(.{1})(.+)(.{1})$";
                var match = Regex.Match(input, pattern);
                var mask = new string(maskingChar, match.Groups[2].Length);
                return $"{match.Groups[1]}{mask}{match.Groups[3]}";
            }
            else
                return new string(maskingChar, input.Length);
        }
    }
}
