#include "AttackWidget.h"
#include <qpushbutton.h>
#include <cassert>
#include "softProj\helpers.h"
#include <qstring.h>
#include "math\SupportFunctions.h"
#include <iostream>

using namespace std;

AttackRecord::AttackRecord(void)
{
	this->id = false;
	this->playerName.clear();
	this->fortressName.clear();
}

AttackWidget::AttackWidget( QWidget *parent ): QWidget( parent)
{
	this->ui_.setupUi(this);

	/*
	pressed ( const QModelIndex & index )
	*/
	bool ok;
	ok = QObject::connect(	this->ui_.targetTable, SIGNAL( pressed ( const QModelIndex &) ),
								this, SLOT( recordSelectionSlot( const QModelIndex & )));

	ok = ok &&  QObject::connect(	this->ui_.closeButton, SIGNAL( clicked(void) ),
									this, SLOT( hideSlot(void) ));

	assert( ok && "Connection failed!" );

	

	this->ui_.targetTable->insertColumn( TARGET_PLAYER_COLUMN );
	this->ui_.targetTable->setColumnWidth( TARGET_PLAYER_COLUMN, 75 );

	this->ui_.targetTable->insertColumn( TARGET_SHIP_COLUMN );
	this->ui_.targetTable->setColumnWidth( TARGET_SHIP_COLUMN, 75 );

	this->ui_.targetTable->insertColumn( TARGET_ID_COLUMN );
	this->ui_.targetTable->setColumnWidth( TARGET_ID_COLUMN, 40 );

	rowCount = 0;
	
	this->clearRecords();
}
void AttackWidget::hideSlot( void )
{
	this->hide();
	emit this->hideSignal();
}
void AttackWidget::showSlot( void )
{
	this->show();
	emit this->showSignal();
}
void AttackWidget::clearRecords( void )
{
	this->ui_.targetTable->clear();

	//remove all rows
	for ( int i=0; i< this->rowCount; ++i)
	{
		this->ui_.targetTable->removeRow( 0 );
	}

	QStringList labels;
	labels.push_back( QString::fromUtf16(L"Hráè") );
	labels.push_back( "Pevnost" );
	labels.push_back( "Id" );
	this->ui_.targetTable->setHorizontalHeaderLabels( labels );
	this->rowCount = 0;
}
void AttackWidget::insertRecord(const std::vector< AttackRecord > &records )
{
	for ( int i=0; i< records.size(); ++i)
	{
		this->insertRecord( records[i] );
	}
}
void AttackWidget::insertRecord( const AttackRecord &record )
{
	this->ui_.targetTable->insertRow( this->rowCount );

	this->ui_.targetTable->setItem(  this->rowCount , TARGET_PLAYER_COLUMN, new QTableWidgetItem( record.playerName ));
	this->ui_.targetTable->setItem(  this->rowCount , TARGET_SHIP_COLUMN, new QTableWidgetItem( record.fortressName ));
	this->ui_.targetTable->setItem(  this->rowCount , TARGET_ID_COLUMN, new QTableWidgetItem( intToString(record.id).c_str()) );

	++this->rowCount;
}
void AttackWidget::recordSelectionSlot( /*const AttackRecord &record*/ const QModelIndex &index )
{
	 AttackRecord record;
	 record.playerName = this->ui_.targetTable->item( index.row(), TARGET_PLAYER_COLUMN)->data( Qt::DisplayRole ).toString();
	 record.fortressName = this->ui_.targetTable->item( index.row(), TARGET_SHIP_COLUMN)->data( Qt::DisplayRole ).toString();
	 record.id = this->ui_.targetTable->item( index.row(), TARGET_ID_COLUMN)->data( Qt::DisplayRole ).toULongLong();

	emit recordSelectionSignal( record );
	emit targetSelectionSignal( record.id );
}