<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<div class="message-container" style="margin-left: <%= depth * 10 %>px;">
	<a id="<portlet:namespace />message_<%= message.getMessageId() %>"></a>

	<table class="thread-message-view <%= className %>" id="<portlet:namespace />message_<%= message.getMessageId() %>">
	<tr>
		<td class="lfr-top user-info" <%= editable ? "rowspan=\"2\"" : StringPool.BLANK %>>
			<c:choose>
				<c:when test="<%= message.isAnonymous() %>">
					<liferay-ui:message key="anonymous" />
				</c:when>
				<c:otherwise>
					<liferay-ui:user-display
						userId="<%= message.getUserId() %>"
						userName="<%= HtmlUtil.escape(message.getUserName()) %>"
						displayStyle="<%= 2 %>"
					>

						<%
						MBStatsUser statsUser = MBStatsUserLocalServiceUtil.getStatsUser(scopeGroupId, message.getUserId());

						int posts = statsUser.getMessageCount();
						String[] ranks = MBUtil.getUserRank(preferences, themeDisplay.getLanguageId(), statsUser);
						%>

						<c:if test="<%= Validator.isNotNull(ranks[1]) %>">
							<span class="thread-user-role-<%= ranks[1].toLowerCase() %>"><%= ranks[1] %></span>
						</c:if>

						<c:if test="<%= Validator.isNotNull(ranks[0]) %>">
							<span class="thread-user-rank"><liferay-ui:message key="rank" />: <%= ranks[0] %></span>
						</c:if>

						<liferay-ui:message key="posts" />: <%= posts %><br />
						<liferay-ui:message key="join-date" />: <%= dateFormatDate.format(userDisplay.getCreateDate()) %><br />

						<br />

						<portlet:renderURL var="recentPostsURL">
							<portlet:param name="struts_action" value="/message_boards/view" />
							<portlet:param name="topLink" value="recent-posts" />
							<portlet:param name="groupThreadsUserId" value="<%= String.valueOf(userDisplay.getUserId()) %>" />
						</portlet:renderURL>

						<liferay-ui:icon
							image="view"
							label="<%= true %>"
							message="recent-posts"
							method="get"
							url="<%= recentPostsURL.toString() %>"
						/>

						<c:if test="<%= (userDisplay != null) && (user.getUserId() != userDisplay.getUserId()) && !PortalUtil.isCommunityAdmin(userDisplay, scopeGroupId) && MBPermission.contains(permissionChecker, scopeGroupId, ActionKeys.BAN_USER) %>">
							<br />

							<c:choose>
								<c:when test="<%= MBBanLocalServiceUtil.hasBan(scopeGroupId, userDisplay.getUserId()) %>">
									<portlet:actionURL var="unbanUserURL">
										<portlet:param name="struts_action" value="/message_boards/ban_user" />
										<portlet:param name="<%= Constants.CMD %>" value="unban" />
										<portlet:param name="redirect" value="<%= currentURL %>" />
										<portlet:param name="banUserId" value="<%= String.valueOf(userDisplay.getUserId()) %>" />
									</portlet:actionURL>

									<liferay-ui:icon
										image="../message_boards/unban_user"
										label="<%= true %>"
										message="unban-this-user"
										url="<%= unbanUserURL.toString() %>"
									/>
								</c:when>
								<c:otherwise>
									<portlet:actionURL var="banUserURL">
										<portlet:param name="struts_action" value="/message_boards/ban_user" />
										<portlet:param name="<%= Constants.CMD %>" value="ban" />
										<portlet:param name="redirect" value="<%= currentURL %>" />
										<portlet:param name="banUserId" value="<%= String.valueOf(userDisplay.getUserId()) %>" />
									</portlet:actionURL>

									<liferay-ui:icon
										image="../message_boards/ban_user"
										label="<%= true %>"
										message="ban-this-user"
										url="<%= banUserURL.toString() %>"
									/>
								</c:otherwise>
							</c:choose>
						</c:if>
					</liferay-ui:user-display>
				</c:otherwise>
			</c:choose>
		</td>
		<td class="lfr-top">
			<div class="thread-top float-container">
				<div class="subject">
					<a href="#<portlet:namespace />message_<%= message.getMessageId() %>" title="<liferay-ui:message key="permanent-link-to-this-item" />"><strong><%= HtmlUtil.escape(message.getSubject()) %></strong></a><br />

					<%
					String assetTagNames = (String)request.getAttribute("edit_message.jsp-assetTagNames");
					%>

					<div class="tags">
						<liferay-ui:asset-tags-summary
							assetTagNames="<%= assetTagNames %>"
							className="<%= MBMessage.class.getName() %>"
							classPK="<%= message.getMessageId() %>"
						/>
					</div>

					<div class="answer <%= !message.isRoot() && MBMessageFlagLocalServiceUtil.hasAnswerFlag(message.getMessageId()) ? "" : "aui-helper-hidden" %>" id="<portlet:namespace />deleteAnswerFlag_<%= message.getMessageId() %>">
						<liferay-ui:icon
							image="checked"
							label="<%= true %>"
							message="answer"
						/>

						(<a href="javascript:<portlet:namespace />deleteAnswerFlag('<%= message.getMessageId() %>');"><liferay-ui:message key="unmark" /></a>)
					</div>

					<%= dateFormatDateTime.format(message.getModifiedDate()) %>

					<c:if test="<%= (message != null) && !message.isApproved() %>">
						<aui:model-context bean="<%= message %>" model="<%= MBMessage.class %>" />

						<aui:workflow-status status="<%= message.getStatus() %>" />
					</c:if>

					<%
					MBMessage parentMessage = null;

					try {
						parentMessage = MBMessageLocalServiceUtil.getMessage(message.getParentMessageId());
					}
					catch (Exception e) {}
					%>

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

						<%
						PortletURL parentMessageURL = renderResponse.createRenderURL();

						parentMessageURL.setParameter("struts_action", "/message_boards/view_message");
						parentMessageURL.setParameter("messageId", String.valueOf(parentMessage.getMessageId()));

						String author = parentMessage.isAnonymous() ? LanguageUtil.get(pageContext, "anonymous") : HtmlUtil.escape(PortalUtil.getUserName(parentMessage.getUserId(), parentMessage.getUserName()));
						%>

						<%= LanguageUtil.format(pageContext, "posted-as-a-reply-to", author) %>
					</c:if>
				</div>

				<c:if test="<%= editable %>">

					<%
					boolean showAnswerFlag = false;
					boolean hasReplyPermission = MBCategoryPermission.contains(permissionChecker, scopeGroupId, message.getCategoryId(), ActionKeys.REPLY_TO_MESSAGE);

					if (!message.isRoot()) {
						MBMessage rootMessage = MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId());

						showAnswerFlag = MBMessagePermission.contains(permissionChecker, rootMessage, ActionKeys.UPDATE) && !MBMessageFlagLocalServiceUtil.hasAnswerFlag(message.getMessageId()) && (MBMessageFlagLocalServiceUtil.hasQuestionFlag(rootMessage.getMessageId()) || MBMessageFlagLocalServiceUtil.hasAnswerFlag(rootMessage.getMessageId()));
					}
					%>

					<c:if test="<%= showAnswerFlag || hasReplyPermission %>">
						<ul class="edit-controls lfr-component">
							<li class="<%= showAnswerFlag ? "" : "aui-helper-hidden" %>" id="<portlet:namespace />addAnswerFlag_<%= message.getMessageId() %>">

								<%
								String taglibMarkAsAnswerURL = "javascript:" + renderResponse.getNamespace() + "addAnswerFlag('" + message.getMessageId() + "');";
								%>

								<liferay-ui:icon
									image="checked"
									label="<%= true %>"
									message="mark-as-an-answer"
									url="<%= taglibMarkAsAnswerURL %>"
								/>
							</li>

							<c:if test="<%= hasReplyPermission && !thread.isLocked() %>">
								<li>
									<portlet:renderURL var="replyURL">
										<portlet:param name="struts_action" value="/message_boards/edit_message" />
										<portlet:param name="redirect" value="<%= currentURL %>" />
										<portlet:param name="mbCategoryId" value="<%= String.valueOf(message.getCategoryId()) %>" />
										<portlet:param name="threadId" value="<%= String.valueOf(message.getThreadId()) %>" />
										<portlet:param name="parentMessageId" value="<%= String.valueOf(message.getMessageId()) %>" />
									</portlet:renderURL>

									<liferay-ui:icon
										image="reply"
										label="<%= true %>"
										url="<%= replyURL %>"
									/>
								</li>
								<li>
									<portlet:renderURL var="quoteURL">
										<portlet:param name="struts_action" value="/message_boards/edit_message" />
										<portlet:param name="redirect" value="<%= currentURL %>" />
										<portlet:param name="mbCategoryId" value="<%= String.valueOf(message.getCategoryId()) %>" />
										<portlet:param name="threadId" value="<%= String.valueOf(message.getThreadId()) %>" />
										<portlet:param name="parentMessageId" value="<%= String.valueOf(message.getMessageId()) %>" />
										<portlet:param name="quote" value="true" />
									</portlet:renderURL>

									<liferay-ui:icon
										image="quote"
										label="<%= true %>"
										message="reply-with-quote"
										url="<%= quoteURL %>"
									/>
								</li>
								<li>

									<%
									String taglibQuickReplyURL = "javascript:" + renderResponse.getNamespace() + "addQuickReply('reply', '" + message.getMessageId() + "');";
									%>

									<liferay-ui:icon
										image="bottom"
										label="<%= true %>"
										message="quick-reply"
										url="<%= taglibQuickReplyURL %>"
									/>
								</li>
							</c:if>
						</ul>
					</c:if>
				</c:if>
			</div>

			<div class="thread-body">

				<%
				/*String msgBody = message.getBody();

				String codeStart = "<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->";
				String codeEnd = "<!--c2--></div><!--ec2-->";

				while (true) {
					int x = msgBody.indexOf(codeStart);
					int y = msgBody.indexOf(codeEnd, x);

					if ((x == -1) || (y == -1)) {
						break;
					}

					String code = msgBody.substring(x + codeStart.length(), y);

					code = StringUtil.replace(code, "<br />", "\n");
					code = StringUtil.replace(code, "&nbsp;", " ");
					code = StringUtil.replace(code, "&#60;", "<");
					code = StringUtil.replace(code, "&#62;", ">");

					msgBody = msgBody.substring(0, x) + "[code]" + code + "[/code]" + msgBody.substring(y + codeEnd.length(), msgBody.length());
				}

				String quoteBegin1 = "<!--QuoteBegin-";
				String quoteBegin2 = "<!--QuoteEBegin-->";

				while (true) {
					int x = msgBody.indexOf(quoteBegin1);
					int y = msgBody.indexOf(quoteBegin2, x);

					if ((x == -1) || (y == -1)) {
						break;
					}

					String replace = msgBody.substring(x, y + quoteBegin2.length());

					msgBody = StringUtil.replace(msgBody, replace, "[quote]");
				}

				quoteBegin1 = "<!--quoteo(";
				quoteBegin2 = "<!--quotec-->";

				while (true) {
					int x = msgBody.indexOf(quoteBegin1);
					int y = msgBody.indexOf(quoteBegin2, x);

					if ((x == -1) || (y == -1)) {
						break;
					}

					String replace = msgBody.substring(x, y + quoteBegin2.length());

					msgBody = StringUtil.replace(msgBody, replace, "[quote]");
				}

				String quoteEnd = "<!--QuoteEnd--></div><!--QuoteEEnd-->";

				msgBody = StringUtil.replace(msgBody, quoteEnd, "[/quote]");

				String urlStart = "http://forums.liferay.com/index.php";

				while (true) {
					int x = msgBody.indexOf(urlStart);

					if (x == -1) {
						break;
					}

					int spacePos = msgBody.indexOf(" ", x);
					int singleQuotePos = msgBody.indexOf("'", x);
					int doubleQuotePos = msgBody.indexOf("\"", x);
					int lessThanPos = msgBody.indexOf("<", x);

					int y = -1;

					if (spacePos != -1) {
						y = spacePos;
					}

					if ((y == -1) || ((singleQuotePos != -1) && (y > singleQuotePos))) {
						y = singleQuotePos;
					}

					if ((y == -1) || ((doubleQuotePos != -1) && (y > doubleQuotePos))) {
						y = doubleQuotePos;
					}

					if ((y == -1) || ((lessThanPos != -1) && (y > lessThanPos))) {
						y = lessThanPos;
					}

					if (y == -1) {
						break;
					}

					String url = msgBody.substring(x, y);

					String entryStart = "&#entry";

					int entryIdPos = url.indexOf(entryStart);

					if (entryIdPos != -1) {
						long messageId = GetterUtil.getLong(url.substring(entryIdPos + entryStart.length(), url.length()));

						url = "http://www.liferay.com/web/guest/devzone/forums/message_boards/message/" + messageId;
					}
					else {
						String topicId = HttpUtil.getParameter(url, "showtopic", false);

						if (Validator.isNotNull(topicId)) {
							url = "http://www.liferay.com/web/guest/devzone/forums/message_boards/topic/" + topicId;
						}
						else {
							break;
						}
					}

					msgBody = msgBody.substring(0, x) + url + msgBody.substring(y, msgBody.length());
				}

				msgBody = StringUtil.replace(msgBody, "style_emoticons/<#EMO_DIR#>", "@theme_images_path@/emoticons");

				String emoticonsPath = "@theme_images_path@/emoticons";

				int x = 0;
				x = msgBody.indexOf(emoticonsPath, x);

				while (x != -1) {
					int y = msgBody.indexOf(".gif", x);

					String emotionImage = msgBody.substring(x + emoticonsPath.length() + 1, y) + ".gif";

					x = msgBody.indexOf(emoticonsPath, x + 1);
				}*/

				String msgBody = BBCodeUtil.getHTML(message);

				msgBody = StringUtil.replace(msgBody, "@theme_images_path@/emoticons", themeDisplay.getPathThemeImages() + "/emoticons");
				%>

				<%= msgBody %>

				<liferay-ui:custom-attributes-available className="<%= MBMessage.class.getName() %>">
					<div class="custom-attributes">
						<liferay-ui:custom-attribute-list
							className="<%= MBMessage.class.getName() %>"
							classPK="<%= (message != null) ? message.getMessageId() : 0 %>"
							editable="<%= false %>"
							label="<%= true %>"
						/>
					</div>
				</liferay-ui:custom-attributes-available>

				<c:if test="<%= message.isAttachments() %>">

					<%
					String[] attachmentsFiles = message.getAttachmentsFiles();

					for (int j = 0; j < attachmentsFiles.length; j++) {
						String fileName = FileUtil.getShortFileName(attachmentsFiles[j]);

						if (StringUtil.endsWith(fileName, ".gif") || StringUtil.endsWith(fileName, ".jpg") || StringUtil.endsWith(fileName, ".png")) {
					%>

							<div>
								<img alt="<liferay-ui:message key="attachment" />" src="<%= themeDisplay.getPathMain() %>/message_boards/get_message_attachment?messageId=<%= message.getMessageId() %>&attachment=<%= HttpUtil.encodeURL(fileName) %>" />
							</div>

							<br />

					<%
						}
					}
					%>

					<table class="lfr-table">
					<tr>
						<td class="lfr-top">
							<strong><liferay-ui:message key="attachments" />:</strong>
						</td>
						<td>

							<%
							for (int j = 0; j < attachmentsFiles.length; j++) {
								String fileName = FileUtil.getShortFileName(attachmentsFiles[j]);
								long fileSize = DLServiceUtil.getFileSize(company.getCompanyId(), CompanyConstants.SYSTEM, attachmentsFiles[j]);
							%>

								<a href="<portlet:actionURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>"><portlet:param name="struts_action" value="/message_boards/get_message_attachment" /><portlet:param name="messageId" value="<%= String.valueOf(message.getMessageId()) %>" /><portlet:param name="attachment" value="<%= fileName %>" /></portlet:actionURL>"><%= fileName %></a> (<%= TextFormatter.formatKB(fileSize, locale) %>k)<%= (j < (attachmentsFiles.length - 1)) ? ", " : "" %>

							<%
							}
							%>

						</td>
					</tr>
					</table>
				</c:if>
			</div>
		</td>
	</tr>

	<c:if test="<%= editable %>">
		<tr>
			<td class="thread-bottom">
				<c:if test="<%= enableRatings %>">
					<liferay-ui:ratings
						className="<%= MBMessage.class.getName() %>"
						classPK="<%= message.getMessageId() %>"
						type="thumbs"
					/>
				</c:if>

				<c:if test="<%= enableFlags %>">
					<liferay-ui:flags
						className="<%= MBMessage.class.getName() %>"
						classPK="<%= message.getMessageId() %>"
						contentTitle="<%= message.getSubject() %>"
						reportedUserId="<%= message.getUserId() %>"
					/>
				</c:if>

				<ul class="edit-controls lfr-component">
					<li>

						<%
						String topHREF = "#" + renderResponse.getNamespace() + "message_0";
						%>

						<liferay-ui:icon
							image="top"
							label="<%= true %>"
							url="<%= topHREF %>"
						/>
					</li>

					<c:if test="<%= MBMessagePermission.contains(permissionChecker, message, ActionKeys.UPDATE) && !thread.isLocked() %>">
						<li>
							<portlet:renderURL var="editURL">
								<portlet:param name="struts_action" value="/message_boards/edit_message" />
								<portlet:param name="redirect" value="<%= currentURL %>" />
								<portlet:param name="messageId" value="<%= String.valueOf(message.getMessageId()) %>" />
							</portlet:renderURL>

							<liferay-ui:icon
								image="edit"
								label="<%= true %>"
								url="<%= editURL %>"
							/>
						</li>
					</c:if>

					<c:if test="<%= MBMessagePermission.contains(permissionChecker, message, ActionKeys.PERMISSIONS) && !thread.isLocked() %>">
						<li>
							<liferay-security:permissionsURL
								modelResource="<%= MBMessage.class.getName() %>"
								modelResourceDescription="<%= HtmlUtil.escape(message.getSubject()) %>"
								resourcePrimKey="<%= String.valueOf(message.getMessageId()) %>"
								var="permissionsURL"
							/>

							<liferay-ui:icon
								image="permissions"
								label="<%= true %>"
								url="<%= permissionsURL %>"
							/>
						</li>
					</c:if>

					 <c:if test="<%= (message.getParentMessageId() != MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) && MBCategoryPermission.contains(permissionChecker, scopeGroupId, category.getCategoryId(), ActionKeys.MOVE_THREAD) %>">
						<li>
							<portlet:renderURL var="splitThreadURL">
								<portlet:param name="struts_action" value="/message_boards/split_thread" />
								<portlet:param name="redirect" value="<%= currentURL %>" />
								<portlet:param name="messageId" value="<%= String.valueOf(message.getMessageId()) %>" />
							</portlet:renderURL>

							<liferay-ui:icon
								image="unlink"
								label="<%= true %>"
								message="split-thread"
								url="<%= splitThreadURL %>"
							/>
						</li>
					</c:if>

					<c:if test="<%= MBMessagePermission.contains(permissionChecker, message, ActionKeys.DELETE) && !thread.isLocked() %>">
						<li>

							<%
							PortletURL categoryURL = renderResponse.createRenderURL();

							categoryURL.setParameter("struts_action", "/message_boards/view");
							categoryURL.setParameter("mbCategoryId", String.valueOf(message.getCategoryId()));
							%>

							<portlet:actionURL var="deleteURL">
								<portlet:param name="struts_action" value="/message_boards/edit_message" />
								<portlet:param name="<%= Constants.CMD %>" value="<%= Constants.DELETE %>" />
								<portlet:param name="redirect" value="<%= categoryURL.toString() %>" />
								<portlet:param name="messageId" value="<%= String.valueOf(message.getMessageId()) %>" />
							</portlet:actionURL>

							<liferay-ui:icon-delete
								label="<%= true %>"
								url="<%= deleteURL %>"
							/>
						</li>
					</c:if>
				</ul>
			</td>
		</tr>
	</c:if>

	</table>
</div>