namespace DocuMed.PWA.Pages;

public class MedicalHistoryTemplatesPageViewModel(
    NavigationManager navigationManager,
    IUserUtility userUtility,
    IRestWrapper restWrapper,
    ISnackbar snackbar)
    : BaseViewModel<List<MedicalHistoryTemplateSDto>>
{
    public void CreateMedicalHistoryTemplateClicked() => navigationManager.NavigateTo("MedicalHistoryTemplateActionPage");
    public void MedicalHistoryTemplateClicked(MedicalHistoryTemplateSDto template) => navigationManager.NavigateTo($"MedicalHistoryTemplateActionPage/{template.Id.ToString()}");

    public override async Task InitializeAsync()
    {
        try
        {
            IsProcessing = true;
            await Task.Delay(500);
            var token = await userUtility.GetBearerTokenAsync();
            var list = await restWrapper
                .CrudDtoApiRest<MedicalHistoryTemplateLDto, MedicalHistoryTemplateSDto, Guid>( Address.MedicalHistoryTemplateController)
                .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();
    }
}