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

@{
    var currentMember = Members.GetCurrentMember();
    var topicAuthor = Members.GetById(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.Id) || Members.IsAdmin()))
    {
        var forum = Umbraco.TypedContent(Model.ParentId);
        var roles = topicAuthor.GetRoles();


        <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.Id">@topicAuthor.Name</a> <span>@if (topicAuthor.ForumPosts() == 0 || topicAuthor.ForumPosts() > 1){@topicAuthor.ForumPosts() <text>posts</text>}else{@topicAuthor.ForumPosts() <text>post</text>}</span> <span>@topicAuthor.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/@topicAuthor.Id" class="photo">@Html.Raw(OurUmbraco.Our.Utils.GetMemberAvatar(topicAuthor, 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 (Members.IsLoggedIn() && !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="@forum.Url">@forum.Name</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 (Members.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 topic has been automatically marked as spam. A moderator has been notified and will evaluate the validity of your topic. <br/>
                            Only moderators and the topic author starter can see this topic. When this topic has been marked as clean, this topic will be shown as normal.<br/>
                        </div>
                    }

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

            <div class="actions">
                @if (Members.IsLoggedIn())
                {
                    <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 (forum.NewTopicsAllowed())
                    {
                        <a href="#" data-author="@topicAuthor.Name" data-topic="@Model.Id" data-controller="comment" class="reply forum-reply">
                            <i class="icon-Reply-arrow"></i><span>Reply</span>
                        </a>
                    }

                    if (Members.IsAdmin() || Members.GetCurrentMember().Id == topicAuthor.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 (Members.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 -->
    }
}