using AbrBlazorTools;
using Microsoft.AspNetCore.Components;
using Models.Identity;
using Newtonsoft.Json;

namespace ApiUtils;

public class UserUtils : IUserUtils
{
    private readonly IHttpClientWithLoginToken _client;

    public  UserUtils(IHttpClientWithLoginToken client)
    {
        _client = client;
    }

    public async Task<HttpResponseMessage> SendVerificationRequest(object otpModel)
    {
        return await _client.HttpPost("/api/AUTH/Login",otpModel);
    }

    public async Task<HttpResponseMessage> SendSignUpRequest(object accountModel)
    {
        return await _client.HttpPost("/api/AUTH/SignUp",accountModel);

    }
    public async Task<HttpResponseMessage> LoginWithPasswordRequest(object loginModel)
    {
        return await _client.HttpPost("/api/AUTH/LoginWithPassword",loginModel);
    }
    public async Task<HttpResponseMessage> CheckLogin(bool goToLogin = true)
    {
        return await _client.HttpGet("/api/AUTH/CheckLogin",true,goToLogin);
    }

    public async Task<HttpResponseMessage> RefreshToken(object refreshTokenModel)
    {
        return await _client.HttpPost("/api/Auth/refresh", refreshTokenModel);
    }

    public async Task<CheckLoginResponse> SaveEmailAndName(NameAndEmailModel model)
    {
        var res=await _client.HttpPost("/api/auth/SetNameAndEmail", model);
        if (!res.IsSuccessStatusCode)
            return null;
        return JsonConvert.DeserializeObject<CheckLoginResponse>(await res.Content.ReadAsStringAsync());
    }
}


public interface IUserUtils
{
    Task<HttpResponseMessage> SendVerificationRequest(object otpModel);
    Task<HttpResponseMessage> LoginWithPasswordRequest(object loginModel);
    Task<HttpResponseMessage> SendSignUpRequest(object accountModel);
    Task<HttpResponseMessage> CheckLogin(bool goToLogin = true);
    Task<HttpResponseMessage> RefreshToken(object refreshTokenModel);

    Task<CheckLoginResponse> SaveEmailAndName(NameAndEmailModel model);


}