using System.Text;

namespace YMhut.Box.WinUI;

public static class CrashLog
{
    private static readonly object Gate = new();

    public static string LogPath
    {
        get
        {
            var root = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                "YMhut Box",
                "WinUI",
                "Logs");
            Directory.CreateDirectory(root);
            return Path.Combine(root, "startup-crash.log");
        }
    }

    public static void Write(string message)
    {
        lock (Gate)
        {
            File.AppendAllText(
                LogPath,
                $"[{DateTimeOffset.Now:O}] {message}{Environment.NewLine}",
                Encoding.UTF8);
        }
    }

    public static void Write(Exception? exception)
    {
        Write(exception?.ToString() ?? "Unknown exception.");
    }
}
