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

namespace Indotalent.Features.Utilities.Todo;

public class TodoService : BaseService
{
    private readonly RestClient _client;

    public TodoService(
            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<GetTodoListResponse>>?> GetTodoListAsync()
    {
        var request = new RestRequest("api/todo", Method.Get);
        return await ExecuteWithResponseAsync<List<GetTodoListResponse>>(_client, request);
    }

    public async Task<ApiResponse<GetTodoByIdResponse>?> GetTodoByIdAsync(string id)
    {
        var request = new RestRequest($"api/todo/{id}", Method.Get);
        return await ExecuteWithResponseAsync<GetTodoByIdResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateTodoResponse>?> CreateTodoAsync(CreateTodoRequest data)
    {
        var request = new RestRequest("api/todo", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateTodoResponse>(_client, request);
    }

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

    public async Task<ApiResponse<UpdateTodoResponse>?> UpdateTodoAsync(UpdateTodoRequest data)
    {
        var request = new RestRequest("api/todo/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateTodoResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateTodoItemResponse>?> CreateTodoItemAsync(CreateTodoItemRequest data)
    {
        var request = new RestRequest("api/todo/todo-item", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateTodoItemResponse>(_client, request);
    }

    public async Task<ApiResponse<UpdateTodoItemResponse>?> UpdateTodoItemAsync(UpdateTodoItemRequest data)
    {
        var request = new RestRequest("api/todo/todo-item/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateTodoItemResponse>(_client, request);
    }

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