using NetinaShop.Domain.Entities.Users;

namespace NetinaShop.AdminPanel.PWA.Pages;

public class NewsletterManagementPageViewModel : BaseViewModel
{


    private readonly ISnackbar _snackbar;
    private readonly IUserUtility _userUtility;
    private readonly IDialogService _dialogService;
    private readonly IRestWrapper _restWrapper;

    public ObservableCollection<NewsletterMemberSDto> Members { get; set; } = new ObservableCollection<NewsletterMemberSDto>();

    public int CurrentPage = 0;
    public int PageCount = 1;

    public NewsletterManagementPageViewModel(ISnackbar snackbar, IUserUtility userUtility, IRestWrapper restWrapper, IDialogService dialogService)
    {
        _snackbar = snackbar;
        _userUtility = userUtility;
        _restWrapper = restWrapper;
        _dialogService = dialogService;
    }

    public override async Task InitializeAsync()
    {
        try
        {
            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");
            IsProcessing = true;
            Members.Clear();
            var dto = await _restWrapper.CrudDtoApiRest<NewsletterMember, NewsletterMemberSDto, Guid>(Address.NewsletterMemberController)
                .ReadAll(CurrentPage, token);
            dto.ForEach(d => Members.Add(d));
            if (Members.Count == 20)
                PageCount = 2;
        }
        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();
    }

    public async Task ChangePageAsync(int page)
    {
        CurrentPage = page - 1;
        if (CurrentPage > PageCount - 2)
        {

            try
            {
                IsProcessing = true;

                var token = await _userUtility.GetBearerTokenAsync();
                if (token == null)
                    throw new Exception("Token is null");
                List<NewsletterMemberSDto> dto = await _restWrapper.CrudDtoApiRest<NewsletterMember, NewsletterMemberSDto, Guid>(Address.NewsletterMemberController)
                    .ReadAll(CurrentPage, token);


                dto.ForEach(d => Members.Add(d));
                if (Members.Count % 20 == 0)
                    PageCount = CurrentPage + 2;

            }
            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;
            }
        }
    }

}