using PARR.API.Contracts.V1.Responses.Base;
using PARR.Domain.Common.Pagination;

namespace PARR.API.Extensions
{
    public static class PaginationExtensions
    {
        /// <summary>
        /// Заполняет TotalItems, PageNumber, PageSize, TotalPage
        /// </summary>
        /// <typeparam name="TypeResponse"></typeparam>
        /// <typeparam name="Query"></typeparam>
        /// <param name="pagedResponse"></param>
        /// <param name="paginationFilter"></param>
        /// <param name="queryAllItemsApplyFilters"></param>
        /// <returns></returns>
        public static PagedResponse<TypeResponse> GetPaginatedProps<TypeResponse, Query>(
                this PagedResponse<TypeResponse> pagedResponse,
                PaginationFilter paginationFilter,
                IQueryable<Query> queryAllItemsApplyFilters
            )
        {
            // TODO: CountAsync - подумать и затолкать в Task
            int totalItems = queryAllItemsApplyFilters.Count();//.CountAsync().GetAwaiter().GetResult();
            return CreatePaginatedResponse(totalItems, paginationFilter, pagedResponse);
        }


        /// <summary>
        /// Заполняет TotalItems, PageNumber, PageSize, TotalPage
        /// </summary>
        /// <typeparam name="TypeResponse"></typeparam>
        /// <param name="pagedResponse"></param>
        /// <param name="paginationFilter"></param>
        /// <param name="totalItems"></param>
        /// <returns></returns>
        public static PagedResponse<TypeResponse> GetPaginatedProps<TypeResponse>(
               this PagedResponse<TypeResponse> pagedResponse,
               PaginationFilter paginationFilter,
               int totalItems
           )
        {
            return CreatePaginatedResponse(totalItems, paginationFilter, pagedResponse);
        }

        private static PagedResponse<T> CreatePaginatedResponse<T>(int totalItems, PaginationFilter paginationFilter, PagedResponse<T> pagedResponse)
        {
            pagedResponse.TotalItems = totalItems;
            pagedResponse.PageNumber = paginationFilter.PageNumber >= 1 ? paginationFilter.PageNumber : (int?)null;
            pagedResponse.PageSize = paginationFilter.PageSize >= 1 ? paginationFilter.PageSize : (int?)null;

            //pagedResponse.PageNumber = paginationFilter.IsDisabledPagination 
            //                                                ? 1 
            //                                                : (paginationFilter.PageNumber >= 1 ? paginationFilter.PageNumber : (int?)null);

            //pagedResponse.PageSize = paginationFilter.IsDisabledPagination 
            //                                                ? totalItems 
            //                                                : (paginationFilter.PageSize >= 1 ? paginationFilter.PageSize : (int?)null);

            pagedResponse.TotalPage =
               (pagedResponse.TotalItems.HasValue && pagedResponse.PageSize.HasValue)
               ? (int)Math.Ceiling(pagedResponse.TotalItems.Value / (double)pagedResponse.PageSize.Value)
               : (int?)null;

            return pagedResponse;
        }
    }
}
