@page "/"
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject IJSRuntime JS
@inject NavigationManager Nav

<PageTitle>FilterCair</PageTitle>

<div class="container py-4 text-center">

    <!-- 🔐 Login / Logout Bereich -->
    <AuthorizeView Context="auth">
        <Authorized>
            <div class="mb-4">
                👋 Willkommen, <strong>@auth.User.Identity?.Name</strong>!
                <br />
                <button class="btn btn-outline-danger btn-sm mt-2" @onclick="Logout">
                    <i class="bi bi-box-arrow-right me-1"></i> Abmelden
                </button>
            </div>
        </Authorized>
        <NotAuthorized>
            <div class="mb-4">
                <button class="btn btn-success btn-lg rounded-pill" @onclick="Login">
                    <i class="bi bi-person-lock me-2"></i> Mit Microsoft anmelden
                </button>
            </div>
        </NotAuthorized>
    </AuthorizeView>

    <!-- 🌐 Online/Offline-Status -->
    <div class="mb-4">
        <span class="badge rounded-pill px-3 py-2 fs-6"
              style="background-color:@(isOnline ? "#198754" : "#dc3545")">
            @(isOnline ? "🟢 Online" : "🔴 Offline")
        </span>
    </div>

    <!-- 💬 Begrüßung -->
    <h4 class="fw-semibold mb-3">Willkommen bei <span class="text-primary">FilterCair</span></h4>
    <p class="text-secondary mb-4">
        Wähle deine Produktionshalle und starte den Wartungsrundgang.
    </p>

    <!-- 🏭 Auswahl der Produktionshalle -->
    <div class="mb-4">
        <select class="form-select text-center" @onchange="OnHallChange">
            <option value="">Bitte Halle auswählen...</option>
            @foreach (var hall in halls)
            {
                <option value="@hall">@hall</option>
            }
        </select>
    </div>

    <!-- ⚙️ Aktionen -->
    <div class="d-grid gap-3">
        <button class="btn btn-primary btn-lg rounded-pill" @onclick="StartScan">
            <i class="bi bi-qr-code-scan me-2"></i> QR-Code scannen
        </button>

        <button class="btn btn-outline-secondary btn-lg rounded-pill" @onclick="ShowOfflineData">
            <i class="bi bi-database-down me-2"></i> Offline-Daten anzeigen
        </button>
    </div>

    <!-- 📍 Standortanzeige -->
    <div class="mt-4 text-muted small">
        📍 Standort: @currentLocation
    </div>
</div>

@code {
    private bool isOnline = true;
    private string? selectedHall;
    private List<string> halls = new() { "Halle A", "Halle B", "Halle C" };
    private string currentLocation = "unbekannt";

    protected override async Task OnInitializedAsync()
    {
        await JS.InvokeVoidAsync("FilterCair.registerNetworkHandler", DotNetObjectReference.Create(this));
        isOnline = await JS.InvokeAsync<bool>("FilterCair.isOnline");
        currentLocation = await JS.InvokeAsync<string>("FilterCair.getLocation");
    }

    [JSInvokable]
    public void UpdateNetworkStatus(bool online)
    {
        isOnline = online;
        StateHasChanged();
    }

    private void OnHallChange(ChangeEventArgs e)
    {
        selectedHall = e.Value?.ToString();
    }

    private void StartScan()
    {
        Console.WriteLine($"Starte Scan in {selectedHall}");
    }

    private void ShowOfflineData()
    {
        Console.WriteLine("Offline-Daten anzeigen");
    }

    // 🔐 Auth-Methoden
    [Inject] private IAccessTokenProvider TokenProvider { get; set; } = default!;

    private void Login()
    {
        Nav.NavigateTo("authentication/login", forceLoad: true);

    }

    private void Logout()
    {
        Nav.NavigateTo("authentication/logout");
    }
}
