@using Iceshrimp.Assets.PhosphorIcons
@using Iceshrimp.Frontend.Core.Miscellaneous
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Localization
@using Iceshrimp.Shared.Schemas.Web
@using Microsoft.Extensions.Localization
@inject GlobalComponentSvc             Global;
@inject IStringLocalizer<Localization> Loc;

<div class="wrapper" @onclick="Open" @onclick:stopPropagation="true">
    @if (Attachment.ContentType.StartsWith("image") || Constants.CommonImageExtensions.Any(e => Attachment.FileName.EndsWith(e)))
    {
        <img class="attachment @(BlurImage ? "blur-image" : "")" src="@Attachment.Url" alt="@Attachment.AltText"
             fetchpriority="low"/>
    }
    else if (BlurImage)
    {
        <div class="attachment contents blur-image">
            <Icon Name="Icons.Warning" Size="3em"/>
            <span>@Loc["Sensitive media"]</span>
            <span>@Loc["Click to reveal"]</span>
        </div>
    }
    //  else if (Attachment.ContentType == "audio/x-mod")
    //  {
    //      TODO: Module player
    //  }
    else if (Attachment.ContentType.StartsWith("audio") || Constants.CommonAudioExtensions.Any(e => Attachment.FileName.EndsWith(e)))
    {
        <audio controls class="attachment" src="@Attachment.Url"></audio>
    }
    else if (Attachment.ContentType.StartsWith("video") || Constants.CommonVideoExtensions.Any(e => Attachment.FileName.EndsWith(e)))
    {
        <video controls class="attachment">
            <source src="@Attachment.Url"/>
        </video>
    }
    else
    {
        <div class="attachment contents">
            <Icon Name="Icons.FileArrowDown" Size="3em"/>
            <a href="@Attachment.Url" target="_blank">
                @Attachment.FileName
            </a>
        </div>
    }

    @if (!string.IsNullOrWhiteSpace(Attachment.AltText))
    {
        <button @onclick="DisplayAlt" @onclick:stopPropagation="true" class="alt-btn" title="@Attachment.AltText">
            <Icon Name="Icons.ClosedCaptioning"/>
        </button>
    }
    else
    {
        <button @onclick="DisplayAlt" @onclick:stopPropagation="true" class="alt-btn" title="@Loc["No alt text"]">
            <Icon Name="Icons.Warning"/>
        </button>
    }
</div>

@code {
    [Parameter] [EditorRequired] public required NoteAttachment        Attachment { get; set; }
    [Parameter] [EditorRequired] public required EventCallback<NoteAttachment> OnOpen     { get; set; }

    private bool BlurImage { get; set; }

    private void Open()
    {
        if (BlurImage)
        {
            BlurImage = false;
            StateHasChanged();
        }
        else if (Attachment.ContentType.StartsWith("image"))
        {
            OnOpen.InvokeAsync(Attachment);
        }
    }

    protected override void OnParametersSet()
    {
        BlurImage = Attachment.IsSensitive;
    }

    private async Task DisplayAlt()
    {
        if (string.IsNullOrWhiteSpace(Attachment.AltText))
            await Global.NoticeDialog?.Display(Loc["No alt text"], NoticeDialog.NoticeType.Error)!;
        else
            await Global.NoticeDialog?.Display(Attachment.AltText)!;
    }
}