<%
/*
<!--

    Copyright (C) 2003-2010 eXo Platform SAS.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Affero General Public License
    as published by the Free Software Foundation; either version 3
    of the License, or (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, see<http://www.gnu.org/licenses/>.

-->
*/
%>
<%
  import org.exoplatform.portal.webui.util.Util;
  import org.exoplatform.webui.form.UIFormTextAreaInput;
  import org.exoplatform.social.webui.composer.PopupContainer;
  import org.exoplatform.social.plugin.doc.UIDocViewer;
  import org.exoplatform.social.core.service.LinkProvider;
  import org.exoplatform.social.core.space.model.Space;
  import org.exoplatform.social.webui.Utils;

  def pcontext = Util.getPortalRequestContext();
  def labelActivityHasBeenDeleted = _ctx.appRes("UIActivity.label.Activity_Has_Been_Deleted");
  def activity = uicomponent.getActivity();
  def activityDeletable = uicomponent.isActivityDeletable();
  def activityCommentAndLikable = uicomponent.isActivityCommentAndLikable();
%>

<% if (activity) { //process if not null

  def jsManager = pcontext.getJavascriptManager();
  jsManager.importJavascript("eXo.social.Util", LinkProvider.JAVASCRIPT_RESOURCE_URL);
  jsManager.importJavascript("eXo.social.PortalHttpRequest", LinkProvider.JAVASCRIPT_RESOURCE_URL);
  jsManager.importJavascript("eXo.social.webui.UIForm", LinkProvider.JAVASCRIPT_RESOURCE_URL);
  jsManager.importJavascript("eXo.social.webui.UIActivity", LinkProvider.JAVASCRIPT_RESOURCE_URL);

  def labelComment = _ctx.appRes("UIActivity.label.Comment");
  def labelLike = _ctx.appRes("UIActivity.label.Like");
  def labelUnlike = _ctx.appRes("UIActivity.label.Unlike");
  def labelSource = _ctx.appRes("UIActivity.label.Source");
  def inputWriteAComment = _ctx.appRes("UIActivity.input.Write_A_Comment");
  def labelShowAllComments = _ctx.appRes("UIActivity.label.Show_All_Comments");
  def labelHideAllComments = _ctx.appRes("UIActivity.label.Hide_All_Comments");
  def labelOnePersonLikeThis = _ctx.appRes("UIActivity.label.One_Person_Like_This");
  def labelPeopleLikeThis = _ctx.appRes("UIActivity.label.People_Like_This");
  def labelYouLikeThis = _ctx.appRes("UIActivity.label.You_Like_This");
  def labelYouAndOnePersonLikeThis = _ctx.appRes("UIActivity.label.You_And_One_Person_Like_This");
  def labelYouAndPeopleLikeThis = _ctx.appRes("UIActivity.label.You_And_People_Like_This");

  def likeActivityAction = uicomponent.event("LikeActivity", "true");
  def unlikeActivityAction = uicomponent.event("LikeActivity", "false");

  def commentList = uicomponent.getComments();
  def allComments = uicomponent.getAllComments();
  if (allComments) {
    labelShowAllComments = labelShowAllComments.replace("{0}", allComments.size() + "");
    labelHideAllComments = labelHideAllComments.replace("{0}", allComments.size() + "");

  }
  def displayedIdentityLikes = uicomponent.getDisplayedIdentityLikes();
  def identityLikesNum = 0;
  def labelLikes = null;
  def toggleDisplayLikesAction = uicomponent.event("ToggleDisplayLikes");
  def startTag = "<a onclick=\"$toggleDisplayLikesAction\" id=\"ToggleDisplayListPeopleLikes${activity.id}\" href=\"#ToggleDisplayListPeopleLikes\">";
  def endTag = "</a>";
  if (displayedIdentityLikes != null) {
    identityLikesNum = displayedIdentityLikes.length;
  }
  def commentListStatus = uicomponent.getCommentListStatus();
  def commentFormDisplayed = uicomponent.isCommentFormDisplayed();
  def likesDisplayed = uicomponent.isLikesDisplayed();
  //params for init UIActivity javascript object
  def params = """
    {activityId: '${activity.id}',
     inputWriteAComment: '$inputWriteAComment',
     commentMinCharactersAllowed: ${uicomponent.getCommentMinCharactersAllowed()},
     commentMaxCharactersAllowed: ${uicomponent.getCommentMaxCharactersAllowed()},
     commentFormDisplayed: $commentFormDisplayed,
     commentFormFocused: ${uicomponent.isCommentFormFocused()}
    }
  """
  jsManager.addOnLoadJavascript("initUIActivity${activity.id}");
  //make sures commentFormFocused is set to false to prevent any refresh to focus, only focus after post a comment
  uicomponent.setCommentFormFocused(false);
  def ownerName, ownerUri, ownerAvatar, activityContentBody, activityPostedTime;
  def listPeopleLikeBlockClass = "", listPeopleBGClass = "";

  if (!likesDisplayed) {
    listPeopleLikeBlockClass = "DisplayNone";
  }
  if (uicomponent.isLiked()) {
    if (identityLikesNum > 1) {
      labelLikes = labelYouAndPeopleLikeThis.replace("{start}", startTag).replace("{end}", endTag).replace("{0}", identityLikesNum + "");
    } else if (identityLikesNum == 1) {
      labelLikes = labelYouAndOnePersonLikeThis.replace("{start}", startTag).replace("{end}", endTag);
    } else {
      labelLikes = labelYouLikeThis;
    }
  } else {
    if (identityLikesNum > 1) {
        labelLikes = labelPeopleLikeThis.replace("{start}", startTag).replace("{end}", endTag).replace("{0}", identityLikesNum + "");
    } else if (identityLikesNum == 1) {
        labelLikes = labelOnePersonLikeThis.replace("{start}", startTag).replace("{end}", endTag);
    }
  }

  if (!labelLikes) {
   //hides diplayPeopleBG
   listPeopleBGClass = "DisplayNone";
  }

  activityContentTitle = activity.title;
  activityPostedTime = uicomponent.getPostedTimeString(_ctx, activity.postedTime);

  def ownerIdentity = uicomponent.ownerIdentity;
  if (uicomponent.isUserActivity()) {
    ownerUri = ownerIdentity.profile.url;
    ownerAvatar = ownerIdentity.profile.avatarUrl;
    ownerName = ownerIdentity.profile.fullName;
    if (!ownerAvatar) ownerAvatar= LinkProvider.PROFILE_DEFAULT_AVATAR_URL;
  } else {
    Space space = Utils.getSpaceService().getSpaceByName(ownerIdentity.remoteId);
    ownerUri = space.url;
    ownerAvatar = space.avatarUrl;
    ownerName = space.displayName;
    if (!ownerAvatar) ownerAvatar = LinkProvider.SPACE_DEFAULT_AVATAR_URL;
  }

  def dMessage = uicomponent.message;
  def dName = uicomponent.docName;
  def dLink = uicomponent.docLink;

  def downloadDocumentLabel = _ctx.appRes("UIComposerDocumentExtension.label.Download");
  def viewDocumentLabel = _ctx.appRes("UIComposerDocumentExtension.label.View_Document");
%>

<div class="UIActivity">
  <script type="text/javascript">
    function initUIActivity${activity.id}() {
      new eXo.social.webui.UIActivity($params);
    }
  </script>

  <% uiform.begin() %>
  <div class="NormalBox ClearFix">
    <a class="Avatar" title="$ownerName" href="$ownerUri">
      <img title="$ownerName" src="$ownerAvatar" alt="$ownerName">
    </a>
    <div class="ContentBox" id="ContextBox${activity.id}">
      <div class="TitleContent ClearFix">
        <div class="Text">
          <a title="$ownerName" href="$ownerUri">$ownerName</a>
        </div>
      <% if (activityDeletable) {%>
        <span onclick="<%= uicomponent.event("DeleteActivity", uicomponent.getId(), ""); %>" class="CloseContentBoxNormal" id="DeleteActivityButton${activity.id}"></span>
      <%}%>
      </div>
      <div class="Content">
        <%
          if (!dMessage.equals(null) && !dMessage.equals("")){
            %>
              <p>$dMessage</p>
            <%
          }
        %>
        <img src="/social-resources/skin/ShareImages/activity_file.png" alt="" style="padding: 0 4px 6px 10px;"/>$dName
        <a class="ColorLink" target="_blank" href="$dLink">$downloadDocumentLabel</a>
        &nbsp;|&nbsp;
        <a class="ColorLink" href="<%= uicomponent.event("ViewDocument", uicomponent.getId(), "")%>">$viewDocumentLabel</a>
      </div>
      <div class="LinkCM">
        <span class="DateTime">$activityPostedTime *</span>
      <% def toggleDisplayCommentAction = uicomponent.event('ToggleDisplayCommentForm', null, false);
         def commentLink = "";
      %>
     <% if (activityCommentAndLikable) { %>
        <a class="$commentLink" onclick="$toggleDisplayCommentAction" id="CommentLink${activity.id}" href="#comment">
          $labelComment
        </a> |
      <% if (uicomponent.isLiked()) { %>
        <a onclick="$unlikeActivityAction" id="UnLikeLink${activity.id}" href="#unlike">
          $labelUnlike
        </a>
      <% } else { %>
        <a onclick="$likeActivityAction" id="LikeLink${activity.id}" href="#like">
          $labelLike
        </a>
      <% }%>
     <% }%>
      </div>
    </div>

    <% if (identityLikesNum > 0 || uicomponent.isLiked()) {%>
      <div class="ListPeopleLikeBG $listPeopleBGClass">
          <div class="ListPeopleContent">
          <% if (!labelLikes) labelLikes = ""; %>
			      $labelLikes

            <% if (identityLikesNum > 0) {%>
            <div class="$listPeopleLikeBlockClass">
            <%
            displayedIdentityLikes.each({
              def id = Utils.getIdentityManager().getIdentity(it);
              personLikeFullName = id.profile.fullName;
              personLikeProfileUri = id.profile.url;
              personLikeAvatarImageSource = id.profile.avatarUrl;
              if (!personLikeAvatarImageSource) personLikeAvatarImageSource = LinkProvider.PROFILE_DEFAULT_AVATAR_URL;
              %>
                <a class="AvatarPeopleBG" title="$personLikeFullName" href="$personLikeProfileUri">
                  <img src="$personLikeAvatarImageSource" alt="$personLikeFullName" title="$personLikeFullName" />
                </a>
             <% })%>
            </div>
            <% }%>
          </div>
      </div>
    <%}%>
      <% if (uicomponent.commentListToggleable()) {
      def showAllCommentsAction = uicomponent.event("SetCommentListStatus", "all");
      def hideAllCommentsAction = uicomponent.event("SetCommentListStatus", "none");
      %>
      <div class="CommentListInfo">
            <% if (commentListStatus.getStatus().equals("latest") || commentListStatus.getStatus().equals("none")) { %>
              <a onclick="$showAllCommentsAction" href="#show-all-comments">
                $labelShowAllComments
              </a>
            <% } else if (commentListStatus.getStatus().equals("all")) { %>
              <a onclick="$hideAllCommentsAction" href="#hide-all-comments">
                $labelHideAllComments
              </a>
            <% } %>
      </div>
  <% } %>
  <% if (allComments.size() > 0) { %>
    <span class="DownIconCM"></span>
  <% }%>

  <%
  def commenterFullName, commenterProfileUri, commenterImageSource, commentMessage, commentPostedTime;
  def first = true, commentContentClass;
  commentList.each({
    if (first & !uicomponent.commentListToggleable()) {
      commentContentClass = "CommentContent";
      first = false;
    } else {
      commentContentClass = "";
    }
    def id = Utils.getIdentityManager().getIdentity(it.userId);
    commenterFullName = id.profile.fullName;
    commenterProfileUri = id.profile.url;
    commenterImageSource = id.profile.avatarUrl;
    if (!commenterImageSource) commenterImageSource = LinkProvider.PROFILE_DEFAULT_AVATAR_URL;
    commentMessage = it.title;
    commentPostedTime = uicomponent.getPostedTimeString(_ctx, it.postedTime);
  %>
    <div id="CommentBlock${activity.id}" class="CommentBox ClearFix">
      <a class="AvatarCM" title="$commenterFullName" href="$commenterProfileUri">
        <img src="$commenterImageSource" alt="$commenterFullName">
      </a>
      <div class="ContentBox">
        <div class="Content">
          <a href="$commenterProfileUri"><span class="Commenter">$commenterFullName<span></a>
            $commentMessage
        </div>
        <div class="DateTime">
         $commentPostedTime
        </div>
      </div>
    <%
      if (uicomponent.isCommentDeletable(it.userId)) {
    %>
      <span onclick="<%= uicomponent.event("DeleteComment", uicomponent.id, it.id); %>" class="CloseCMContentHilight"></span>
    <% } %>
    </div>
  <% }) %>

    <div class="CommentBox ClearFix" id="CommentFormBlock${activity.id}">
      <% uicomponent.renderChild(UIFormTextAreaInput.class); %>
      <input type="button" onclick="<%= uicomponent.event("PostComment") %>" value="$labelComment" class="CommentButton DisplayNone" id="CommentButton${activity.id}" />
    </div>

  </div>
  <% uiform.end() %>
</div>
<% } else { %> <!-- activity deleted -->
<div class="UIActivity Deleted">$labelActivityHasBeenDeleted</div>
<% }%>

