/*
 * Copyright(c) 2017 Eli Belash
 * Licensed under the MIT license. See https://github.com/Nucs/JsonSettings/blob/master/LICENSE for full license information.
 */

namespace BardMusicPlayer.Pigeonhole.JsonSettings.Inline;

/// <summary>
///     Class that determines paths.
/// </summary>
internal static class Paths
{
    /// <summary>
    ///     Gives the path to windows dir, most likely to be 'C:/Windows/'
    /// </summary>
    /// <summary>
    ///     The path to the entry exe.
    /// </summary>
    public static FileInfo ExecutingExe => new (AppContext.BaseDirectory);

    /// <summary>
    ///     The path to the entry exe's directory.
    /// </summary>
    public static DirectoryInfo ExecutingDirectory
    {
        get
        {
            try
            {
                return ExecutingExe.Directory;
            }
            catch
            {
                return new DirectoryInfo(Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(AppContext.BaseDirectory).Path)) ?? string.Empty);
            }
        }
    }

    /// <summary>
    ///     Combines the file name with the dir of <see cref="Paths.ExecutingExe" />, resulting in path of a file inside the
    ///     directory of the executing exe file.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static FileInfo CombineToExecutingBase(string filename)
    {
        return ExecutingExe.DirectoryName != null ? new FileInfo(Path.Combine(ExecutingDirectory.FullName, filename)) : null;
    }

    /// <summary>
    ///     Normalizes path to prepare for comparison or storage
    /// </summary>
    public static string NormalizePath(string path, bool forComparsion = false)
    {
        const string validBackslash = "\\";
        const string invalidBackslash = "/";

        path = path
            .Replace(invalidBackslash, validBackslash)
            .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

        if (forComparsion)
        {
            path = path.ToUpperInvariant();
        }

        if (path.Contains(validBackslash))
            if (Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute))
                try
                {
                    path = Path.GetFullPath(new Uri(path).LocalPath);
                }
                catch
                {
                    // ignored
                }

        return path;
    }
}