@page "/editmail_redaction"

@inject ReAct_PME.WebUI.ServicesUI.ApiService ApiService
@using Services;
@using ReAct_PME.WebUI.ServicesUI;
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager Navigation
@inject HttpClient Http
@using Microsoft.AspNetCore.Components.Authorization;

<PageTitle>Génération d'un Email</PageTitle>
<h3>Rédaction d'un mail</h3>

<div class="container mt-3 p-3 rounded shadow-sm">
    <div class="form-group mt-3">
        <label>L'objet du message</label>
        <input class="form-control" @bind="VM.SelectItem.Object" />
    </div>

    <div class="form-group mt-2">
        <label>Destinataires du message (A)</label>
        <input class="form-control" @bind="VM.SelectItem.DestinataireA"
               placeholder="Séparez les adresses par un ;" />
    </div>

    <div class="form-group mt-2">
        <label>Destinataires du message (Cc)</label>
        <input class="form-control" @bind="VM.SelectItem.DestinataireCC"
               placeholder="Séparez les adresses par un ;" />
    </div>

    <div class="form-group mt-2">
        <label>Type de message</label>
        <select class="form-select" @bind="VM.SelectItem.Type">
            @foreach (var type in VM.ListeTypes)
            {
                <option value="@type">@type</option>
            }
        </select>
    </div>

    <div class="form-group mt-2">
        <label>Ton</label>
        <input class="form-control" list="Tons" @bind="VM.SelectItem.Ton" />
        <datalist id="Tons">
            @foreach (var t in VM.ListeTons)
            {
                <option value="@t" />
            }
        </datalist>
    </div>

    <div class="form-group mt-2">
        <label>Posture</label>
        <input class="form-control" list="Postures" @bind="VM.SelectItem.Posture" />
        <datalist id="Postures">
            @foreach (var p in VM.ListePostures)
            {
                <option value="@p" />
            }
        </datalist>
    </div>

    <div class="form-group mt-2">
        <label>Couleur</label>
        <input class="form-control" list="Couleurs" @bind="VM.SelectItem.Couleur" />
        <datalist id="Couleurs">
            @foreach (var c in VM.ListeCouleurs)
            {
                <option value="@c" />
            }
        </datalist>
    </div>

    <div class="form-group mt-2">
        <label>Niveau de détail</label>
        <input class="form-control" list="NiveauxDetail" @bind="VM.SelectItem.NiveauDetail" />
        <datalist id="NiveauxDetail">
            @foreach (var n in VM.ListeNiveauxDetail)
            {
                <option value="@n" />
            }
        </datalist>
    </div>

    <div class="form-check mt-3">
        <input class="form-check-input" type="checkbox" @bind="VM.SelectItem.VOUS_TU" id="vouvoiement" />
        <label class="form-check-label" for="vouvoiement">
            Vouvoiement nécessaire ?
        </label>
    </div>

    <div class="form-group mt-3">
        <label>Votre requête à l'assistant</label>
        <textarea class="form-control" rows="4" @bind="VM.SelectItem.Query"></textarea>
    </div>

    <div class="email-footer mt-3 d-flex justify-content-between">
        <button class="btn btn-outline-light" @onclick="OnGenerateClick" disabled="@IsGenerating">Générer</button>
    </div>

    <div class="form-group mt-3">
        <label>Message généré</label>
        <textarea class="form-control" rows="8" style="background-color:#faebd7;"
                  @bind="VM.SelectItem.Body"></textarea>
    </div>

    @if (!string.IsNullOrEmpty(StatusMessage))
    {
        <div class="alert mt-3 @(IsSuccess ? "alert-success" : "alert-danger")">
            @StatusMessage
        </div>
    }

    <div class="email-footer mt-3 d-flex justify-content-between">
        <button class="btn btn-outline-light" @onclick="OnSendClick" disabled="@(!CanSend)">Envoyer</button>
        <button class="btn btn-outline-light" @onclick="OnCloseClick">Fermer</button>
    </div>
</div>

@code {
    private EditMail_Redaction_VM? VM;
    private string? StatusMessage;
    private bool IsSuccess;
    private bool IsGenerating = false;
    private bool CanSend = false;

    protected override void OnInitialized()
    {
        VM = new(ApiService);
    }

    private async Task OnGenerateClick()
    {
        IsGenerating = true;
        CanSend = false;
        StatusMessage = "Génération en cours...";
        StateHasChanged();

        var (ok, msg) = await VM.Generer();
        IsGenerating = false;
        CanSend = ok;
        IsSuccess = ok;
        StatusMessage = ok ? "Email généré avec succès." : $"Erreur : {msg}";
    }

    private async Task OnSendClick()
    {
        StatusMessage = "Envoi en cours...";
        var (ok, msg) = await VM.Envoyer();
        IsSuccess = ok;
        StatusMessage = ok ? "Email envoyé avec succès." : $"Erreur : {msg}";
    }

    private void OnCloseClick()
    {
        Navigation.NavigateTo($"/");
    }
}
