@using TightWiki.Models
@model TightWiki.Models.ViewModels.Page.PageDisplayViewModel
@{
    Layout = "/Views/Shared/_Layout.cshtml";
    var sessionState = ViewData["SessionState"] as TightWiki.SessionState ?? throw new Exception("Wiki State Context cannot be null.");
}

@if (Model.Revision < Model.MostCurrentRevision)
{
    <div class="card border-info mb-3">
        <div class="card-header bg-info">
        <strong>Viewing a historical version</strong>
        </div>
        <div class="card-body">
            <p class="card-text">
                You are viewing revision @Model.Revision of "@Model.Name" modified by @Model.ModifiedByUserName on @Model.ModifiedDate.<br />

                <a class="btn btn-success btn-thin" href="@GlobalConfiguration.BasePath/@Model.Navigation">View latest revision</a>
                @if(sessionState.CanModerate)
                {
                    <a class="btn btn-warning btn-thin" href="@GlobalConfiguration.BasePath/@Model.Navigation/revert/@Model.Revision">Revert to revision @Model.Revision</a>
                }
            </p>
        </div>
    </div>
}

@Html.Raw(Model.Body)

@if (TightWiki.Models.GlobalConfiguration.ShowLastModifiedOnPageFooter && string.IsNullOrWhiteSpace(@Model.ModifiedByUserName) == false && Model.HideFooterLastModified != true)
{
    <br />
    if (TightWiki.Models.GlobalConfiguration.EnablePublicProfiles)
    {
        <small><cite title="Modified By">Last modified by <a href="@GlobalConfiguration.BasePath/Profile/@Model.ModifiedByUserName/Public">@Model.ModifiedByUserName</a> @@ @Model.ModifiedDate</cite></small>
    }
    else
    {
        <small><cite title="Modified By">Last modified by @Model.ModifiedByUserName @@ @Model.ModifiedDate</cite></small>
    }
    <br />
}

@if (Model.HideFooterComments == false)
{
    @if (TightWiki.Models.GlobalConfiguration.EnablePageComments && TightWiki.Models.GlobalConfiguration.ShowCommentsOnPageFooter)
    {
        <hr class="mt-5 mb-5">
        <h2>Comments</h2>
    }
    @if (TightWiki.Models.GlobalConfiguration.EnablePageComments && TightWiki.Models.GlobalConfiguration.ShowCommentsOnPageFooter)
    {
        if (@sessionState.IsAuthenticated == true)
        {
            <form method="post" action="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Comments">
                <div class="container">
                    <div class="d-flex justify-content-end mb-4">
                        <div class="flex-grow-1 me-2">
                            <input type="text" name="Comment" id="Comment" class="form-control" placeholder="Type comment..." />
                        </div>
                        <button type="submit" value="Post" class="btn btn-primary">Post</button>
                    </div>
                </div>
            </form>
        }
        else
        {
            @:<a href="@GlobalConfiguration.BasePath/Identity/Account/Login?returnUrl=@Context.Request.Path.Value">Login to leave a comment</a>.
            <br />
        }
    }
    @if (TightWiki.Models.GlobalConfiguration.EnablePageComments && TightWiki.Models.GlobalConfiguration.ShowCommentsOnPageFooter && Model.Comments != null && Model.Comments.Count > 0)
    {
        <div class="container mt-4">
            @foreach (var h in Model.Comments)
            {
                <div class="border rounded mb-2 p-2">
                    <p class="mb-1">@Html.Raw(h.Body)</p>
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="d-flex align-items-center small text-muted mb-0">
                            <img src="@GlobalConfiguration.BasePath/Profile/@h.UserNavigation/Avatar?Exact=16" class="rounded-circle me-2" alt="Avatar" />
                            <a href="@GlobalConfiguration.BasePath/Profile/@h.UserNavigation/Public" class="small text-decoration-none mb-0">@Html.DisplayTextFor(x => h.UserName)</a>
                            &nbsp;&nbsp;<p class="small text-decoration-none text-muted mb-0">@Html.DisplayTextFor(x => h.CreatedDate)</p>
                        </div>
                        @if (sessionState.CanModerate == true || h.UserId == sessionState.Profile?.UserId)
                        {
                            <a href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Comments?Delete=@h.Id" class="small text-danger text-decoration-none" onclick="return confirm('Are you sure you want to delete this comment?')">Delete</a>
                        }
                    </div>
                </div>
            }
        </div>
    }
    @if (TightWiki.Models.GlobalConfiguration.EnablePageComments && TightWiki.Models.GlobalConfiguration.ShowCommentsOnPageFooter)
    {
        <a href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Comments">View all comments</a>
    }
}
