#include "aboutwindow.h"
QModelIndex LicenseModel::index(int row, int column, const QModelIndex &parent) const {
    return createIndex(row, 0, row);
}
QModelIndex LicenseModel::parent(const QModelIndex &child) const {
    return QModelIndex();
}
Qt::ItemFlags LicenseModel::flags(const QModelIndex &index) const {
    return Qt::ItemFlag::ItemIsEnabled|Qt::ItemFlag::ItemIsSelectable;
}
int LicenseModel::rowCount(const QModelIndex &parent) const {
    return parent.isValid() ? 0 : licenseData.size();
}
int LicenseModel::columnCount(const QModelIndex &parent) const {
    return parent.isValid() ? 0 : 1;
}
QVariant LicenseModel::data(const QModelIndex &index, int role) const {
    if ((index.row() < 0 || licenseData.size() <= index.row()) && (role == Qt::DisplayRole || role == Qt::UserRole)) {
        return "";
    }
    switch (role) {
        case Qt::DisplayRole: {
            const LicenseData &data = licenseData[index.row()];
            return QString::asprintf("%s (%s)", data.Project.c_str(), data.Spdx.c_str());
        } break;
        case Qt::UserRole: {
            return QString(licenseData[index.row()].LicenseContents.c_str());
        } break;
        default: {
            return QVariant();
        } break;
    }
}
LicenseModel::LicenseModel() {
    auto tmp = get_license_data();
    for (auto data : tmp) {
        licenseData.push_back(data);
    }
}
LicenseModel::~LicenseModel() {
    
}
AboutWindow::AboutWindow() {
    license_text = new QTextBrowser(this);
    license_list = new QListView(this);
    license_list->setModel(new LicenseModel());
    QObject::connect(license_list, &QListView::clicked, [=,this](const QModelIndex &idx) {
        license_text->setText(license_list->model()->data(idx, Qt::UserRole).toString());
    });
    license_list->setSelectionMode(QAbstractItemView::SingleSelection);
    license_list->setSelectionBehavior(QAbstractItemView::SelectRows);
    QBoxLayout *mainLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
    QLabel *title = new QLabel("Looper", this);
    auto font = title->font();
    font.setPointSize(24);
    title->setFont(font);
    mainLayout->addWidget(title);
    QLabel *versionText = new QLabel(TAG, this);
    mainLayout->addWidget(versionText);
    QSplitter *splitter = new QSplitter(Qt::Orientation::Horizontal, this);
    splitter->addWidget(license_list);
    splitter->addWidget(license_text);
    mainLayout->addWidget(splitter);
    setLayout(mainLayout);
    license_list->clicked(license_list->model()->index(0, 0));
    setWindowTitle("About Looper");
}