@inherits UmbracoViewPage<OurUmbraco.Forum.Models.Topic>
@using OurUmbraco.Forum.Extensions
@using OurUmbraco.Powers.Library

@{
    var currentMember = Model.MemberData != null && Model.MemberData.Member != null ? Model.MemberData.Member : null;
    var topicAuthor = Model.TopicMembers.FirstOrDefault(x => x.Member.Id == Model.MemberId);

    @* If author exists and question is either not spam or the viewer is a the author or an admin admin *@
    if (topicAuthor != null && (Model.IsSpam == false || (currentMember != null && currentMember.Id == topicAuthor.Member.Id) || Model.MemberData != null && Model.MemberData.IsAdmin))
    {
        <li class="comment question" id="comment-@Model.Id" data-deeplink="@Model.Title" data-version="@Model.Version" data-cat="@Model.ParentId">
            <!-- Start of question -->
            <div class="meta">
                <div class="profile">
                    <a href="/member/@topicAuthor.Member.Id">@topicAuthor.Member.Name</a> <span>@if (topicAuthor.Member.ForumPosts() == 0 || topicAuthor.Member.ForumPosts() > 1){@topicAuthor.Member.ForumPosts() <text>posts</text>}else{@topicAuthor.Member.ForumPosts() <text>post</text>}</span> <span>@topicAuthor.Member.Karma() karma points</span>

                    @if (topicAuthor.Roles.Any())
                    {
                        <span class="roles">
                            @foreach (var role in topicAuthor.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/@topicAuthor.Member.Id" class="photo">@Html.Raw(OurUmbraco.Our.Utils.GetMemberAvatar(topicAuthor.Member, 75))</a>
                <div class="highfive highfive-question">
                    @{
                        var html = "High fives from:<br />";
                        foreach (var vote in Model.Votes)
                        {
                            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 (Model.MemberData != null && !Utils.HasVoted(currentMember.Id, Model.Id, "powersTopic"))
                    {
                        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-meta">
                    <div class="topic">
                        <h2>@Model.Title</h2>
                    </div>
                    <div class="categories">
                        <div class="category core">
                            <a href="@Model.ForumUrl">@Model.ForumName</a>
                        </div>

                        @if (Model.Version > 0)
                        {
                            <div class="version">
                                <a href="#">Umbraco @Model.Version</a>
                            </div>
                        }
                    </div>
                </div>

                <div class="body markdown-syntax">

                    @if (Model.IsSpam)
                    {
                        <div class="alert">
                            @if (Model.MemberData != null && Model.MemberData.IsAdmin)
                            {
                                <span>You can see this topic because you're a moderator of the forum.</span><br/><br/>
                            }
                            Sorry if we're causing you any inconvenience but this post has been automatically marked for manual approval. A moderator has been notified and will evaluate to make sure you're not trying to post spam. <br />
                            Only moderators and the post author can see this post. When this post has been approved this post will be shown as normal. After that, your future posts will not need manual approval.<br /><br />
                            <strong>Note:</strong> We will send you an email when your account has been approved.<br /><br />
                            <strong>Why are we doing this?</strong> We're dealing with a spam problem that cannot be fought with automated systems. Therefore, we approve each account that has a low number of posts manually. Again, sorry for the inconvenience, we much appreciate your contribution and your patience while we approve your account.
                        </div>
                    }

                    @Model.Body.Sanitize()
                </div>
            </div>

            <div class="actions">
                @if (Model.MemberData != null)
                {
                    <a href="#" class="flag-thread" data-id="@Model.Id" data-member="@currentMember.Id">
                        <i class="icon-Flag"></i><span>Flag as spam</span>
                    </a>

                    if (Model.ForumNewTopicsAllowed)
                    {
                        <a href="#" data-author="@topicAuthor.Member.Name" data-topic="@Model.Id" data-controller="comment" class="reply forum-reply">
                            <i class="icon-Reply-arrow"></i><span>Reply</span>
                        </a>
                    }

                    if (Model.MemberData.IsAdmin || Model.MemberData.Member.Id == topicAuthor.Member.Id)
                    {
                        <a href="#" class="edit-post" data-id="@Model.Id" data-controller="topic">
                            <i class="icon-Edit"></i><span>Edit</span>
                        </a>

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

                    if (Model.MemberData.IsAdmin)
                    {
                        if (Model.IsSpam)
                        {
                            <a href="#" class="mark-as-ham" data-id="@Model.Id" data-controller="topic">
                                <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="topic">
                                <i class="icon-Alert-alt"></i><span>Mark as spam</span>
                            </a>
                        }
                    }
                }

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