package mil.dds.anet.beans.lists;

import io.leangen.graphql.annotations.GraphQLQuery;

import java.lang.invoke.MethodHandles;
import java.util.List;

import org.skife.jdbi.v2.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import mil.dds.anet.beans.Person;
import mil.dds.anet.beans.Report;

public class AnetBeanList<T> {

	private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	List<T> list;
	Integer pageNum;
	Integer pageSize;
	Integer totalCount;
	
	public AnetBeanList() { /*Serialization Constructor */ } 
	
	public AnetBeanList(List<T> list) {
		this(null, null, list);
		this.totalCount = list.size();
	}
	
	public AnetBeanList(Integer pageNum, Integer pageSize, List<T> list) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.list = list;
	}

	public AnetBeanList(Query<T> query, int pageNum, int pageSize, Long manualRowCount) {
		this(pageNum, pageSize, query.list());
		int resultSize = getList().size();
		if (manualRowCount != null) {
			setTotalCount(manualRowCount.intValue());
		} else if (resultSize == 0) {
			setTotalCount(0);
		} else {
			logger.debug("Bulk query context attributes are {}", query.getContext().getAttributes());
			Integer foundCount = (Integer) query.getContext().getAttribute("totalCount");
			setTotalCount(foundCount == null ? resultSize : foundCount);
		}
	}

	@GraphQLQuery(name="list")
	public List<T> getList() {
		return list;
	}
	
	public void setList(List<T> list) {
		this.list = list;
	}
	
	public Integer getPageNum() {
		return pageNum;
	}
	
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	
	public Integer getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	public Integer getTotalCount() {
		return totalCount;
	}
	
	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public static AnetBeanList<Report> getReportList(Person user, Query<Report> query, int pageNum, int pageSize) {
		final AnetBeanList<Report> results = new AnetBeanList<Report>(query, pageNum, pageSize, null);
		for (final Report report : results.getList()) {
			report.setUser(user);
		}
		return results;
	}
}

@@ -85,47 +85,8 @@
 y;%0A%0A
-import java.lang.invoke.MethodHandles;%0A
 impo
@@ -140,65 +140,8 @@
 ery;
-%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aim
@@ -243,110 +243,8 @@
  %7B%0A%0A
-%09private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());%0A%0A
 %09Lis
@@ -936,101 +936,8 @@
 e %7B%0A
-%09%09%09logger.debug(%22Bulk query context attributes are %7B%7D%22, query.getContext().getAttributes());%0A
 %09%09%09I

Remove left-over debug logging