@inherits Umbraco.Web.Mvc.UmbracoViewPage<OurUmbraco.Forum.Models.ReadOnlyComment>
@using OurUmbraco.Forum.Extensions
@using OurUmbraco.Forum.Services
@using OurUmbraco.Powers.Library

@{
    var currentMember = Members.GetCurrentMember();
    var answer = (int)ViewData["answer"];
    var roles = new List<string>();

    var commentAuthor = Members.GetById(Model.MemberId);
    // author could have been deleted (spammer) so make sure to check if they exist
    if (commentAuthor != null)
    {
        roles = commentAuthor.GetRoles();
    }
    var topicService = new TopicService(ApplicationContext.DatabaseContext);
    var topic = topicService.CurrentTopic(this.Context, ApplicationContext.ApplicationCache.RequestCache);
    var forum = Umbraco.TypedContent(topic.ParentId);
    var forumReadOnly = forum.NewTopicsAllowed() == false;

}
@* If author exists and comment is either not spam or the viewer is a the author or an admin admin *@
@if (commentAuthor != null && (Model.IsSpam == false || (currentMember != null && currentMember.Id == commentAuthor.Id) || Members.IsAdmin()))
{
    <li class="comment  @(Model.ParentCommentId > 0 ? "level-2" : null) @(Model.Id == answer ? "solution" : null)" data-parent="@Model.ParentCommentId" id="comment-@Model.Id">
        <div class="meta">
            <div class="profile">
                <a href="/member/@commentAuthor.Id">@commentAuthor.Name</a> <span>@if (commentAuthor.ForumPosts() == 0 || commentAuthor.ForumPosts() > 1){@commentAuthor.ForumPosts() <text>posts</text>}else{@commentAuthor.ForumPosts() <text>post</text>}</span> <span>@commentAuthor.Karma() karma points</span>
                

                @if (roles.Any())
                {
                    <span class="roles">
                        @foreach (var role in roles)
                        {
                            <span class="@role">@role</span>
                        }
                    </span>
                }

            </div>
            <div class="time" title="@(string.Format("{0:ddd, dd MMM yyyy} {0:HH:mm:ss} UTC+{1}", Model.Created, TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)))">
                @Model.Created.ConvertToRelativeTime()
            </div>
        </div>

        <div class="comment-inner">
            <a href="/member/@commentAuthor.Id" class="photo">@Html.Raw(OurUmbraco.Our.Utils.GetMemberAvatar(commentAuthor, 75))</a>

            <div class="highfive highfive-comment">
                @{
                    var html = "High fives from:<br />";
                    foreach (var vote in Model.Votes)
                    {
                        if (Model.Id == answer && vote.MemberId == Model.TopicAuthorId)
                        {
                            html = string.Format("{0}<a href=\"/member/{1}\">{2}</a> (marked as the answer)<br />", html, vote.MemberId, vote.MemberName);
                        }
                        else
                        {
                            html = string.Format("{0}<a href=\"/member/{1}\">{2}</a><br />", html, vote.MemberId, vote.MemberName);
                        }
                    }

                    @*html = string.Format("{0}<a href=\"/documentation/our/karma\" class=\"explain\">what is this?</a><br />", html);*@
                }
                <div class="highfive-count votes-tooltip" title="@(Model.Votes.Any() ? html : null)">
                    @Model.Score
                </div>
                @if (Members.IsLoggedIn() && !Utils.HasVoted(currentMember.Id, Model.Id, "powersComment"))
                {
                    if (currentMember.Id != Model.MemberId)
                    {
                        <a href="#" data-id="@Model.Id"><img src="/assets/images/highfive.svg" alt="high five" />High Five</a>

                    }
                }
            </div>

            <div class="body markdown-syntax">
                @if (Model.IsSpam)
                {
                    <div class="alert">
                        @if (Members.IsAdmin())
                        {
                            <span>You can see this comment because you're a moderator of the forum.</span><br/><br/>
                        }
                        Sorry if we're causing you any inconvenience but this comment has been automatically marked as spam. A moderator has been notified and will evaluate the validity of your comment. <br/>
                        Only moderators and the comment author starter can see this comment. When this comment has been marked as clean, this comment will be shown as normal.<br/>
                    </div>
                }

                @Model.Body.Sanitize()
            </div>
        </div>
        <div class="actions">
            @if (Members.IsLoggedIn())
            {
                <a href="#" class="flag-comment" data-id="@Model.Id" data-member="@currentMember.Id">
                    <i class="icon-Flag"></i><span>Flag as spam</span>
                </a>

                if (answer == 0 && (Members.IsAdmin() || currentMember.Id == Model.TopicAuthorId))
                {
                    <a href="#" class="solved" data-id="@Model.Id">
                        <i class="icon-Check"></i><span>Mark as solution</span>
                    </a>
                }

                if (Model.ParentCommentId <= 0 && forumReadOnly == false)
                {
                    <a href="#" data-author="@commentAuthor.Name" data-topic="@Model.TopicId" data-parent="@Model.Id" data-controller="comment" class="forum-reply reply">
                        <i class="icon-Reply-arrow"></i><span>Reply</span>
                    </a>
                }

                if (Model.ParentCommentId > 0 && forumReadOnly == false)
                {
                    <a href="#" data-author="@commentAuthor.Name" data-topic="@Model.TopicId" data-parent="@Model.ParentCommentId" data-controller="comment" class="forum-reply reply">
                        <i class="icon-Reply-arrow"></i><span>Reply</span>
                    </a>
                }
                
                if (Members.IsAdmin() || currentMember.Id == Model.MemberId)
                {
                    <a href="#" class="edit-post" data-id="@Model.Id" data-topic="@Model.TopicId" data-controller="comment">
                        <i class="icon-Edit"></i><span>Edit</span>
                    </a>

                    <a href="#" class="delete-reply" data-id="@Model.Id">
                        <i class="icon-Delete-key"></i><span>Delete</span>
                    </a>

                    if (Members.IsAdmin())
                    {
                        if (Model.IsSpam)
                        {
                            <a href="#" class="mark-as-ham" data-id="@Model.Id" data-controller="comment">
                                <i class="icon-Alert-alt"></i><span>Mark as ham</span>
                            </a>
                        }
                        else
                        {
                            <a href="#" class="mark-as-spam" data-id="@Model.Id" data-controller="comment">
                                <i class="icon-Alert-alt"></i><span>Mark as spam</span>
                            </a>
                        }
                    }
                }
            }

            <a href="#" id="copy-link" class="copy-link" data-id="#comment-@Model.Id">
                <i class="icon-Link"></i><span>Copy Link</span>
            </a>
        </div>
    </li>
}
                    

<link rel="stylesheet" type="text/css" href="/css/tooltipster.css" />
<script type="text/javascript" src="/scripts/jquery.tooltipster.min.js"></script>
<script type="text/javascript">
    $('.highfive-count').tooltipster({
        contentAsHTML: true,
        interactive: true
    });
</script>