@inherits LayoutComponentBase
@inject IProjectService ProjectService
@inject ISettingsService SettingsService
@inject IThemeService ThemeService
@inject OllamaSettings OllamaSettings

<div class="page">
    <div class="main-content">
        <aside class="sidebar">
            <div class="nav-section">
                <button class="nav-section-header" @onclick='() => ToggleSection("file")'>
                    <span class="nav-icon">@(activeSection == "file" ? "▼" : "▶")</span>
                    Datei
                </button>
                @if (activeSection == "file")
                {
                    <div class="nav-section-body">
                        <button class="nav-item" @onclick="CreateNewProject">Neues Projekt</button>
                        <button class="nav-item" @onclick="OpenProject">Öffnen</button>
                        <div class="nav-separator"></div>
                        <button class="nav-item" @onclick="ExitApp">Beenden</button>
                    </div>
                }
            </div>
            <div class="nav-section">
                <button class="nav-section-header" @onclick='() => ToggleSection("view")'>
                    <span class="nav-icon">@(activeSection == "view" ? "▼" : "▶")</span>
                    Ansicht
                </button>
                @if (activeSection == "view")
                {
                    <div class="nav-section-body">
                        <button class="nav-item" @onclick="OpenEditor">Editor öffnen</button>
                    </div>
                }
            </div>
            <div class="nav-section">
                <button class="nav-section-header" @onclick='() => ToggleSection("extras")'>
                    <span class="nav-icon">@(activeSection == "extras" ? "▼" : "▶")</span>
                    Extras
                </button>
                @if (activeSection == "extras")
                {
                    <div class="nav-section-body">
                        <button class="nav-item" @onclick='() => NavigateTo("/settings")'>Einstellungen</button>
                        <button class="nav-item" @onclick='() => NavigateTo("/models")'>Modelle verwalten</button>
                    </div>
                }
            </div>
            <div class="nav-section">
                <button class="nav-section-header" @onclick='() => ToggleSection("projects")'>
                    <span class="nav-icon">@(activeSection == "projects" ? "▼" : "▶")</span>
                    Projekte
                </button>
                @if (activeSection == "projects")
                {
                    <div class="nav-section-body">
                        <button class="nav-item nav-item-toggle" @onclick='() => showProjectList = !showProjectList'>
                            <span class="nav-icon">@(showProjectList ? "▼" : "▶")</span>
                            Liste vorhandener Projekte
                        </button>
                        @if (showProjectList)
                        {
                            <div class="nav-sublist">
                                @if (projects == null)
                                {
                                    <span class="nav-hint">Lade Projekte...</span>
                                }
                                else if (projects.Count == 0)
                                {
                                    <span class="nav-hint">Keine Projekte vorhanden.</span>
                                }
                                else
                                {
                                    @foreach (var p in projects)
                                    {
                                        <button class="nav-item nav-item-sub" @onclick='() => SelectProject(p)'>@p.Name</button>
                                    }
                                }
                            </div>
                        }
                        <div class="nav-separator"></div>
                        <button class="nav-item" @onclick="CreateNewProject">Projekt erstellen</button>
                        <button class="nav-item" @onclick="OpenProject">Projekt laden</button>
                    </div>
                }
            </div>
        </aside>

        <main class="detail-panel">
            @Body
        </main>
    </div>

    <footer class="status-bar">
        <span class="status-dot connected"></span>
        <span class="status-text">@statusText</span>
        @if (isBusy)
        {
            <progress value="@operationProgress" max="100"></progress>
        }
    </footer>
</div>

@code {
    private string? activeSection;
    private bool showProjectList;
    private List<Project>? projects;
    private string statusText = "Bereit";
    private bool isBusy;
    private double operationProgress;

    [Inject]
    private NavigationManager Navigation { get; set; } = default!;

    protected override async Task OnInitializedAsync()
    {
        await LoadProjects();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await SettingsService.InitializeAsync();
            var theme = OllamaSettings.Theme;
            if (!string.IsNullOrEmpty(theme))
                await ThemeService.ApplyTheme(theme);
        }
    }

    private async Task LoadProjects()
    {
        try
        {
            projects = await ProjectService.GetAllProjectsAsync();
        }
        catch
        {
            projects = new();
        }
    }

    private void ToggleSection(string section)
    {
        activeSection = activeSection == section ? null : section;
    }

    private void ExitApp()
    {
        activeSection = null;
        Navigation.NavigateTo("/");
    }

    private void CreateNewProject()
    {
        activeSection = null;
        Navigation.NavigateTo("/editor?action=newproject");
    }

    private void OpenProject()
    {
        activeSection = null;
        Navigation.NavigateTo("/editor?action=open");
    }

    private void OpenEditor()
    {
        activeSection = null;
        Navigation.NavigateTo("/editor");
    }

    private void NavigateTo(string path)
    {
        activeSection = null;
        Navigation.NavigateTo(path);
    }

    private void SelectProject(Project project)
    {
        activeSection = null;
        Navigation.NavigateTo($"/editor?projectId={project.Id}");
    }

    public void SetStatus(string text, bool busy = false, double progress = 0)
    {
        statusText = text;
        isBusy = busy;
        operationProgress = progress;
        StateHasChanged();
    }
}
