using System.Collections.Generic;
using System.Linq;

namespace Quadarax.Foundation.Core.Data.Extensions
{
    public static class PagingExt
    {
        public static IQueryable<TEntity> Page<TEntity>(this IQueryable<TEntity> queryable, IPaging paging, int overlap = 0)
        {
            return queryable.Page(paging.IsDisabled ? null : paging.Page, paging.IsDisabled ? null :paging.PageSize, overlap);
        }
        public static IQueryable<TEntity> Page<TEntity>(this IQueryable<TEntity> queryable, int? page, int? pageSize, int overlap = 0)
        {
            if (!(page.HasValue && pageSize.HasValue))
                return queryable;

            pageSize = pageSize.GetValueOrDefault(int.MaxValue);

            return queryable.Skip(page.Value * pageSize.Value).Take(pageSize.Value + overlap);
        }

        public static IEnumerable<TEntity> Page<TEntity>(this IEnumerable<TEntity> queryable, IPaging paging, int overlap = 0)
        {
            return queryable.Page(paging.IsDisabled ? null : paging.Page, paging.IsDisabled ? null :paging.PageSize, overlap);
        }

        public static IEnumerable<TEntity> Page<TEntity>(this IEnumerable<TEntity> queryable, int? page, int? pageSize, int overlap = 0)
        {
            if (!(page.HasValue && pageSize.HasValue))
                return queryable;
            pageSize = pageSize.GetValueOrDefault(int.MaxValue);

            return queryable.Skip(page.Value * pageSize.Value).Take(pageSize.Value + overlap);
        }
    }
}
