<%--
/**
 * 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.
 *
 *
 *
 */
--%>

<liferay-ui:search-container
	searchContainer="<%= threadEntriesSearchContainer %>"
>
	<liferay-ui:search-container-row
		className="Object"
		escapedModel="<%= true %>"
		keyProperty="categoryId"
		modelVar="result"
	>
		<%@ include file="/message_boards/cast_result.jspf" %>

		<%
		MBMessage message = MBMessageLocalServiceUtil.fetchMBMessage(thread.getRootMessageId());

		if (message == null) {
			_log.error("Thread requires missing root message id " + thread.getRootMessageId());

			row.setSkip(true);
		}

		if (message != null) {
			message = message.toEscapedModel();

			row.setPrimaryKey(String.valueOf(thread.getThreadId()));
			row.setRestricted(!MBMessagePermission.contains(permissionChecker, message, ActionKeys.VIEW));
		}

		if (!MBThreadFlagLocalServiceUtil.hasThreadFlag(themeDisplay.getUserId(), thread)) {
			row.setCssClass("list-group-item-primary");
		}
		%>

		<liferay-portlet:renderURL varImpl="rowURL">
			<portlet:param name="mvcRenderCommandName" value="/message_boards/view_message" />
			<portlet:param name="messageId" value="<%= (message != null) ? String.valueOf(message.getMessageId()) : String.valueOf(MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) %>" />
		</liferay-portlet:renderURL>

		<liferay-ui:search-container-column-text>
			<liferay-ui:user-portrait
				userId="<%= thread.getLastPostByUserId() %>"
			/>
		</liferay-ui:search-container-column-text>

		<liferay-ui:search-container-column-text
			colspan="<%= 2 %>"
		>
			<h2 class="h5">
				<aui:a href="<%= rowURL.toString() %>">
					<c:if test="<%= message != null %>">
						<c:choose>
							<c:when test="<%= !MBThreadFlagLocalServiceUtil.hasThreadFlag(themeDisplay.getUserId(), thread) %>">
								<span class="sr-only"><liferay-ui:message key="unread-thread" /></span>

								<strong><%= message.getSubject() %></strong>
							</c:when>
							<c:otherwise>
								<%= message.getSubject() %>
							</c:otherwise>
						</c:choose>
					</c:if>
				</aui:a>

				<%
				String[] threadPriority = MBUtil.getThreadPriority(mbGroupServiceSettings, themeDisplay.getLanguageId(), thread.getPriority());
				%>

				<c:if test="<%= (threadPriority != null) && (thread.getPriority() > 0) %>">
					<div class="<%= threadPriority[1] %>" title="<%= HtmlUtil.escapeAttribute(threadPriority[0]) %>"></div>
				</c:if>

				<c:if test="<%= thread.isQuestion() %>">
					<aui:icon cssClass="icon-monospaced" image="question-circle" markupView="lexicon" message="question" />
				</c:if>
			</h2>

			<c:choose>
				<c:when test="<%= (message != null) && (thread.getMessageCount() == 1) %>">

					<%
					String messageUserName = "anonymous";

					if (!message.isAnonymous()) {
						messageUserName = message.getUserName();
					}

					Date modifiedDate = message.getModifiedDate();

					String modifiedDateDescription = LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true);
					%>

					<p class="list-group-subtext">
						<liferay-ui:message arguments="<%= new String[] {messageUserName, modifiedDateDescription} %>" key="x-modified-x-ago" />
					</p>
				</c:when>
				<c:otherwise>

					<%
					String messageUserName = "anonymous";

					if (thread.getLastPostByUserId() != 0) {
						messageUserName = HtmlUtil.escape(PortalUtil.getUserName(thread.getLastPostByUserId(), StringPool.BLANK));

						if (Validator.isNull(messageUserName)) {
							MBMessage lastThreadMessage = MBMessageLocalServiceUtil.getLastThreadMessage(thread.getThreadId(), thread.getStatus());

							messageUserName = HtmlUtil.escape(PortalUtil.getUserName(lastThreadMessage.getUserId(), lastThreadMessage.getUserName()));
						}
					}

					Date lastPostDate = thread.getLastPostDate();

					String lastPostDateDescription = LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - lastPostDate.getTime(), true);
					%>

					<p class="list-group-subtext">
						<liferay-ui:message arguments="<%= new String[] {messageUserName, lastPostDateDescription} %>" key="x-replied-x-ago" />
					</p>
				</c:otherwise>
			</c:choose>

			<c:if test="<%= (message != null) && !message.isApproved() %>">
				<span class="h6">
					<aui:workflow-status bean="<%= message %>" markupView="lexicon" model="<%= MBMessage.class %>" showIcon="<%= false %>" showLabel="<%= false %>" status="<%= message.getStatus() %>" />
				</span>
			</c:if>

			<%
			int repliesCount = Math.max(thread.getMessageCount() - 1, 0);
			long viewCount = thread.getViewCount();
			%>

			<div class="list-group-subtext mt-2">
				<clay:content-row
					noGutters="true"
				>
					<clay:content-col>
						<span class="lfr-portal-tooltip" title="<%= LanguageUtil.format(request, (repliesCount == 1) ? "x-reply" : "x-replies", repliesCount, false) %>">
							<%= repliesCount %> <clay:icon symbol="message" />
						</span>
					</clay:content-col>

					<clay:content-col>
						<span class="lfr-portal-tooltip" title="<%= LanguageUtil.format(request, (viewCount == 1) ? "x-view" : "x-views", viewCount, false) %>">
							<%= viewCount %> <clay:icon symbol="view" />
						</span>
					</clay:content-col>
				</clay:content-row>
			</div>

			<c:if test="<%= thread.isQuestion() %>">

				<%
				int threadAnswersCount = MBMessageServiceUtil.getThreadAnswersCount(thread.getGroupId(), thread.getCategoryId(), thread.getThreadId());
				%>

				<div>
					<%= threadAnswersCount %>

					<liferay-ui:message key='<%= (threadAnswersCount == 1) ? "answer" : "answers" %>' />
				</div>
			</c:if>

			<c:if test="<%= thread.isLocked() %>">
				<div>
					<aui:icon image="lock" markupView="lexicon" />

					<liferay-ui:message key="locked" />
				</div>
			</c:if>
		</liferay-ui:search-container-column-text>

		<%
		row.setObject(new Object[] {message});
		%>

		<c:if test="<%= message != null %>">
			<liferay-ui:search-container-column-jsp
				path="/message_boards/message_action.jsp"
			/>
		</c:if>
	</liferay-ui:search-container-row>

	<liferay-ui:search-iterator
		displayStyle="descriptive"
		markupView="lexicon"
		resultRowSplitter="<%= new MBResultRowSplitter() %>"
	/>
</liferay-ui:search-container>