using EliteBGS.Util;
using System;
using System.Text;
using System.Net.Http;
using System.Text.Json.Nodes;

namespace EliteBGS;

public class DiscordPoster {
    public static readonly int DiscordLimit = 2000;

    public static void PostToDiscord(DiscordWebhook webhook, string log, string username = "EDBGS") {
        JsonObject obj = new();
        obj.Add("content", log);
        obj.Add("username", username);

        using (var client = new HttpClient()) {
            var content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json");
            var response = client.PostAsync(webhook.Webhook, content);
            if (response == null) {
                throw new Exception("failed to post content to Discord webhook");
            }
            response.Wait();
            var resp = response.Result;
            if (!resp.IsSuccessStatusCode) {
                throw new Exception(string.Format(
                    "failed to post content to webhook {0}: {1} / {2}",
                    webhook.Name, resp.StatusCode, resp.Content.ToString()));
            }
        }
    }
}