<%
  import java.util.Locale;
  import java.text.DateFormat;
  import org.exoplatform.portal.webui.util.Util;
  import org.exoplatform.wiki.mow.api.WikiNodeType;
  import org.exoplatform.web.application.Parameter;
  import org.exoplatform.wiki.webui.UIWikiPortlet;

  Locale currentLocale = Util.getPortalRequestContext().getLocale();
  DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, currentLocale);
%>
<div class="UIWikiPageVersionsCompare" id="$uicomponent.id">
<%
  String showHistory = uicomponent.SHOW_HISTORY;
  String anchor = "#" + showHistory;
  String showHistoryLinkId = UIWikiPortlet.WIKI_PORTLET_ACTION_PREFIX + showHistory;;
  String showHistoryLink = uicomponent.event(uicomponent.SHOW_HISTORY);
  String fromVersionName = uicomponent.getFromVersion().getName();
  String fromVersionViewLink = uicomponent.event(uicomponent.VIEW_REVISION, fromVersionName);
  String fromVersionAuthor = uicomponent.getFromVersion().getNTFrozenNode().getAuthor();
  fromVersionAuthor = (fromVersionAuthor != null) ? fromVersionAuthor : _ctx.appRes("UIWikiPortlet.label.Anonymous");
  String fromUpdatedDate = df.format(uicomponent.getFromVersion().getNTFrozenNode().getUpdatedDate());
  String previousCompare = "";
  String previousCompareLink = "";
  Parameter[] parameters = new Parameter[2];
  if(!"1".equals(fromVersionName)){
    String previous = String.valueOf(Integer.valueOf(fromVersionName) - 1);
    previousCompare = "<< " + _ctx.appRes("UIWikiPageVersionsCompare.label.PreviousAndNextCompare").replace("{0}", "<span style=\"font-weight: bold;\">" + previous + "</span> to <span style=\"font-weight: bold;\">" + fromVersionName + "</span>");
    parameters[0] = new Parameter(uicomponent.FROM_PARAM, previous);
    parameters[1] = new Parameter(uicomponent.TO_PARAM, fromVersionName);
    previousCompareLink = uicomponent.event(uicomponent.COMPARE_ACTION, null, parameters);
  }
  
  String toVersionName = uicomponent.getToVersion().getName();
  String toVersionViewLink = uicomponent.event(uicomponent.VIEW_REVISION, toVersionName);
  String toVersionAuthor = uicomponent.getToVersion().getNTFrozenNode().getAuthor();
  toVersionAuthor = (toVersionAuthor != null) ? toVersionAuthor : _ctx.appRes("UIWikiPortlet.label.Anonymous");
  String toUpdatedDate = df.format(uicomponent.getToVersion().getNTFrozenNode().getUpdatedDate());
  String nextCompare = "";
  String nextCompareLink = "";
  if(!toVersionName.equals(uicomponent.getCurrentVersionIndex())){
    String next = String.valueOf(Integer.valueOf(toVersionName) + 1);
    nextCompare = _ctx.appRes("UIWikiPageVersionsCompare.label.PreviousAndNextCompare").replace("{0}", "<span style=\"font-weight: bold;\">" + toVersionName + "</span> to <span style=\"font-weight: bold;\">" + next + "</span>") + " >>";
    parameters[0] = new Parameter(uicomponent.FROM_PARAM, toVersionName);
    parameters[1] = new Parameter(uicomponent.TO_PARAM, next);
    nextCompareLink = uicomponent.event(uicomponent.COMPARE_ACTION, null, parameters);
  } else {
    toVersionName = "Current";
  }
  
  String fromChangeVersionInfoLabel = _ctx.appRes("UIWikiPageVersionsCompare.label.ChangeVersionInfo");
  fromChangeVersionInfoLabel = fromChangeVersionInfoLabel.replace("{0}", fromVersionName +  "</span>");
  fromChangeVersionInfoLabel = fromChangeVersionInfoLabel.replace("{1}", "<span class=\"Admin\">" + fromVersionAuthor + "</span>");
  String toChangeVersionInfoLabel = _ctx.appRes("UIWikiPageVersionsCompare.label.ChangeVersionInfo");
  toChangeVersionInfoLabel = toChangeVersionInfoLabel.replace("{0}", toVersionName +  "</span>");
  toChangeVersionInfoLabel = toChangeVersionInfoLabel.replace("{1}", "<span class=\"Admin\">" + toVersionAuthor + "</span>");
  String wordAddedRemovedLabel = _ctx.appRes("UIWikiPageVersionsCompare.label.this-word-was-added-removed");
  wordAddedRemovedLabel = wordAddedRemovedLabel.replace("{0}", "</span><span class=\"LineRemoved\">");
  wordAddedRemovedLabel = wordAddedRemovedLabel.replace("{1}", "</span><span>");
  wordAddedRemovedLabel = wordAddedRemovedLabel.replace("{2}", "</span><span class=\"LineAdded\">");
  wordAddedRemovedLabel = wordAddedRemovedLabel.replace("{3}", "</span>");
%>
  <div class="ComparedForm ClearFix">
		<div class="ChangeVersionForm">
			<span class="ChangeVersionInfo"><%= fromChangeVersionInfoLabel %>
			<div class="DateChange"><%= _ctx.appRes("UIWikiPageVersionsCompare.label.DateChange").replace("{0}", fromUpdatedDate); %>.</div>
			<div class="Changes" onclick="$previousCompareLink"><%= previousCompare %></div>
		</div>
		<div class="ComparedWith"> <%= _ctx.appRes("UIWikiPageVersionsCompare.label.ComparedWith"); %> </div>
		<div class="ChangeVersionForm">
			<span class="ChangeVersionInfo"><%= toChangeVersionInfoLabel %>
			<div class="DateChange"><%= _ctx.appRes("UIWikiPageVersionsCompare.label.DateChange").replace("{0}", toUpdatedDate); %>.</div>
			<div class="Changes" onclick="$nextCompareLink"><%= nextCompare %></div>
		</div>
		<div class="ChangeVersionForm" style="float: right; width: 220px;">
			<span class="ChangeVersionInfo"><%= _ctx.appRes("UIWikiPageVersionsCompare.label.Key"); %></span><br>
			<span class="LineRemoved"><%= _ctx.appRes("UIWikiPageVersionsCompare.label.this-line-was-removed"); %></span><br>
			<span><%= wordAddedRemovedLabel %><br>
			<span class="LineAdded"><%= _ctx.appRes("UIWikiPageVersionsCompare.label.this-line-was-added"); %></span>
		</div>
	</div>
	
	<div class="ChangedForm">
		<div class="Label"><span style="font-weight: bold;"><%= _ctx.appRes("UIWikiPageVersionsCompare.label.Changes").replace("{0}",  String.valueOf(uicomponent.changes)); %></span>
		<a style="Color:#4175B2;" href="javascript:eXo.wiki.UIWikiAjaxRequest.makeNewHash('#$showHistory');"> <%= _ctx.appRes("UIWikiPageVersionsCompare.label.ViewPageHistory"); %></a></div>
		<a style="display:none" id="$showHistoryLinkId" onclick="$showHistoryLink"></a>
		<%= uicomponent.differencesAsHTML %>
	</div>

</div>