namespace DocuMed.PWA.Pages;

public class MedicalHistoryTemplatesPageViewModel : BaseViewModel<List<MedicalHistoryTemplateSDto>>
{
    private readonly NavigationManager _navigationManager;
    private readonly IUserUtility _userUtility;
    private readonly IRestWrapper _restWrapper;
    private readonly ISnackbar _snackbar;

    public MedicalHistoryTemplatesPageViewModel(NavigationManager navigationManager,IUserUtility userUtility , IRestWrapper restWrapper,ISnackbar snackbar)
    {
        _navigationManager = navigationManager;
        _userUtility = userUtility;
        _restWrapper = restWrapper;
        _snackbar = snackbar;
    }
    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();
    }
}