using System.Net.Http.Headers;
using System.Text.Json;
using Lentia.Core.Constants;
using Lentia.Core.Utils;

namespace Lentia.Utils;

public static class MojangAPI {

    public static async Task<string> UploadSkinAsync(string filePath, string variant, string token) {
        using var content = new MultipartFormDataContent();
        
        content.Add(new StringContent(variant), "variant");

        byte[] fileBytes = await File.ReadAllBytesAsync(filePath);
        var fileContent = new ByteArrayContent(fileBytes);
        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
        
        content.Add(fileContent, "file", Path.GetFileName(filePath));

        var response = await HttpHelper.FetchAsync(
            "https://yggdrasil.azures.fr/minecraftservices/minecraft/profile/skins", 
            method: HttpMethod.Post, 
            body: content, 
            headers: new() { { "Authorization", $"Bearer {token}" } }
        );

        if (response.IsSuccessStatusCode) {
            return await response.Content.ReadAsStringAsync();
        } else {
            string errorBody = await response.Content.ReadAsStringAsync();
            throw new Exception($"Erreur serveur ({response.StatusCode}): {errorBody}");
        }
    }

    public static async Task<object> GetPlayerProfileAsync(string accessToken) {        
        var response = await HttpHelper.FetchAsync("https://yggdrasil.azures.fr/minecraftservices/minecraft/profile", headers: new() { { "Authorization", $"Bearer {accessToken}" }, { "Accept", "application/json" } } );

        if (response.IsSuccessStatusCode) {
            string jsonResponse = await response.Content.ReadAsStringAsync();
            return JsonSerializer.Deserialize<object>(jsonResponse, LauncherConstants._jsonOptions)!;
        } else {
            throw new Exception($"API Error: {response.StatusCode}");
        }
    }

    public static async Task<object> HideCapeAsync(string accessToken) {
        var response = await HttpHelper.FetchAsync("https://yggdrasil.azures.fr/minecraftservices/minecraft/profile/capes/active", method: HttpMethod.Delete, headers: new() { { "Authorization", $"Bearer {accessToken}" } } );

        if (response.IsSuccessStatusCode) {
            string jsonResponse = await response.Content.ReadAsStringAsync();
            return JsonSerializer.Deserialize<object>(jsonResponse, LauncherConstants._jsonOptions)!;
        } else {
            throw new Exception($"Échec du retrait de la cape : {response.StatusCode}");
        }
    }

    public static async Task<object> ShowCapeAsync(string accessToken, string capeId) {
        var response = await HttpHelper.FetchAsync("https://yggdrasil.azures.fr/minecraftservices/minecraft/profile/capes/active", method: HttpMethod.Put, body: new { capeId = capeId }, headers: new() { { "Authorization", $"Bearer {accessToken}" } } );

        if (response.IsSuccessStatusCode) {
            string jsonResponse = await response.Content.ReadAsStringAsync();
            return JsonSerializer.Deserialize<object>(jsonResponse, LauncherConstants._jsonOptions)!;
        } else {
            throw new Exception($"Erreur lors de l'équipement de la cape : {response.StatusCode}");
        }
    }

    public static async Task<object> ChangeUsernameAsync(string newName, string accessToken) {
        var url = $"https://yggdrasil.azures.fr/minecraftservices/minecraft/profile/name/{newName}";

        var response = await HttpHelper.FetchAsync(url, method: HttpMethod.Put, headers: new() { { "Authorization", $"Bearer {accessToken}" } } );

        if (response.IsSuccessStatusCode) {
            string jsonResponse = await response.Content.ReadAsStringAsync();
            return JsonSerializer.Deserialize<object>(jsonResponse, LauncherConstants._jsonOptions)!;
        } else {
            throw new Exception($"Erreur lors du changement de pseudo (Code: {response.StatusCode})");
        }
    }
}