#include "rumblegraphviewer.h"
#include "ui_rumblegraphviewer.h"

RumbleGraphViewer::RumbleGraphViewer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RumbleGraphViewer),
    mEntryCount(),
    mRumbleGraphFile(nullptr) {
    ui->setupUi(this);

    ui->tableWidget_Entries->setColumnCount(2);
    ui->tableWidget_Entries->setHorizontalHeaderLabels({ "X", "Y" });
    ui->tableWidget_Entries->setRowCount(0);
    ui->tableWidget_Entries->setColumnWidth(0, 200);
    ui->tableWidget_Entries->horizontalHeader()->setStretchLastSection(true);
}

RumbleGraphViewer::~RumbleGraphViewer() {
    delete ui;
}

void RumbleGraphViewer::SetRumbleGraphFile(const XRawFile* aRawFile) {
    mRumbleGraphFile = aRawFile;

    XDataStream rawFileStream;//(mRumbleGraphFile->contents.toLatin1());

    QByteArray magic(15, Qt::Uninitialized);
    rawFileStream.readRawData(magic.data(), 15);

    rawFileStream.skipRawData(4);

    char sectionChar;
    rawFileStream >> sectionChar;
    int sectionCount = sectionChar - '0';
    ui->tableWidget_Entries->setRowCount(sectionCount);
    ui->spinBox_Entries->setValue(sectionCount);
    ui->groupBox_LocalStrViewer->setTitle(QString("Entries (%1)").arg(sectionCount));

    rawFileStream.skipRawData(2);

    for (int i = 0; i < sectionCount; i++) {
        QByteArray xVal(6, Qt::Uninitialized), yVal(6, Qt::Uninitialized);
        rawFileStream.readRawData(xVal.data(), 6);
        rawFileStream.skipRawData(1);
        rawFileStream.readRawData(yVal.data(), 6);
        rawFileStream.skipRawData(2);

        QTableWidgetItem *xItem = new QTableWidgetItem(xVal);
        QTableWidgetItem *yItem = new QTableWidgetItem(yVal);

        ui->tableWidget_Entries->setItem(i, 0, xItem);
        ui->tableWidget_Entries->setItem(i, 1, yItem);
    }
}
