namespace Lada.Framework.DTO.SmartTable
{
    /// <summary>
    /// SmartTableExtension
    /// </summary>
    public static class SmartTableExtension
    {
        /// <summary>
        /// ToSmartTableResult
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="query"></param>
        /// <param name="param"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        public static async Task<SmartTableResult<TResult>> ToSmartTableResultAsync<TModel, TResult>(this IQueryable<TModel> query, SmartTableParam param, Func<TModel, TResult> selector)
        {
            if (param == null) param = new SmartTableParam();
            if (param.Pagination == null) param.Pagination = new Pagination();
            if (param.Sort == null) param.Sort = new Sort();

            if (param.Pagination.PageIndex <= 0)
            {
                param.Pagination.PageIndex = 1;
            }
            if (param.Pagination.PageSize <= 0)
            {
                param.Pagination.PageSize = 15;
            }
            var totalRecord = 0;
            try
            {
                totalRecord = query.Count();
            }
            catch (Exception)
            {

                throw;
            }

            query = !string.IsNullOrWhiteSpace(param.Sort.Predicate) ? query.OrderByName(param.Sort.Predicate, param.Sort.Reverse) : query.OrderByName("Id", false);

            var items = query
                .Skip(param.Pagination.Start)
                .Take(param.Pagination.PageSize)
                .Select(selector).ToList();

            return new SmartTableResult<TResult>
            {
                Items = await Task.FromResult(items),
                TotalRecord = totalRecord,
                NumberOfPages = (int)Math.Ceiling((double)totalRecord / param.Pagination.PageSize)
            };
        }

        /// <summary>
        /// ToSmartTableResult2
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="query"></param>
        /// <param name="param"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        public static SmartTableResult<TResult> ToSmartTableResult2<TModel, TResult>(this IQueryable<TModel> query, SmartTableParam param, Func<TModel, TResult> selector)
        {
            if (param == null) param = new SmartTableParam();
            if (param.Pagination == null) param.Pagination = new Pagination();
            if (param.Sort == null) param.Sort = new Sort();

            if (param.Pagination.Number <= 0)
            {
                param.Pagination.Number = 1;
            }
            if (param.Pagination.PageSize <= 0)
            {
                param.Pagination.PageSize = 10;
            }
            var totalRecord = 0;
            try
            {
                totalRecord = query.Count();
            }
            catch (Exception)
            {
                //var b = "bug";
                throw;
            }

            query = !string.IsNullOrWhiteSpace(param.Sort.Predicate) ? query.OrderByName(param.Sort.Predicate, param.Sort.Reverse) : query.OrderByName("Id", true);

            var items = query
                .Skip(param.Pagination.Start)
                .Take(param.Pagination.PageSize)
                .Select(selector).ToList();

            return new SmartTableResult<TResult>
            {
                Items = items,
                TotalRecord = totalRecord,
                NumberOfPages = (int)Math.Ceiling((double)totalRecord / param.Pagination.Number)
            };
        }
    }
}
