<%
/*
<!--

    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();
  def streamOwner = activity.getStreamOwner();
%>

<% 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,
     allCommentSize: ${allComments.size()},
     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, activityPostedTimeInSpace;
  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 = LinkProvider.getUserProfileUri(ownerIdentity.getRemoteId());
    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 downloadFileLabel = _ctx.appRes("UIComposerDocumentExtension.label.Download");
  def viewFileLabel = _ctx.appRes("UIComposerDocumentExtension.label.View_File");
  def gotoFolderLabel = _ctx.appRes("UIComposerDocumentExtension.label.Goto_Folder");
%>

<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">
          <span class="BorderTop"></span>
      <span class="BorderBottom"></span>
    </a>
    <div class="ContentBox" id="ContextBox${activity.id}">
      <div id="ActivityContextBox${activity.id}">
      <h5 class="TitleContent ClearFix">
        <div class="Text">
          <h5><a title="$ownerName" href="$ownerUri">$ownerName</a></h5>
        </div>
      <% if (activityDeletable) {%>
        <span onclick="<%= uicomponent.event("DeleteActivity", uicomponent.getId(), ""); %>" class="CloseContentBoxNormal" id="DeleteActivityButton${activity.id}"></span>
      <%}%>
      </h5>
    
         <%
          if (!dMessage.equals(null) && !dMessage.equals("")){
            %>
              <div class="TitleBlock">$dMessage</div>
            <%
          }
        %>
        
        <div class="BoxGray">
          <% 
            if(uicomponent.isPreviewable()) {
          %>
              <a href="<%= uicomponent.event("ViewDocument")%>" title="$dName">      
                <img src="<%= uicomponent.docThumbnail %>" style="max-height: 300px; max-width: 300px;" />
              </a>          
          <%
            } else {
          %>
        $dName
      <%
            }
          %>
      </div>
      <div class="DocActivityIcon">
        <% if (uicomponent.isUserActivity() && uicomponent.isSpaceStreamOwner() && !uicomponent.isUISpaceActivitiesDisplay()) {%>
            <%
              Space spaceSource = Utils.getSpaceService().getSpaceByPrettyName(streamOwner);
                    def spaceSourceURI = LinkProvider.getSpaceUri(spaceSource.url);
                    def spaceName = spaceSource.displayName;
                    activityPostedTimeInSpace = uicomponent.getPostedTimeInSpaceString(_ctx, activity.postedTime);
          %>
          <span class="DateTime">$activityPostedTimeInSpace <b><a class="Text" title="$spaceName" href="$spaceSourceURI">$spaceName</a></b> *</span>
        <%} else {%>
          <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> |
      <% }%>
     <% }%>
        <a class="ColorLink" href="<%= uicomponent.event("DownloadDocument")%>">$downloadFileLabel</a> |
        <a class="ColorLink" href="<%= uicomponent.event("ViewDocument")%>">$viewFileLabel</a> |
        <a class="ColorLink" href="<%= uicomponent.event("GotoFolder")%>">$gotoFolderLabel</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 = LinkProvider.getUserProfileUri(id.getRemoteId());
              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>
    <%}%>
   <div id="CommentBlockBound${activity.id}">
      <% if (uicomponent.commentListToggleable()) {
      def showAllCommentsAction = uicomponent.event("SetCommentListStatus", "all");
      def hideAllCommentsAction = uicomponent.event("SetCommentListStatus", "none");
      %>
      <% if (commentListStatus.getStatus().equals("none")) {%>
      <div class="CommentListInfo HideBottomLine">
      <% } else {%>
      <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>
  <% } %>

  <%
  def commenterFullName, commenterProfileUri, commentMessage, commentPostedTime, commenterAvatarImgSrc;
  def first = true, commentContentClass;
  def commentCounter = 0;
  commentList.each({
    commentCounter++;
    if (first & !uicomponent.commentListToggleable()) {
      commentContentClass = "CommentContent";
      first = false;
    } else {
      commentContentClass = "";
    }
    def id = Utils.getIdentityManager().getIdentity(it.userId);
    commenterFullName = id.profile.fullName;
    commenterProfileUri = LinkProvider.getUserProfileUri(id.getRemoteId());
    commentMessage = it.title;
    commentPostedTime = uicomponent.getPostedTimeString(_ctx, it.postedTime);
    commenterAvatarImgSrc = id.profile.avatarUrl;
    if (!commenterAvatarImgSrc) commenterAvatarImgSrc = LinkProvider.PROFILE_DEFAULT_AVATAR_URL;
  %>
  <% if (commentCounter == commentList.size()) {%>
    <div id="CommentBlock${activity.id}${commentCounter}" class="CommentBox CommentBoxNoneBottomLine ClearFix">
  <% } else { %>
    <div id="CommentBlock${activity.id}${commentCounter}" class="CommentBox ClearFix">
  <% } %>
      <a href="$commenterProfileUri" title="$commenterFullName" class="AvatarCM">
        <img width="38px" height="36px" alt="$commenterFullName" src="$commenterAvatarImgSrc" />
        <span class="BorderTopCM"></span>
        <span class="BorderBottomCM"></span>
      </a>
      <div class="ContentBox">
        <h5>
          <a href="$commenterProfileUri">$commenterFullName</a>
        </h5>
        <p class="ContentBlock">  
            $commentMessage
        </p>
        <div class="DateTime">
         $commentPostedTime
        </div>
      </div>
    <%
      if (uicomponent.isCommentDeletable(it.userId)) {
    %>
      <span id="DeleteCommentButton${activity.id}${commentCounter}" onclick="<%= uicomponent.event("DeleteComment", uicomponent.id, it.id); %>" class="CloseContentBoxNormal"></span>
    <% } %>
    </div>
  <% }) %>

    <%
      def currentCommenterIdentity = Utils.getOwnerIdentity();
      def currentCommenterUri = LinkProvider.getUserProfileUri(currentCommenterIdentity.getRemoteId());
      def currentCommenterAvatar = currentCommenterIdentity.profile.avatarUrl;
      def currentCommenterName = currentCommenterIdentity.profile.fullName;
      if (!currentCommenterAvatar) currentCommenterAvatar= LinkProvider.PROFILE_DEFAULT_AVATAR_URL;
    %>
    <% if (commentList.size() == 0) { %>
      <div class="CommentBox InputArea CommentFormBoxNone ClearFix" id="CommentFormBlock${activity.id}">
    <% } else { %>
      <div class="CommentBox InputArea CommentFormBox ClearFix" id="CommentFormBlock${activity.id}">
    <% } %>
      <a class="AvatarCM" title="$currentCommenterName" href="$currentCommenterUri">
        <img src="$currentCommenterAvatar">
            <span class="BorderTopCM"></span>
        <span class="BorderBottomCM"></span>
      </a>
      <div class="CommentInputBox"><% uicomponent.renderChild(UIFormTextAreaInput.class); %></div>
      <input type="button" onclick="<%= uicomponent.event("PostComment") %>" value="$labelComment" class="CommentButton DisplayNone" id="CommentButton${activity.id}" />
    </div>
   </div>
    </div>

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

