using DocuMed.Common.Models.Mapper;

namespace DocuMed.PWA.Pages;

public class HomePageViewModel : BaseViewModel<List<MedicalHistorySDto>>
{
    private readonly IUserUtility _userUtility;
    private readonly IRestWrapper _restWrapper;
    private readonly ISnackbar _snackbar;

    public HomePageViewModel(IUserUtility userUtility,IRestWrapper restWrapper,ISnackbar snackbar)
    {
        _userUtility = userUtility;
        _restWrapper = restWrapper;
        _snackbar = snackbar;
    }

    public ApplicationUserSDto User { get; private set; } = new ApplicationUserSDto();

    

    public override async Task InitializeAsync()
    {

        try
        {
            IsProcessing = true;
            User = await _userUtility.GetUserAsync();
            var token = await _userUtility.GetBearerTokenAsync();
            var list = await _restWrapper
                .CrudDtoApiRest<MedicalHistoryLDto, MedicalHistorySDto, Guid>(Address.MedicalHistoryController)
                .ReadAll(0, token);
            PageDto = list;

        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
        }
        finally
        {

            IsProcessing = false;
        }

        await base.InitializeAsync();

    }
}