using Netina.Domain.Entities.Brands;

namespace Netina.AdminPanel.PWA.Pages;

public class CustomersPageViewModel(
    NavigationManager navigationManager,
    ISnackbar snackbar,
    IUserUtility userUtility,
    IRestWrapper restWrapper,
    IDialogService dialogService) : BaseViewModel<ObservableCollection<CustomerSDto>> (userUtility)
{
    public string Search = string.Empty;
    public int CurrentPage = 0;
    public int PageCount = 1;

    public override async Task InitializeAsync()
    {
        try
        {
            IsProcessing = true;
            //var dto = await restWrapper.CrudDtoApiRest<Brand, BrandSDto, Guid>(Address.BrandController)
            //    .ReadAll(0);
            //PageDto = dto;
        }
        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 SearchChanged(string search)
    {
        if (search.IsNullOrEmpty() && !Search.IsNullOrEmpty())
            await InitializeAsync();
        Search = search;
    }

    public async Task SearchAsync()
    {
        try
        {
            if (Search.IsNullOrEmpty())
                throw new AppException("نام برند برای جست جو وارد نشده است");
            IsProcessing = true;
            CurrentPage = 0;
            PageCount = 1;
            PageDto.Clear();
            //var dto = await restWrapper.BrandRestApi.ReadAll(CurrentPage, Search);
            //dto.ForEach(d => PageDto.Add(d));
            //if (PageDto.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;
        }
    }

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

            try
            {
                IsProcessing = true;

                List<BrandSDto> dto = new List<BrandSDto>();
                if (Search.IsNullOrEmpty())
                {
                    dto = await restWrapper.CrudDtoApiRest<Brand, BrandSDto, Guid>(Address.BrandController)
                        .ReadAll(CurrentPage);
                }
                else
                {
                    dto = await restWrapper.BrandRestApi.ReadAll(CurrentPage, Search);
                }

                //dto.ForEach(d => PageDto.Add(d));
                if (PageDto.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;
            }
        }
    }
}