@page
@using Models;
@using System.Globalization
@using System.Security.Claims;
@model WebApp.Pages.AnnouncementModel
@{
    Announcement announcement = (Announcement)ViewData["announcement"];
    ViewData["Title"] = $"{announcement.Title}";
}
<h1 @if (announcement.IsImportant)
{
    @: style="color: red;"
}>@announcement.Title
</h1>
<p>
    Published @announcement.PublishDate.ToString("g") by @announcement.Author.Name
    @Html.Raw((announcement.Author.Role == UserRole.ADMIN || announcement.Author.Role == UserRole.MANAGER)
        ? $"<b>({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(announcement.Author.Role.ToString().ToLower())})</b>"
        : "")
    @(announcement.IsSticky ? "Pinned" : "")
</p>
<p>
    @if (User.FindFirst(ClaimTypes.Role).Value == "ADMIN" || User.Identity.Name == announcement.Author.Name)
    {
        <a href="./EditAnnouncement?id=@announcement.ID" class="btn btn-outline-warning">Edit</a>
        <a href="./DeleteAnnouncement?id=@announcement.ID" class="btn btn-outline-danger">Delete</a>
    }
</p>
<hr/>
<p>@Html.Raw(announcement.Description.Replace(Environment.NewLine, "<br />"))</p>
<br/>
<h3>Comments</h3>
<a href="./AddComment?t=announcement&id=@announcement.ID" class="btn btn-primary">Add comment</a>
@if (announcement.Comments.Count() == 0)
{
    <p>No comments found</p>
}
else
{
    foreach (Comment comment in announcement.Comments)
    {
        DisplayComment(comment, 0);
    }
}

@{
    void DisplayComment(Comment comment, int level)
    {
        <div class="d-flex flex-row">
        @for (int i = 0; i < level; i++)
        {
            <a class="me-3" href="#"></a>
        }
        <div class="card flex-fill">
            <div class="card-body">
                    <h5 class="card-title">@comment.Author.Name @Html.Raw((comment.Author.Role == UserRole.ADMIN || comment.Author.Role == UserRole.MANAGER) ? $"<b>({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(comment.Author.Role.ToString().ToLower())})</b>" : "")</h5>
                    <h6>@comment.PublishDate.ToString("g")</h6>
                    <p class="card-text">@Html.Raw(comment.Description.Replace(Environment.NewLine, "<br />"))</p>
                    <a class="btn btn-outline-primary" href="./AddComment?t=comment&id=@comment.ID">Reply</a>
                </div>
                @if (comment.Responses.Count != 0)
                {
                    foreach (var response in comment.Responses)
                    {
                        DisplayComment(response, level + 1);
                    }
                }
            </div>
        </div>
    }
}