#include "tracklistwidget.h"
#include <QFileInfo>
#include <QVBoxLayout>
#include "src/data/cleanerScripts.h"

TrackListWidget::TrackListWidget(const QStringList& audioFiles, QWidget* parent)
    : QWidget(parent)
{
    list_ = new QListWidget(this);

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(list_);

    int trackNum = 1;

    for (const QString& f : audioFiles) {

        // Extract just the filename
        QString fileName = QFileInfo(f).fileName();

        // Run through your cleaner
        QString songTitle = cleanTrackTitle(fileName);

        // Format display text: "1. Song Title"
        QString display = QString("%1. %2")
                              .arg(trackNum++)
                              .arg(songTitle);

        list_->addItem(display);
    }

    // Emit signal when clicked
    connect(list_, &QListWidget::itemClicked, this, [=](QListWidgetItem* item){
        int index = list_->row(item);
        emit trackSelected(index);
    });
}

void TrackListWidget::setCurrentRow(int row)
{
    list_->setCurrentRow(row);
}
