<%
/**
 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
%>

<div class="message-container" style="margin-left: <%= depth * 10 %>px;">
	<a id="<portlet:namespace />message_<%= message.getMessageId() %>"></a>

	<table cellpadding="0" cellspacing="0" class="<%= className %>" id="<portlet:namespace />message_<%= message.getMessageId() %>" width="100%">
	<tr>
		<td class="user-info" rowspan="2" valign="top">
			<c:choose>
				<c:when test="<%= message.isAnonymous() %>">
					<liferay-ui:message key="anonymous" />
				</c:when>
				<c:otherwise>
					<liferay-ui:user-display
						userId="<%= message.getUserId() %>"
						userName="<%= 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="tabs1" value="recent_posts" />
							<portlet:param name="groupThreadsUserId" value="<%= String.valueOf(userDisplay.getUserId()) %>" />
						</portlet:renderURL>

						<liferay-ui:icon
							image="view"
							message="recent-posts"
							url="<%= recentPostsURL.toString() %>"
							method="get"
							label="<%= true %>"
						/>

						<c:if test="<%= (userDisplay != null) && (user.getUserId() != userDisplay.getUserId()) && 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"
										message="unban-this-user"
										url="<%= unbanUserURL.toString() %>"
										label="<%= true %>"
									/>
								</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"
										message="ban-this-user"
										url="<%= banUserURL.toString() %>"
										label="<%= true %>"
									/>
								</c:otherwise>
							</c:choose>
						</c:if>
					</liferay-ui:user-display>
				</c:otherwise>
			</c:choose>
		</td>
		<td valign="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" />"><b><%= HtmlUtil.escape(message.getSubject()) %></b></a><br />

					<div class="tags">
						<liferay-ui:tags-summary
							className="<%= MBMessage.class.getName() %>"
							classPK="<%= message.getMessageId() %>"
						/>
					</div>

					<c:choose>
						<c:when test="<%= !message.isRoot() && MBMessageFlagLocalServiceUtil.hasAnswerFlag(message.getMessageId()) %>">
							<div class="answer" id="<portlet:namespace />deleteAnswerFlag_<%= message.getMessageId() %>">
								<liferay-ui:icon
									image="checked"
									message="answer"
									label="<%= true %>"
								/>

								(<a href="javascript: <portlet:namespace />deleteAnswerFlag('<%= message.getMessageId() %>');"><liferay-ui:message key="unmark" /></a>)
							</div>
						</c:when>
					</c:choose>

					<%= dateFormatDateTime.format(message.getModifiedDate()) %>

					<%
					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") : PortalUtil.getUserName(parentMessage.getUserId(), parentMessage.getUserName());
						%>

						<%= LanguageUtil.format(pageContext, "posted-as-a-reply-to", author) %>
					</c:if>
				</div>

				<c:if test="<%= editable && (MBCategoryPermission.contains(permissionChecker, category, ActionKeys.ADD_MESSAGE) || MBCategoryPermission.contains(permissionChecker, category, ActionKeys.REPLY_TO_MESSAGE)) %>">
					<ul class="edit-controls lfr-component">

						<%
						if (!message.isRoot()) {
							MBMessage rootMessage = MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId());

							if (MBMessagePermission.contains(permissionChecker, rootMessage, ActionKeys.UPDATE) &&
								!MBMessageFlagLocalServiceUtil.hasAnswerFlag(message.getMessageId()) &&
								(MBMessageFlagLocalServiceUtil.hasQuestionFlag(rootMessage.getMessageId()) || MBMessageFlagLocalServiceUtil.hasAnswerFlag(rootMessage.getMessageId()))) {
						%>

								<li id="<portlet:namespace />addAnswerFlag_<%= message.getMessageId() %>">

									<%
									String taglibMarkAsAnswerURL = "javascript: " + renderResponse.getNamespace() + "addAnswerFlag('" + message.getMessageId() + "');";
									%>

									<liferay-ui:icon
										image="checked"
										message="mark-as-an-answer"
										url="<%= taglibMarkAsAnswerURL %>"
										label="<%= true %>"
									/>
								</li>

						<%
							}
						}
						%>

						<li>
							<portlet:renderURL var="replyURL">
								<portlet:param name="struts_action" value="/message_boards/edit_message" />
								<portlet:param name="redirect" value="<%= currentURL %>" />
								<portlet:param name="categoryId" 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" url="<%= replyURL %>" label="<%= true %>" />
						</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="categoryId" 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" message="reply-with-quote" url="<%= quoteURL %>" label="<%= true %>" />
						</li>
					</ul>
				</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 %>

				<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 valign="top">
							<b><liferay-ui:message key="attachments" />:</b>
						</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" valign="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" url="<%= topHREF %>" label="<%= true %>" />
					</li>

					<c:if test="<%= MBMessagePermission.contains(permissionChecker, message, ActionKeys.UPDATE) && MBCategoryPermission.contains(permissionChecker, category, ActionKeys.UPDATE) %>">
						<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" url="<%= editURL %>" label="<%= true %>" />
						</li>
					</c:if>

					<c:if test="<%= MBMessagePermission.contains(permissionChecker, message, ActionKeys.PERMISSIONS) && MBCategoryPermission.contains(permissionChecker, category, ActionKeys.PERMISSIONS) %>">
						<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" url="<%= permissionsURL %>" label="<%= true %>" />
						</li>
					</c:if>

					<c:if test="<%= (message.getParentMessageId() != MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID) && MBCategoryPermission.contains(permissionChecker, category, 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" message="split-thread" url="<%= splitThreadURL %>" label="<%= true %>" />
						</li>
					</c:if>


					
					<c:if test="<%= MBMessagePermission.contains(permissionChecker, message, ActionKeys.DELETE) && MBCategoryPermission.contains(permissionChecker, category, ActionKeys.DELETE) %>">
						<li>

							<%
							PortletURL categoryURL = renderResponse.createRenderURL();

							categoryURL.setParameter("struts_action", "/message_boards/view");
							categoryURL.setParameter("categoryId", 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 url="<%= deleteURL %>" label="<%= true %>" />
						</li>
					</c:if>
				</ul>
			</td>
		</tr>
	</c:if>

	</table>
</div>