using Indotalent.ConfigBackEnd.Interfaces;
using Indotalent.ConfigFrontEnd.Common;
using Indotalent.Features.Setting.SystemUser.Cqrs;
using Indotalent.Infrastructure.Authentication.Identity;
using Indotalent.Shared.Models;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using RestSharp;

namespace Indotalent.Features.Setting.SystemUser;

public class SystemUserService : BaseService
{
    private readonly RestClient _client;

    public SystemUserService(
            IHttpClientFactory clientFactory,
            NavigationManager nav,
            ISnackbar snackbar,
            ICurrentUserService currentUserService,
            TokenProvider tokenProvider)
            : base(clientFactory, nav, snackbar, currentUserService, tokenProvider)
    {
        _client = new RestClient(nav.BaseUri);
    }

    public async Task<ApiResponse<List<GetSystemUserListResponse>>?> GetSystemUserListAsync()
    {
        var request = new RestRequest("api/system-user", Method.Get);
        return await ExecuteWithResponseAsync<List<GetSystemUserListResponse>>(_client, request);
    }

    public async Task<ApiResponse<GetSystemUserByIdResponse>?> GetSystemUserByIdAsync(string id)
    {
        var request = new RestRequest($"api/system-user/{id}", Method.Get);
        return await ExecuteWithResponseAsync<GetSystemUserByIdResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateSystemUserResponse>?> CreateSystemUserAsync(CreateSystemUserRequest data)
    {
        var request = new RestRequest("api/system-user", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateSystemUserResponse>(_client, request);
    }

    public async Task<ApiResponse<UpdateSystemUserResponse>?> UpdateSystemUserAsync(UpdateSystemUserRequest data)
    {
        var request = new RestRequest("api/system-user/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateSystemUserResponse>(_client, request);
    }

    public async Task<bool> DeleteSystemUserByIdAsync(string id)
    {
        var request = new RestRequest($"api/system-user/delete/{id}", Method.Post);
        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }

    public async Task<ApiResponse<ChangePasswordResponse>?> ChangePasswordAsync(ChangePasswordRequest data)
    {
        var request = new RestRequest("api/system-user/change-password", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<ChangePasswordResponse>(_client, request);
    }

    public async Task<bool> SendResetLinkAsync(string userId)
    {
        var request = new RestRequest($"api/system-user/send-reset-link/{userId}", Method.Post);
        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }

    public async Task<bool> ChangeRoleAsync(ChangeRoleRequest data)
    {
        var request = new RestRequest("api/system-user/change-role", Method.Post);
        request.AddJsonBody(data);
        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }

    public async Task<ApiResponse<ChangeAvatarResponse>?> ChangeAvatarAsync(ChangeAvatarRequest data)
    {
        var request = new RestRequest("api/system-user/change-avatar", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<ChangeAvatarResponse>(_client, request);
    }


    public async Task<byte[]?> GetAvatarBlobAsync(string fileName)
    {
        var request = new RestRequest($"api/system-user/avatar-image/{fileName}", Method.Get);

        if (!string.IsNullOrEmpty(TokenProvider.Token))
        {
            request.AddHeader("Authorization", $"Bearer {TokenProvider.Token}");
        }

        if (!string.IsNullOrEmpty(CurrentUserService.UserId))
        {
            request.AddHeader("X-UserId", CurrentUserService.UserId);
        }

        var response = await _client.ExecuteAsync(request);

        return response.IsSuccessful ? response.RawBytes : null;
    }

}