-# account and models will be removed.
 {argv}              = require 'optimist'
 {uri, client}       = require('koding-config-manager').load("main.#{argv.c}")
 
 
-
-
-createCommentNode = (comment)->
-  commentContent = ""
-  if comment.body
-    commentContent =
-    """
-    <li itemtype="http://schema.org/Comment" itemscope itemprop="comment">
-        <span itemprop="commentText">#{comment.body}</span> - at
-        <span itemprop="commentTime">#{comment.createdAt}</span> by
-        <a href="#{uri.address}/#{comment.authorNickname}"><span itemprop="name">#{comment.authorName}</span></a>
-    </li>
-    """
-  return commentContent
-
-createAccountName = (fullName)->
-  return "#{fullName}"
 getAvatarImageUrl = (hash, avatar)->
   imgURL   = "//gravatar.com/avatar/#{hash}?size=90&d=https://koding-cdn.s3.amazonaws.com/images/default.avatar.140.png&r=g"
   if avatar

        """
      tags += content

  shortenedTitle = activityContent.title
  if shortenedTitle?.length > 150
    shortenedTitle = activityContent.title.substring(0, 150) + "..."
  title =
    """
      <a href="#{slugWithDomain}">#{shortenedTitle}</a>
    """

  content  =
    """
      <div class="kdview activity-item status">
          <a class="avatarview author-avatar" href="#{uri.address}/#{nickname}" style="width: 70px; height: 70px; background-size: 70px; background-image: none;">
              #{avatarImage}
          </a>

          <a class="profile" href="#{uri.address}/#{nickname}">
            <span data-paths="profile.firstName profile.lastName" itemscope itemtype="http://schema.org/person" title="#{accountName}">
              #{accountName}
            </span>
          </a>

          <article data-paths="body">
            <p>
              #{body}
            </p>
          </article>

          <footer>
              #{userInteractionMeta}
              <div class="kdview comment-header activity-actions">
                  <span class="kdview logged-in action-container"><a class="action-link" href="/Login?warning=1&type=like">Like</a>
                      <a class="count" href="/Login?warning=1&type=like">
                          <span data-paths="meta.likes">#{likesCount}</span>
                      </a>
                  </span>
                  <span class="logged-in action-container">
                      <a class="action-link" href="/Login?warning=1&type=comment">Comment</a>
                      <a class="count" href="/Login?warning=1&type=comment">
                          <span data-paths="repliesCount" >#{commentsCount}</span>
                      </a>
                  </span>
              </div>
              #{createdAt}
          </footer>
          #{commentsContent}
      </div>
    """
  return content

module.exports = {
  getSingleActivityPage
  getSingleActivityContent
}

createAvatarImage = (hash, avatar)=>
  imgURL = getAvatarImageUrl hash, avatar
  """
  <img width="42" height="42" src="#{imgURL}" style="opacity: 1;" itemprop="image" />
  """

createCreationDate = (createdAt, slug)->
  """
    <a href="#{uri.address}/Activity/Post/#{slug}" style="text-decoration:none">
      <time class="kdview"><span itemprop=\"dateCreated\">#{createdAt}</span></time>
    </a>
  """

createAuthor = (accountName, nickname)->
  return "<a href=\"#{uri.address}/#{nickname}\"><span itemprop=\"name\">#{accountName}</span></a>"

prepareComments = (activityContent)->
  commentsList = ""
  return commentsList  unless activityContent?.replies

  for comment in activityContent.replies
    {replier, message} = comment
    {hash, avatar, nickname, fullName} = replier
    avatarImage = createAvatarImage hash, avatar
    commentsList +=
      """
        <div class="kdview kdlistitemview kdlistitemview-comment">
          <a class="avatarview" href="/#{comment.authorNickname}" style="background-image: none; background-size: 42px;">
            #{avatarImage}
          </a>
          <div class="comment-contents clearfix">
            <a href="#{uri.address}/#{nickname}" class="profile" itemprop="name">#{fullName}</a>
            <div class="comment-body-container">
              <p itemprop="commentText">#{message.body}</p>
            </div>
          </div>
        </div>
      """

  return commentsList

prepareLikes = (activityContent)->
  return ""  unless activityContent?.likers?.length
  likeList = """<div class="kdview like-summary">"""

  likerCount = activityContent.likers.length
  for i in [0...likerCount]
    {nickname, fullName} = activityContent.likers[i]
    likeList += """<a href="#{uri.address}/#{nickname}" class="profile"><span>#{fullName}</span></a>"""
    likeList += addLikeSeperator i, likerCount

  likeList += "<span> liked this.</span></div>"

addLikeSeperator = (index, count) ->
  return ", "    if index < count - 2
  return " and " if index < count - 1
  return ""      if index == count - 1

getSingleActivityContent = (activityContent)->
  slugWithDomain = "#{uri.address}/Activity/Public/#{activityContent.slug}"
  {body, nickname, fullName, hash, avatar, createdAt, commentCount, likeCount} = activityContent
  avatarImage   = createAvatarImage hash, avatar
  createdAt     = createCreationDate createdAt, activityContent.slug
  author        = createAuthor fullName, nickname

  commentsList = prepareComments activityContent

  likeList = prepareLikes activityContent

  tags = ""
  if activityContent?.tags?.length > 0
