#include "FunctionListDialog.h"
#include "FunctionListPanel.h"

#include <QVBoxLayout>
#include <QSettings>
#include <QCloseEvent>

FunctionListDialog::FunctionListDialog(QWidget *parent)
    : QDialog(parent, Qt::Window)
{
    setWindowTitle(tr("Projektfunktionen – BareCode"));
    setMinimumSize(500, 400);

    // Fenstergröße und -position wiederherstellen
    QSettings s(QSettings::IniFormat, QSettings::UserScope, "BareCode", "BareCode");
    if (s.contains("funclist/geometry"))
    {
        restoreGeometry(s.value("funclist/geometry").toByteArray());
    }
    else
    {
        resize(600, 700);
    }

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);

    m_panel = new FunctionListPanel(this);
    m_panel->show(); // Immer sichtbar — Panel ist jetzt der gesamte Dialog-Inhalt
    layout->addWidget(m_panel);

    connect(m_panel, &FunctionListPanel::fileLineRequested,
            this, &FunctionListDialog::fileLineRequested);
}

void FunctionListDialog::setProjectRoot(const QString &path)
{
    m_panel->setProjectRoot(path);
}

void FunctionListDialog::refresh()
{
    m_panel->refresh();
}

void FunctionListDialog::closeEvent(QCloseEvent *event)
{
    QSettings s(QSettings::IniFormat, QSettings::UserScope, "BareCode", "BareCode");
    s.setValue("funclist/geometry", saveGeometry());
    event->accept();
}
