/***************************************************************************
*   Copyright (C) 2006 by Ian Reinhart Geiser   *
*   geiseri@sourcextreme.com   *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU Library 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 Library General Public     *
*   License along with this program; if not, write to the                 *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "mainwindow.h"
#include "serverinstance.h"
#include "nodeinterface.h"
#include "remotenode.h"
#include "nodeid.h"

#include <QHostAddress>
#include <QDebug>


MainWindow::MainWindow( QWidget *parent )
		: QMainWindow( parent )
{
	KRPC::NodeID::registerNodeID();

	m_ui.setupUi(this);
	KRPC::ServerInstance::instance()->start();
	updateFields( );
}


MainWindow::~MainWindow()
{

}

void MainWindow::on_actionExit_activated( )
{
	close();
}

void MainWindow::on_join_clicked( )
{
	QHostAddress address( m_ui.remote_address->text());
	KRPC::NodeInterface *localNode = KRPC::ServerInstance::instance()->serverNode();
	localNode->join( new KRPC::RemoteNode( address, 8080));

	updateFields();

}

void MainWindow::updateFields( )
{

	KRPC::NodeInterface *localNode = KRPC::ServerInstance::instance()->serverNode();
	m_ui.previousNode->setText( localNode->predecessor()->address().toString() );
	m_ui.nextNode->setText( localNode->successor()->address().toString() );
	m_ui.thisNode->setText( localNode->key().toString() );
	m_ui.upperNode->setText( localNode->upper()->address().toString() );
	m_ui.lowerNode->setText( localNode->lower()->address().toString() );
}

void MainWindow::on_refresh_clicked( )
{
	updateFields();
}



