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

<%@ include file="/discussion/init.jsp" %>

<%
int index = GetterUtil.getInteger(request.getAttribute("liferay-comment:discussion:index"));
int initialIndex = GetterUtil.getInteger(request.getAttribute("liferay-comment:discussion:index"));
String originalNamespace = ParamUtil.getString(request, "namespace");
String randomNamespace = GetterUtil.getString(request.getAttribute("liferay-comment:discussion:randomNamespace"));
int rootIndexPage = GetterUtil.getInteger(request.getAttribute("liferay-comment:discussion:rootIndexPage"));

DiscussionRequestHelper discussionRequestHelper = new DiscussionRequestHelper(request);
DiscussionTaglibHelper discussionTaglibHelper = new DiscussionTaglibHelper(request);

Discussion discussion = CommentManagerUtil.getDiscussion(discussionTaglibHelper.getUserId(), discussionRequestHelper.getScopeGroupId(), discussionTaglibHelper.getClassName(), discussionTaglibHelper.getClassPK(), new ServiceContextFunction(request));

DiscussionComment rootDiscussionComment = (discussion == null) ? null : discussion.getRootDiscussionComment();

DiscussionCommentIterator discussionCommentIterator = (rootDiscussionComment == null) ? null : rootDiscussionComment.getThreadDiscussionCommentIterator(rootIndexPage - 1);
%>

<c:if test="<%= discussionCommentIterator != null %>">

	<%
	while (discussionCommentIterator.hasNext()) {
		rootIndexPage = discussionCommentIterator.getIndexPage();

		if (index >= (initialIndex + PropsValues.DISCUSSION_COMMENTS_DELTA_VALUE)) {
			break;
		}

		request.setAttribute("aui:form:portletNamespace", originalNamespace + randomNamespace);
		request.setAttribute("liferay-comment:discussion:depth", 0);
		request.setAttribute("liferay-comment:discussion:discussion", discussion);
		request.setAttribute("liferay-comment:discussion:discussionComment", discussionCommentIterator.next());
	%>

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

	<%
		index = GetterUtil.getInteger(request.getAttribute("liferay-comment:discussion:index"));
	}
	%>

</c:if>

<script>
	var indexInput = document.getElementById(
		'<%= originalNamespace + randomNamespace %>index'
	);

	if (indexInput) {
		indexInput.value = '<%= String.valueOf(index) %>';
	}

	var rootIndexPageInput = document.getElementById(
		'<%= originalNamespace + randomNamespace %>rootIndexPage'
	);

	if (rootIndexPageInput) {
		rootIndexPageInput.value = '<%= String.valueOf(rootIndexPage) %>';
	}

	<c:if test="<%= (rootDiscussionComment != null) && (discussion.getDiscussionCommentsCount() <= index) %>">
		var moreCommentsContainer = document.getElementById(
			'<%= HtmlUtil.escape(originalNamespace) %>moreCommentsContainer'
		);

		if (moreCommentsContainer) {
			moreCommentsContainer.classList.add('hide');
		}
	</c:if>
</script>