using System;
using System.Collections.Generic;
using MareSynchronos.API.Dto.McdfShare;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;

namespace MareSynchronos.WebAPI;

public sealed partial class ApiController
{
    public async Task<List<McdfShareEntryDto>> McdfShareGetOwn()
    {
        if (!IsConnected) return [];
        try
        {
            return await _mareHub!.InvokeAsync<List<McdfShareEntryDto>>(nameof(McdfShareGetOwn)).ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            Logger.LogWarning(ex, "Error during {method}", nameof(McdfShareGetOwn));
            return [];
        }
    }

    public async Task<List<McdfShareEntryDto>> McdfShareGetShared()
    {
        if (!IsConnected) return [];
        try
        {
            return await _mareHub!.InvokeAsync<List<McdfShareEntryDto>>(nameof(McdfShareGetShared)).ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            Logger.LogWarning(ex, "Error during {method}", nameof(McdfShareGetShared));
            return [];
        }
    }

    public async Task McdfShareUpload(McdfShareUploadRequestDto requestDto)
    {
        if (!IsConnected) return;
        try
        {
            await _mareHub!.InvokeAsync(nameof(McdfShareUpload), requestDto).ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            Logger.LogWarning(ex, "Error during {method}", nameof(McdfShareUpload));
            throw;
        }
    }

    public async Task<McdfSharePayloadDto?> McdfShareDownload(Guid shareId)
    {
        if (!IsConnected) return null;
        try
        {
            return await _mareHub!.InvokeAsync<McdfSharePayloadDto?>(nameof(McdfShareDownload), shareId).ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            Logger.LogWarning(ex, "Error during {method}", nameof(McdfShareDownload));
            throw;
        }
    }

    public async Task<bool> McdfShareDelete(Guid shareId)
    {
        if (!IsConnected) return false;
        try
        {
            return await _mareHub!.InvokeAsync<bool>(nameof(McdfShareDelete), shareId).ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            Logger.LogWarning(ex, "Error during {method}", nameof(McdfShareDelete));
            throw;
        }
    }

    public async Task<McdfShareEntryDto?> McdfShareUpdate(McdfShareUpdateRequestDto requestDto)
    {
        if (!IsConnected) return null;
        try
        {
            return await _mareHub!.InvokeAsync<McdfShareEntryDto?>(nameof(McdfShareUpdate), requestDto).ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            Logger.LogWarning(ex, "Error during {method}", nameof(McdfShareUpdate));
            throw;
        }
    }
}
