using System;
using System.IO;

namespace ModpackLauncher.Services;

public static class FileLog
{
    private static readonly object _lock = new();

    public static string LogPath { get; } = BuildPath();

    private static string BuildPath()
    {
        var dir = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
            "BrassAndSigil"
        );
        Directory.CreateDirectory(dir);
        return Path.Combine(dir, "launcher.log");
    }

    public static void Init()
    {
        try
        {
            // Truncate per launch so we always have the most recent run.
            File.WriteAllText(LogPath,
                $"=== ModpackLauncher launched {DateTime.Now:yyyy-MM-dd HH:mm:ss} ==={Environment.NewLine}");
        }
        catch { }
    }

    public static void Write(string message)
    {
        try
        {
            lock (_lock)
            {
                File.AppendAllText(LogPath,
                    $"[{DateTime.Now:HH:mm:ss}] {message}{Environment.NewLine}");
            }
        }
        catch { }
    }
}
