using System.Globalization;
using CsvHelper;

namespace TelegramMessageCounter;

public static class ResultWriter
{
    public static void SaveStatsToTxt(List<CounterInfo> infos, string path = "results.txt")
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.Write($"Saving results to {path}...");
        string fileData = "";
        float multiplier = 0;
        foreach (CounterInfo info in infos)
        {
            fileData += "username: " + info.TargetUsername + "\n" +
                        "message multiplier (You/Target): " + info.MessageMultiplier + "\n" + 
                        "symbol multiplier (You/Target): " + info.SymbolsMultiplier + "\n" + 
                        "total messages: " + info.FullMessageCount + "\n" +
                        "your messages: " + info.UserMessageCount + "\n" +
                        "target messages: " + info.TargetMessageCount + "\n" +
                        "your symbols in messages: " + info.UserSymbolsCount + "\n" +
                        "target symbols in messages: " + info.TargetSymbolsCount + "\n-----\n";
            multiplier += info.MessageMultiplier;
        }
        multiplier /= infos.Count;
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("[DONE]");
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine($"Middle result is: {multiplier}");
        File.WriteAllText(path, fileData);
        Console.WriteLine("Goodbye!");
    }

    public static void SaveStatsToCsv(List<CounterInfo> infos, string path = "results.csv")
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.Write($"Saving results to {path}...");
        
        using (var writer = new StreamWriter(path))
        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
        {
            csv.WriteRecords(infos);
        }
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("[DONE]");
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Goodbye!");
    }
}