@page "/chatroom_llm"

<h3>Chat Room</h3>
@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;
@inject ReAct_PME.WebUI.ServicesUI.AuthService AuthService

<PageTitle>ChatRoom</PageTitle>

<div class="container-fluid">

    <div class="container">
        <div class="form-group row">
            <label for="Question" class="col-sm-2 col-form-label">Question* : </label>
            <div class="col-sm-10">
                <textarea id="Question" @bind="Question" class="form-control" @oninput="AutoResizeQuestion" rows="3" />
            </div>
        </div>
        <div class="form-group row">
            <label for="EnvoiRequete" class="col-sm-2 col-form-label"></label>
            <div class="col-sm-10">
                <button class="btn-action btn-primary" @onclick="EnvoiRequete">
                    <i class="bi bi-arrow-clockwise"></i>Envoyer la question
                </button>
            </div>
        </div>
        <div class="form-group row">
            <label for="Reponse" class="col-sm-2 col-form-label">Reponse : </label>
            <div class="col-sm-10">
                <textarea id="Reponse" @bind="Reponse" class="form-control" @oninput="AutoResizeReponse" rows="5" disabled />
            </div>
        </div>
    </div>
</div>


@code {

    private string Question = "Quelle a été le premier homme à marcher sur la lune ?";
    private string Reponse = string.Empty;
    private Guid IdConversationSelected = Guid.Empty;

    #region Rendre les zones extensibles
    [Inject] IJSRuntime? JS { get; set; }
    private void AutoResizeQuestion(ChangeEventArgs e)
    {
        if (JS != null)
        {
            JS.InvokeVoidAsync("resizeTextarea", "Question");
        }
    }
    private void AutoResizeReponse(ChangeEventArgs e)
    {
        if (JS != null)
        {
            JS.InvokeVoidAsync("resizeTextarea", "Reponse");
        }
    }


    #endregion

    protected override async Task OnInitializedAsync()
    {
        await Task.Delay(1);
    }

    private async Task EnvoiRequete()
    {
        try
        {
            var rep = await ApiService.EnvoiRequete<string>(
                "api/ChatRoom/llm", IdConversationSelected.ToString(), AuthService!.ID!,
                Question
            );
            Reponse = rep;
        }
        catch (Exception ex)
        {
            Reponse = ex.Message;
        }
    }

}
