@using Services;
@inject IToastService ToastService
@using Blazored.Toast.Configuration;
@using Blazored.Toast.Services;
@inject NavigationManager Navigation
@inject ReAct_PME.WebUI.ServicesUI.ApiService ApiService

<div class="email-card">
    <div class="email-header">

        <!-- Ligne 1 : Nom + Catégorie -->
        <div class="d-flex justify-content-between align-items-center">
            <div class="fw-bold">@Email.FromName</div>
            <span class="badge bg-primary">@Email.Categorie</span>
        </div>

        <!-- Ligne 2 : Adresse mail + Priorités -->
        <div class="d-flex justify-content-between align-items-center">
            <div class="text-muted small">@Email.From</div>
            <div class="priorite-badge">
                @RenderPriorite()
            </div>
        </div>

        <!-- Ligne 3 : Date -->
        <div class="d-flex justify-content-between align-items-center">
            <div class="text-muted small">@Email.Date</div>
        </div>

        <!-- Ligne 4 : Pictogrammes + Analyse -->
        <div class="d-flex justify-content-between align-items-center">
            <div class="d-flex align-items-center gap-2">
                <!-- Pictogramme destinataire -->
                <div class="fw-bold" title="@Email.IsDestinatairePrincipal_STR_TXT">
                    @Email.IsDestinatairePrincipal_STR
                </div>

                <!-- Pictogramme présence suspecte -->
                @if (Email.PresenceSuspecte)
                {
                    <div class="fw-bold text-danger" title="Éléments suspects contenus dans l'email ou ses PJ">
                        @Email.PresenceSuspecte_STR
                    </div>
                }

                <!-- Pictogramme présence PJ -->
                @if (Email.PJ_NBR > 0)
                {
                    <div class="fw-bold text-danger" title="@($"{Email.PJ_NBR} pièce(s) jointe(s)")">
                        @Email.PJ_NBR_STR
                    </div>
                }
            </div>

            <!-- Analyse -->
            <div class="rounded">
                <span class="badge bg-light" style="color:@GetAnalyseColor();">
                    Analyse : @Email.Analyse
                </span>
            </div>
        </div>
    </div>

    <!-- Ligne 5 : Objet  -->
    <div class="email-body mt-3 d-flex justify-content-between align-items-center">
        <div class="fw-bold">@Email.Subject</div>
        <span class="text-muted small"></span>
    </div>

    <!-- Footer -->
    <div class="email-footer mt-3 d-flex justify-content-between">
        <button class="btn btn-outline-light" @onclick="() => EditItem(Email.UidString!)" title="Voir le mail">Voir le mail et son analyse</button>
        <button class="btn btn-outline-light" @onclick="() => MarquerLuItem(Email.UidString!)" title="Marquer comme lu">Marquer comme lu</button>
    </div>
</div>



@code {
    @inject IJSRuntime JS
    [Parameter] public EmailMessage Email { get; set; } = new();

    protected override void OnInitialized()
    {

    }

    private void EditItem(string id)
    {
        //ToastService.ShowWarning($"EditItem : {id}.", settings => { settings.Timeout = 2000; settings.IconType = IconType.None; settings.Position = ToastPosition.BottomCenter; });

        Navigation.NavigateTo($"/mail_edit/{id}");
    }

    private async Task MarquerLuItem(string id)
    {
        var b = await ApiService!.SauvegardeItem<Services.EmailMessage>($"api/ListeMails/marquerlu", Email);
        if (b)
        {
            Navigation.NavigateTo($"/listemails_card", forceLoad: true);
            ToastService.ShowWarning($"MarquerLuItem : {id}.", settings => { settings.Timeout = 2000; settings.IconType = IconType.None; settings.Position = ToastPosition.BottomCenter; });
        }
        //Navigation.NavigateTo($"/logs_edit/{id}");
    }

    private string GetAnalyseColor()
    {
        return Email.Analyse switch
        {
            "Positif" => "green",
            "Négatif" => "red",
            "Neutre" => "orange",
            _ => "gray"
        };
    }

    private RenderFragment RenderPriorite() => builder =>
    {
        if(Email.IsImportant && Email.IsUrgent)
        {
            builder.AddContent(2, (MarkupString)"<span class='badge bg-danger me-1'>Important</span><span class='badge bg-warning text-dark'>Urgent</span>");
        }
        else if (Email.IsImportant && !Email.IsUrgent)
        {
            builder.AddContent(1, (MarkupString)"<span class='badge bg-danger'>Important</span>");
        }
        else if (!Email.IsImportant && Email.IsUrgent)
        {
            builder.AddContent(0, (MarkupString)"<span class='badge bg-warning text-dark'>Urgent</span>");
        }
    };
}
