@page "/messages/{MessageId:int}"
@using Website.Client.Pages.User.MessagePage.Components
@using Website.Components.Helpers
@using Website.Components.MarkdownEditor

<Title Value="@string.Join(" - ", $"Message #{MessageId}", "Unturned Store")"></Title>

<Meta name="description" content="Marketplace of products for Unturned" />
<Meta property="og:title" content="@($"Message #{MessageId}")" />


<Meta property="og:image" content="/img/logo.png" />
<Meta property="og:image:width" content="128" />
<Meta property="og:image:height" content="128" />
<Meta property="og:description" content="Marketplace of products for Unturned" />


<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="/messages">Messages</a></li>
        <li class="breadcrumb-item active" aria-current="page">@MessageId</li>
    </ol>
</nav>

@if (Message != null)
{
<div class="mb-3">
    <div class="mb-3">
        <h2><span class="fw-bold">Topic:</span> @Message.Title</h2>
    </div>

    @foreach (var reply in Message.Replies.OrderBy(x => x.CreateDate))
    {
        @if (NewReplyId.HasValue && reply.Id == NewReplyId)
        {
            <NewMessageHighlighter Hidden="IsHighlighterHidden" />
        }
        <div class="mb-3">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex align-items-center">
                        <Username User="User(reply.UserId)" />
                        <div class="ms-auto">
                            @if (UserService.UserId == reply.UserId)
                            {
                                <button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteReplyAsync(reply)">
                                    <i class="bi bi-trash-fill"></i>
                                </button>
                            }
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div>
                        @(new MarkupString(MarkdownHelper.ParseToHtml(reply.Content)))
                    </div>
                    <div class="text-muted">
                        <HoverableDateTime Time="reply.CreateDate" />
                    </div>
                </div>
            </div>
        </div>
    }
    @if (!Message.IsClosed)
    {
        <div class="mb-3">
            <div class="card">
                <div class="card-header">
                    <div class="fw-bold">
                        Write a new reply
                    </div>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <MarkdownEditor @bind-Value="Reply.Content" EnableToolbar="false" />
                    </div>

                    <div class="mb-3 d-flex align-content-center">
                        <div>
                            @if (msg != null)
                            {
                                <span class="text-danger">@msg</span>
                            }
                        </div>
                        <div class="ms-auto">
                            <button class="btn btn-secondary me-2" @onclick="ShowCloseMessageAsync"><i class="bi bi-exclamation-circle"></i> Close</button>

                            @if (isLoading)
                            {
                                <LoadingButton />
                            }
                            else
                            {
                                <button class="btn btn-primary" @onclick="AddReplyAsync">Send Reply</button>
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>
    } else
    {
        <div class="mb-3">
            <div class="alert alert-secondary" role="alert">
                This message was closed by @User(Message.ClosingUserId).Name
            </div>
        </div>
    }
</div>
}
else
{
    if (statusCode == System.Net.HttpStatusCode.NoContent)
    {
        <StatementText>Message not found</StatementText>
    }
    else if (statusCode == System.Net.HttpStatusCode.BadRequest)
    {
        <StatementText>You don't have access to this message</StatementText>
    }
    else
    {
        <Progressbar />
    }
}

<ConfirmModal @ref="ConfirmClose" TItem="MMessage" Title="Confirm closing message" BtnClass="btn btn-danger" BtnText="@("<i class=\"bi bi-exclamation-circle\"></i> Close")" ModalId="ConfirmCloseModal" OnConfirm="CloseMessageAsync">
    Are you sure you want to close this message?
</ConfirmModal>
