package applicationservice

import (
	"math"

	"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
)

// getTakeSkipPaging return the take and skip from page number and item quantity
func getTakeSkipPaging(quantity int64, page int64) (take int64, skip int64) {
	if page < 1 {
		page = 1
	}

	if quantity < 1 {
		quantity = 10
	} else if quantity > 1000 {
		quantity = 1000
	}

	take = quantity // itens per page
	skip = (page - 1) * quantity

	return take, skip
}

// buildPaginatedResult returns a paginatedResult instance
func buildPaginatedResult(list interface{}, skip int64, take int64, totalRecords int64) viewmodel.PaginatedResult {
	return viewmodel.PaginatedResult{
		List: list,
		Pagination: viewmodel.ReturnPagination{
			CurrentPage:    (skip / take) + 1,
			RecordsPerPage: take,
			TotalRecords:   totalRecords,
			TotalPages:     int64(math.Ceil(float64(totalRecords) / float64(take))),
		},
	}
}
