using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;

namespace e_suite.Service.EFlowSync.EflowAPI;

public class DeleteArgs
{
    public string UserName { get; set; } = string.Empty;
    public string Reason { get; set; } = string.Empty;
    public bool Cascade { get; set; }
}

public class RestoreArgs
{
    public string Username { get; set; } = string.Empty;
    public string Reason { get; set; } = string.Empty;
    public bool Cascade { get; set; }
}

public class UpdateCategoryArgs
{
    public string Id { get; set; } = string.Empty;
    public string CategoryName { get; set; } = string.Empty;
}

public class EFlowCategories : EFlowApiBase, IEFlowCategories
{
    private readonly string _ePrintUser = "e-print";

    public EFlowCategories(IConfiguration configuration) : base(configuration)
    {
    }
    
    public async Task<CategoryInfo> GetCategories(string categoryId)
    {
        var address = $"api/v1/categories/{categoryId}";

        var result = await Client.GetAsync(address);
        result.EnsureSuccessStatusCode();

        var jsonString = await result.Content.ReadAsStringAsync();
        var jsonBytes = Encoding.ASCII.GetBytes(jsonString);
        var endResult = JsonSerializer.Deserialize<CategoryInfo>(jsonBytes);
        return endResult!;
    }

    public async Task PostCategory(CategoryInfo category)
    {
        var address = $"api/v1/categories";

        var form = new Dictionary<string, string>
        {
            { "Id", category.Id },
            { "ParentId", category.ParentId },
            { "CategoryName", category.DisplayName }

        };

        var result = await Client.PostAsync(address, new FormUrlEncodedContent(form));
        result.EnsureSuccessStatusCode();
    }

    public async Task RestoreCategory(CategoryInfo category)
    {
        var address = $"api/v1/restore?id={Uri.EscapeDataString(category.Id)}";

        var deleteArgs = new RestoreArgs
        {
            Username = _ePrintUser,
            Reason = "e-Print Synchronisation",
            Cascade = false
        };

        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri(address, UriKind.Relative),
            Content = new StringContent(JsonSerializer.Serialize(deleteArgs), Encoding.UTF8, "application/json")

        };

        var result = await Client.SendAsync(request);
        result.EnsureSuccessStatusCode();
    }

    public async Task UpdateCategory(CategoryInfo category)
    {
        var address = $"api/v1/categories";

        var deleteArgs = new UpdateCategoryArgs
        {
            Id = category.Id,
            CategoryName = category.DisplayName
        };

        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Put,
            RequestUri = new Uri(address, UriKind.Relative),
            Content = new StringContent(JsonSerializer.Serialize(deleteArgs), Encoding.UTF8, "application/json")

        };

        var result = await Client.SendAsync(request);
        result.EnsureSuccessStatusCode();
    }

    public async Task DeleteCategory(CategoryInfo category)
    {
        var address = $"api/v1/categories?id={Uri.EscapeDataString(category.Id)}";

        var deleteArgs = new DeleteArgs
        {
            UserName = _ePrintUser,
            Reason = "e-Print Synchronisation",
            Cascade = true
        };

        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Delete,
            RequestUri = new Uri(address, UriKind.Relative),
            Content = new StringContent(JsonSerializer.Serialize(deleteArgs), Encoding.UTF8, "application/json")

        };

        var result = await Client.SendAsync(request);
        result.EnsureSuccessStatusCode();
    }

}