<%--
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */
--%>

<c:if test="<%= useAssetEntryQuery %>">
	<liferay-asset:categorization-filter
		assetType="entries"
		portletURL="<%= portletURL %>"
	/>
</c:if>

<c:if test="<%= results.isEmpty() %>">
	<liferay-ui:empty-result-message
		message="no-entries-were-found"
	/>
</c:if>

<%
BlogsPortletInstanceSettingsHelper blogsPortletInstanceSettingsHelper = new BlogsPortletInstanceSettingsHelper(request, blogsPortletInstanceConfiguration);

Map<String, Object> contextObjects = HashMapBuilder.<String, Object>put(
	"blogsPortletInstanceConfiguration", blogsPortletInstanceConfiguration
).put(
	"blogsPortletInstanceSettingsHelper", blogsPortletInstanceSettingsHelper
).build();

List<BlogsEntry> blogsEntries = _convertToBlogEntries(results);
%>

<liferay-ddm:template-renderer
	className="<%= BlogsEntry.class.getName() %>"
	contextObjects="<%= contextObjects %>"
	displayStyle="<%= blogsPortletInstanceConfiguration.displayStyle() %>"
	displayStyleGroupId="<%= blogsPortletInstanceSettingsHelper.getDisplayStyleGroupId() %>"
	entries="<%= blogsEntries %>"
>
	<div class="widget-mode-simple">

		<%
		long[] classPKs = ListUtil.toLongArray(blogsEntries, BlogsEntry.ENTRY_ID_ACCESSOR);

		Map<Long, RatingsEntry> ratingsEntries = null;
		Map<Long, RatingsStats> ratingsStats = RatingsStatsLocalServiceUtil.getStats(BlogsEntry.class.getName(), classPKs);

		if (ratingsStats.isEmpty()) {
			ratingsEntries = Collections.emptyMap();
		}
		else {
			ratingsEntries = RatingsEntryLocalServiceUtil.getEntries(themeDisplay.getUserId(), BlogsEntry.class.getName(), classPKs);
		}

		int index = 0;

		for (Object result : results) {
			BlogsEntry entry = blogsEntries.get(index);

			AssetEntry assetEntry = null;

			if (result instanceof AssetEntry) {
				assetEntry = (AssetEntry)result;

				assetHelper.addLayoutTags(request, assetEntry.getTags());
			}

			index++;

			request.setAttribute("view_entry_content.jsp-searchContainer", searchContainer);

			request.setAttribute("view_entry_content.jsp-entry", entry);

			request.setAttribute("view_entry_content.jsp-assetEntry", assetEntry);

			request.setAttribute("view_entry_content.jsp-ratingsEntry", ratingsEntries.get(entry.getEntryId()));
			request.setAttribute("view_entry_content.jsp-ratingsStats", ratingsStats.get(entry.getEntryId()));
		%>

			<liferay-util:include page="/blogs/view_entry_content.jsp" servletContext="<%= application %>" />

		<%
		}
		%>

	</div>
</liferay-ddm:template-renderer>

<liferay-ui:search-paginator
	markupView="lexicon"
	searchContainer="<%= searchContainer %>"
/>

<%!
private List<BlogsEntry> _convertToBlogEntries(List results) throws PortalException, SystemException {
	if (results.isEmpty() || (results.get(0) instanceof BlogsEntry)) {
		return results;
	}

	List<BlogsEntry> entries = new ArrayList<>(results.size());

	for (Object result : results) {
		AssetEntry assetEntry = (AssetEntry)result;

		BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(assetEntry.getClassPK());

		entries.add(entry);
	}

	return entries;
}
%>