@page "/editor"
@page "/editor/{action}"
@page "/editor/{action}/{projectId:int}"
@inject IOllamaService OllamaService
@inject IProjectService ProjectService
@inject NavigationManager Navigation

<PageTitle>Editor - Author Buddy</PageTitle>

<div class="editor-container">
    <textarea @bind="editorContent" placeholder="Schreibe hier deinen Text..."></textarea>
    <div class="editor-toolbar">
        <span style="flex: 1; font-size: 12px; color: var(--border-color);">
            @statusText
        </span>
        @if (isChecking)
        {
            <progress value="@checkProgress" max="100"></progress>
        }
        <button @onclick="CheckStyle" disabled="@isChecking">
            Stil prüfen
        </button>
        <button @onclick="SpellCheck" disabled="@isChecking">
            Rechtschreibung prüfen
        </button>
        <button @onclick="RagSearch">
            Fakten suchen (RAG)
        </button>
    </div>
</div>

@if (showSpellDiff && !string.IsNullOrEmpty(correctedText))
{
    <div class="diff-panel">
        <h4>Rechtschreibprüfung - Änderungen:</h4>
        <DiffView Original="@originalText" Corrected="@correctedText" />
    </div>
}
else if (!string.IsNullOrEmpty(analysisResult))
{
    <div class="result-panel">
        <h4>Ergebnis:</h4>
        <div class="result-text">@analysisResult</div>
    </div>
}

@code {
    [Parameter]
    public string? Action { get; set; }

    [Parameter]
    public int? ProjectId { get; set; }

    private string editorContent = string.Empty;
    private string statusText = "Bereit";
    private bool isChecking;
    private double checkProgress;
    private string analysisResult = string.Empty;
    private string originalText = string.Empty;
    private string correctedText = string.Empty;
    private bool showSpellDiff;

    protected override async Task OnInitializedAsync()
    {
        if (Action == "newproject")
        {
            statusText = "Bitte nutze die Einstellungen zum Erstellen eines neuen Projekts.";
        }
    }

    private async Task CheckStyle()
    {
        if (string.IsNullOrWhiteSpace(editorContent)) return;

        isChecking = true;
        statusText = "Ollama analysiert Stil...";
        checkProgress = 50;
        showSpellDiff = false;

        try
        {
            //analysisResult = await OllamaService.StreamStyleCheckAsync(editorContent);
        }
        catch (Exception ex)
        {
            analysisResult = $"Fehler: {ex.Message}";
        }
        finally
        {
            statusText = "Bereit";
            isChecking = false;
            checkProgress = 0;
        }
    }

    private async Task SpellCheck()
    {
        if (string.IsNullOrWhiteSpace(editorContent)) return;

        isChecking = true;
        statusText = "Ollama prüft Rechtschreibung...";
        checkProgress = 50;
        showSpellDiff = false;

        try
        {
            originalText = editorContent;
            correctedText = await OllamaService.QuickSpellCheckAsync(editorContent);
            showSpellDiff = true;
        }
        catch (Exception ex)
        {
            analysisResult = $"Fehler: {ex.Message}";
        }
        finally
        {
            statusText = "Bereit";
            isChecking = false;
            checkProgress = 0;
        }
    }

    private async Task RagSearch()
    {
        if (string.IsNullOrWhiteSpace(editorContent)) return;

        isChecking = true;
        statusText = "Suche verwandte Fakten...";
        showSpellDiff = false;

        await Task.Delay(500);
        analysisResult = "RAG-Suche: Stelle sicher, dass ein Projekt geladen und indexiert wurde.";
        statusText = "Bereit";
        isChecking = false;
    }
}
