@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@using Moonlight.App.Services
@using Moonlight.App.Services.Files
@using System.Text.RegularExpressions

@inject ResourceService ResourceService
@inject SmartTranslateService SmartTranslateService

<div class="scroll-y me-n5 pe-5" style="max-height: 50vh; display: flex; flex-direction: column-reverse;">
@foreach (var message in Messages.OrderByDescending(x => x.Id)) // Reverse messages to use auto scrolling
{
    if (message.IsSupportMessage)
    {
        if (ViewAsSupport)
        {
            <div class="d-flex justify-content-end mb-10 ">
                <div class="d-flex flex-column align-items-end">
                    <div class="d-flex align-items-center mb-2">
                        <div class="me-3">
                            <span class="text-muted fs-7 mb-1">@(Formatter.FormatAgoFromDateTime(message.CreatedAt, SmartTranslateService))</span>
                            @if (message.Sender != null)
                            {
                                <span class="fs-5 fw-bold text-gray-900 text-hover-primary ms-1">@(message.Sender!.FirstName) @(message.Sender!.LastName)</span>
                            }
                        </div>
                        <div class="symbol  symbol-35px symbol-circle ">
                            @if (message.Sender != null)
                            {
                                <img alt="Avatar" src="@(ResourceService.Avatar(message.Sender))">
                            }
                        </div>
                    </div>
                    <div class="p-5 rounded bg-light-primary text-dark fw-semibold mw-lg-400px text-end">
                        @{
                            int i = 0;
                            var arr = message.Content.Split("\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                        }
                        @foreach (var line in arr)
                        {
                            @line
                            if (i++ != arr.Length - 1)
                            {
                                <br/>
                            }
                        }

                        @if (!string.IsNullOrEmpty(message.AttachmentUrl))
                        {
                            <div class="mt-3">
                                @if (Regex.IsMatch(message.AttachmentUrl, @"\.(jpg|jpeg|png|gif|bmp)$"))
                                {
                                    <img src="@(ResourceService.BucketItem("tickets", message.AttachmentUrl))" class="img-fluid" alt="Attachment"/>
                                }
                                else
                                {
                                    <a class="btn btn-secondary" href="@(ResourceService.BucketItem("tickets", message.AttachmentUrl))">
                                        <i class="me-2 bx bx-download"></i> @(message.AttachmentUrl)
                                    </a>
                                }
                            </div>
                        }
                    </div>
                </div>
            </div>
        }
        else
        {
            <div class="d-flex justify-content-start mb-10 ">
                <div class="d-flex flex-column align-items-start">
                    <div class="d-flex align-items-center mb-2">
                        <div class="symbol symbol-35px symbol-circle ">
                            @if (message.Sender != null)
                            {
                                <img alt="Avatar" src="@(ResourceService.Avatar(message.Sender))">
                            }
                        </div>
                        <div class="ms-3">
                            <span class="fs-5 fw-bold text-gray-900 text-hover-primary me-1">@(message.Sender!.FirstName) @(message.Sender!.LastName)</span>
                            <span class="text-muted fs-7 mb-1">@(Formatter.FormatAgoFromDateTime(message.CreatedAt, SmartTranslateService))</span>
                        </div>
                    </div>
                    <div class="p-5 rounded bg-light-info text-dark fw-semibold mw-lg-400px text-start">
                        @{
                            int i = 0;
                            var arr = message.Content.Split("\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                        }
                        @foreach (var line in arr)
                        {
                            @line
                            if (i++ != arr.Length - 1)
                            {
                                <br/>
                            }
                        }

                        @if (!string.IsNullOrEmpty(message.AttachmentUrl))
                        {
                            <div class="mt-3">
                                @if (Regex.IsMatch(message.AttachmentUrl, @"\.(jpg|jpeg|png|gif|bmp)$"))
                                {
                                    <img src="@(ResourceService.BucketItem("tickets", message.AttachmentUrl))" class="img-fluid" alt="Attachment"/>
                                }
                                else
                                {
                                    <a class="btn btn-secondary" href="@(ResourceService.BucketItem("tickets", message.AttachmentUrl))">
                                        <i class="me-2 bx bx-download"></i> @(message.AttachmentUrl)
                                    </a>
                                }
                            </div>
                        }
                    </div>
                </div>
            </div>
        }
    }
    else if (message.IsSystemMessage)
    {
        <div class="separator separator-content border-primary my-15">
            <span class="w-250px fw-bold">
                @(message.Content)
            </span>
        </div>
    }
    else
    {
        if (ViewAsSupport)
        {
            <div class="d-flex justify-content-start mb-10 ">
                <div class="d-flex flex-column align-items-start">
                    <div class="d-flex align-items-center mb-2">
                        <div class="symbol  symbol-35px symbol-circle ">
                            @if (message.Sender != null)
                            {
                                <img alt="Avatar" src="@(ResourceService.Avatar(message.Sender))">
                            }
                        </div>
                        <div class="ms-3">
                            <span class="fs-5 fw-bold text-gray-900 text-hover-primary me-1">@(message.Sender!.FirstName) @(message.Sender!.LastName)</span>
                            <span class="text-muted fs-7 mb-1">@(Formatter.FormatAgoFromDateTime(message.CreatedAt, SmartTranslateService))</span>
                        </div>
                    </div>
                    <div class="p-5 rounded bg-light-info text-dark fw-semibold mw-lg-400px text-start">
                        @{
                            int i = 0;
                            var arr = message.Content.Split("\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                        }
                        @foreach (var line in arr)
                        {
                            @line
                            if (i++ != arr.Length - 1)
                            {
                                <br/>
                            }
                        }

                        @if (!string.IsNullOrEmpty(message.AttachmentUrl))
                        {
                            <div class="mt-3">
                                @if (Regex.IsMatch(message.AttachmentUrl, @"\.(jpg|jpeg|png|gif|bmp)$"))
                                {
                                    <img src="@(ResourceService.BucketItem("tickets", message.AttachmentUrl))" class="img-fluid" alt="Attachment"/>
                                }
                                else
                                {
                                    <a class="btn btn-secondary" href="@(ResourceService.BucketItem("tickets", message.AttachmentUrl))">
                                        <i class="me-2 bx bx-download"></i> @(message.AttachmentUrl)
                                    </a>
                                }
                            </div>
                        }
                    </div>
                </div>
            </div>
        }
        else
        {
            <div class="d-flex justify-content-end mb-10 ">
                <div class="d-flex flex-column align-items-end">
                    <div class="d-flex align-items-center mb-2">
                        <div class="me-3">
                            <span class="text-muted fs-7 mb-1">@(Formatter.FormatAgoFromDateTime(message.CreatedAt, SmartTranslateService))</span>
                            <span class="fs-5 fw-bold text-gray-900 text-hover-primary ms-1">@(message.Sender!.FirstName) @(message.Sender!.LastName)</span>
                        </div>
                        <div class="symbol symbol-35px symbol-circle ">
                            @if (message.Sender != null)
                            {
                                <img alt="Avatar" src="@(ResourceService.Avatar(message.Sender))">
                            }
                        </div>
                    </div>
                    <div class="p-5 rounded bg-light-primary text-dark fw-semibold mw-lg-400px text-end">
                        @{
                            int i = 0;
                            var arr = message.Content.Split("\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                        }
                        @foreach (var line in arr)
                        {
                            @line
                            if (i++ != arr.Length - 1)
                            {
                                <br/>
                            }
                        }

                        @if (!string.IsNullOrEmpty(message.AttachmentUrl))
                        {
                            <div class="mt-3">
                                @if (Regex.IsMatch(message.AttachmentUrl, @"\.(jpg|jpeg|png|gif|bmp)$"))
                                {
                                    <img src="@(ResourceService.BucketItem("tickets", message.AttachmentUrl))" class="img-fluid" alt="Attachment"/>
                                }
                                else
                                {
                                    <a class="btn btn-secondary" href="@(ResourceService.BucketItem("tickets", message.AttachmentUrl))">
                                        <i class="me-2 bx bx-download"></i> @(message.AttachmentUrl)
                                    </a>
                                }
                            </div>
                        }
                    </div>
                </div>
            </div>
        }
    }
}
</div>

@code
{
    [Parameter]
    public IEnumerable<TicketMessage> Messages { get; set; }

    [Parameter]
    public bool ViewAsSupport { get; set; }
}