/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "DialogBreakPoints.h"
#include "DebuggerCoreInterface.h"
#include "Debugger.h"

#include <QHeaderView>
#include <QInputDialog>
#include <QMessageBox>

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
DialogBreakPoints::DialogBreakPoints(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
	ui.tableWidget->verticalHeader()->hide();
	ui.tableWidget->horizontalHeader()->setStretchLastSection(true);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogBreakPoints::showEvent(QShowEvent *) {
	update();
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogBreakPoints::update() {
	DebuggerCoreInterface::BreakPointState bpState;
	Debugger::debuggerBase->backupBreakPoints(bpState);

	ui.tableWidget->setSortingEnabled(false);
	ui.tableWidget->setRowCount(0);
	for(DebuggerCoreInterface::BreakPointState::const_iterator i = bpState.begin(); i != bpState.end(); ++i) {
		int row = ui.tableWidget->rowCount();
		ui.tableWidget->insertRow(row);
	
	
		ui.tableWidget->setItem(row, 0, new QTableWidgetItem(QString().sprintf("%08x", i.key())));
				
		QString bytes;

		for(int k = 0; k < i.value().originalBytes.size(); ++k) {
			bytes += QString().sprintf("%02x", i.value().originalBytes[k] & 0xff);
			if(k != i.value().originalBytes.size() - 1) {
				bytes += ' ';
			}
		}
		

		ui.tableWidget->setItem(row, 1, new QTableWidgetItem(bytes));
		// TODO
		// ui.tableWidget->setItem(row, 2, new QTableWidgetItem(i->second.c_str()));
	}
	ui.tableWidget->setSortingEnabled(true);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogBreakPoints::on_btnAdd_clicked() {

    bool ok;
    QString text = QInputDialog::getText(this, "Add Breakpoint", "Address:", QLineEdit::Normal, "", &ok);
    if (ok && !text.isEmpty()) {
		const BaseTypes::address_t address = text.toUInt(&ok, 16);
		if(ok) {
			Debugger::createBreakPoint(address);
			update();
		}
	}
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogBreakPoints::on_btnRemove_clicked() {
	QList<QTableWidgetItem *> sel = ui.tableWidget->selectedItems();
	if(sel.size() != 0) {
		bool ok;
		const BaseTypes::address_t address = sel.begin()[0]->text().toUInt(&ok, 16);
		if(ok) {
			Debugger::removeBreakPoint(address);
		}
		update();
	}
}
