/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qlabel.h>
#include <qfontmetrics.h>
#include <qlayout.h>

#include <klocale.h>
// #include <kwin.h>

#include "kdevmonconfig.h"
#include "utils.h"
#include "statsframe.h"

#include <iostream>

StatsFrame::StatsFrame( QWidget *parent ) : QWidget( parent )
{
	createGUI();
	initFromConfig();
	
	setCaption( i18n("Kdevmon - Stats") );
}

StatsFrame::~StatsFrame()
{
}

void StatsFrame::initFromConfig()
{
	KdevmonConfig *conf = KdevmonConfig::getConfig();
	
	QPalette pal( QColor(228,228,228), conf->getStatsFrameConfig()->bgColor );
	setPalette( pal );

	pal.setColor( QColorGroup::Foreground, conf->getCommonConfig()->colorIn );

	m_inLabel->setPalette(pal);
	m_currentInBytesValue->setPalette(pal);
	m_maximumInBytesValue->setPalette(pal);
	m_overallInBytesValue->setPalette(pal);
	m_currentInPacketsValue->setPalette(pal);
	m_maximumInPacketsValue->setPalette(pal);
	m_overallInPacketsValue->setPalette(pal);

	pal.setColor( QColorGroup::Foreground, conf->getCommonConfig()->colorOut );
	m_outLabel->setPalette(pal);
	m_currentOutBytesValue->setPalette(pal);
	m_maximumOutBytesValue->setPalette(pal);
	m_overallOutBytesValue->setPalette(pal);
	m_currentOutPacketsValue->setPalette(pal);
	m_maximumOutPacketsValue->setPalette(pal);
	m_overallOutPacketsValue->setPalette(pal);
}

void StatsFrame::updateView(
	const Q3MemArray<double> & bytes, const Q3MemArray<double> & packets )
{
	if( (bytes.size() != 6) || (packets.size() != 6) )
		return;
	
	KdevmonConfig *conf = KdevmonConfig::getConfig();
	
	struct FormattedValue formattedValues[6];
	if( conf->getCommonConfig()->unit == Common::UNIT_BYTE )
		for( int i=0; i<6; i++ )
			formattedValues[i] = Utils::getFormattedBytes(bytes[i]);
	else
	{
		formattedValues[0] = Utils::getFormattedBits(bytes[0]);
		formattedValues[1] = Utils::getFormattedBits(bytes[1]);
		formattedValues[3] = Utils::getFormattedBits(bytes[3]);
		formattedValues[4] = Utils::getFormattedBits(bytes[4]);
		// always print the overall amount in Bytes
		formattedValues[2] = Utils::getFormattedBytes(bytes[2]);
		formattedValues[5] = Utils::getFormattedBytes(bytes[5]);
	}

	m_currentInBytesValue->setText(formattedValues[0].valueString);
	m_currentInBytesUnit->setText(formattedValues[0].unitString+"/s");
	m_maximumInBytesValue->setText(formattedValues[1].valueString);
	m_maximumInBytesUnit->setText(formattedValues[1].unitString+"/s");
	m_overallInBytesValue->setText(formattedValues[2].valueString);
	m_overallInBytesUnit->setText(formattedValues[2].unitString);
	m_currentOutBytesValue->setText(formattedValues[3].valueString);
	m_currentOutBytesUnit->setText(formattedValues[3].unitString+"/s");
	m_maximumOutBytesValue->setText(formattedValues[4].valueString);
	m_maximumOutBytesUnit->setText(formattedValues[4].unitString+"/s");
	m_overallOutBytesValue->setText(formattedValues[5].valueString);
	m_overallOutBytesUnit->setText(formattedValues[5].unitString);
	
	for( int i=0; i<6; i++ )
		formattedValues[i] = Utils::getFormattedPackets(packets[i]);

	m_currentInPacketsValue->setText(formattedValues[0].valueString);
	m_currentInPacketsUnit->setText(formattedValues[0].unitString+"/s");
	m_maximumInPacketsValue->setText(formattedValues[1].valueString);
	m_maximumInPacketsUnit->setText(formattedValues[1].unitString+"/s");
	m_overallInPacketsValue->setText(formattedValues[2].valueString);
	m_overallInPacketsUnit->setText(formattedValues[2].unitString);
	m_currentOutPacketsValue->setText(formattedValues[3].valueString);
	m_currentOutPacketsUnit->setText(formattedValues[3].unitString+"/s");
	m_maximumOutPacketsValue->setText(formattedValues[4].valueString);
	m_maximumOutPacketsUnit->setText(formattedValues[4].unitString+"/s");
	m_overallOutPacketsValue->setText(formattedValues[5].valueString);
	m_overallOutPacketsUnit->setText(formattedValues[5].unitString);
}

void StatsFrame::createGUI()
{
	QFontMetrics metrics( font() );
	int valueWidth = metrics.width( "0.000" ) + 5;
	int unitWidth = metrics.width( "e3 pkts/s" ) + 5;

	QGridLayout *m_currentLayout = new QGridLayout();
	m_currentLayout->setSpacing(2);
	m_currentLayout->setMargin(2);

	m_currentInBytesValue = new QLabel(this);
	m_currentInBytesValue->setMinimumSize(valueWidth, 0);
	m_currentInBytesValue->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_currentInBytesUnit = new QLabel(this);
	m_currentInBytesUnit->setMinimumSize(unitWidth, 0);
	m_currentLayout->addWidget(m_currentInBytesValue, 0, 0);
	m_currentLayout->addWidget(m_currentInBytesUnit, 0, 1);

	m_currentInPacketsValue = new QLabel(this);
	m_currentInPacketsValue->setMinimumSize(valueWidth, 0);
	m_currentInPacketsValue->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_currentInPacketsUnit = new QLabel(this);
	m_currentInPacketsUnit->setMinimumSize(unitWidth, 0);
	m_currentLayout->addWidget(m_currentInPacketsValue, 1, 0);
	m_currentLayout->addWidget(m_currentInPacketsUnit, 1, 1);

	m_currentOutBytesValue = new QLabel(this);
	m_currentOutBytesValue->setMinimumSize(valueWidth, 0);
	m_currentOutBytesValue->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_currentOutBytesUnit = new QLabel(this);
	m_currentOutBytesUnit->setMinimumSize(unitWidth, 0);
	m_currentLayout->addWidget(m_currentOutBytesValue, 2, 0);
	m_currentLayout->addWidget(m_currentOutBytesUnit, 2, 1);

	m_currentOutPacketsValue = new QLabel(this);
	m_currentOutPacketsValue->setMinimumSize(valueWidth, 0);
	m_currentOutPacketsValue->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_currentOutPacketsUnit = new QLabel(this);
	m_currentOutPacketsUnit->setMinimumSize(unitWidth, 0);
	m_currentLayout->addWidget(m_currentOutPacketsValue, 3, 0);
	m_currentLayout->addWidget(m_currentOutPacketsUnit, 3, 1);

	QGridLayout *m_maximumLayout = new QGridLayout();
	m_maximumLayout->setSpacing(2);
	m_maximumLayout->setMargin(2);

	m_maximumInBytesValue = new QLabel(this);
	m_maximumInBytesValue->setMinimumSize(valueWidth, 0);
	m_maximumInBytesValue->setAlignment(
		Qt::AlignRight | Qt::AlignVCenter);
	m_maximumInBytesUnit = new QLabel(this);
	m_maximumInBytesUnit->setMinimumSize(unitWidth, 0);
	m_maximumLayout->addWidget(m_maximumInBytesValue, 0, 0);
	m_maximumLayout->addWidget(m_maximumInBytesUnit, 0, 1);

	m_maximumInPacketsValue = new QLabel(this);
	m_maximumInPacketsValue->setMinimumSize(valueWidth, 0);
	m_maximumInPacketsValue->setAlignment(
		Qt::AlignRight | Qt::AlignVCenter);
	m_maximumInPacketsUnit = new QLabel(this);
	m_maximumInPacketsUnit->setMinimumSize(unitWidth, 0);
	m_maximumLayout->addWidget(m_maximumInPacketsValue, 1, 0);
	m_maximumLayout->addWidget(m_maximumInPacketsUnit, 1, 1);

	m_maximumOutBytesValue = new QLabel(this);
	m_maximumOutBytesValue->setMinimumSize(valueWidth, 0);
	m_maximumOutBytesValue->setAlignment(
		Qt::AlignRight | Qt::AlignVCenter);
	m_maximumOutBytesUnit = new QLabel(this);
	m_maximumOutBytesUnit->setMinimumSize(unitWidth, 0);
	m_maximumLayout->addWidget(m_maximumOutBytesValue, 2, 0);
	m_maximumLayout->addWidget(m_maximumOutBytesUnit, 2, 1);

	m_maximumOutPacketsValue = new QLabel(this);
	m_maximumOutPacketsValue->setMinimumSize(valueWidth, 0);
	m_maximumOutPacketsValue->setAlignment(
		Qt::AlignRight | Qt::AlignVCenter);
	m_maximumOutPacketsUnit = new QLabel(this);
	m_maximumOutPacketsUnit->setMinimumSize(unitWidth, 0);
	m_maximumLayout->addWidget(m_maximumOutPacketsValue, 3, 0);
	m_maximumLayout->addWidget(m_maximumOutPacketsUnit, 3, 1);

	QGridLayout *m_overallLayout = new QGridLayout();
	m_overallLayout->setSpacing(2);
	m_overallLayout->setMargin(2);

	m_overallInBytesValue = new QLabel(this);
	m_overallInBytesValue->setMinimumSize(valueWidth, 0);
	m_overallInBytesValue->setAlignment(
		Qt::AlignRight | Qt::AlignVCenter);
	m_overallInBytesUnit = new QLabel(this);
	m_overallInBytesUnit->setMinimumSize(unitWidth, 0);
	m_overallLayout->addWidget(m_overallInBytesValue, 0, 0);
	m_overallLayout->addWidget(m_overallInBytesUnit, 0, 1);

	m_overallInPacketsValue = new QLabel(this);
	m_overallInPacketsValue->setMinimumSize(valueWidth, 0);
	m_overallInPacketsValue->setAlignment(
		Qt::AlignRight | Qt::AlignVCenter);
	m_overallInPacketsUnit = new QLabel(this);
	m_overallInPacketsUnit->setMinimumSize(unitWidth, 0);
	m_overallLayout->addWidget(m_overallInPacketsValue, 1, 0);
	m_overallLayout->addWidget(m_overallInPacketsUnit, 1, 1);

	m_overallOutBytesValue = new QLabel(this);
	m_overallOutBytesValue->setMinimumSize(valueWidth, 0);
	m_overallOutBytesValue->setAlignment(
		Qt::AlignRight | Qt::AlignVCenter);
	m_overallOutBytesUnit = new QLabel(this);
	m_overallOutBytesUnit->setMinimumSize(unitWidth, 0);
	m_overallLayout->addWidget(m_overallOutBytesValue, 2, 0);
	m_overallLayout->addWidget(m_overallOutBytesUnit, 2, 1);

	m_overallOutPacketsValue = new QLabel(this);
	m_overallOutPacketsValue->setMinimumSize(valueWidth, 0);
	m_overallOutPacketsValue->setAlignment(
		Qt::AlignRight | Qt::AlignVCenter);
	m_overallOutPacketsUnit = new QLabel(this);
	m_overallOutPacketsUnit->setMinimumSize(unitWidth, 0);
	m_overallLayout->addWidget(m_overallOutPacketsValue, 3, 0);
	m_overallLayout->addWidget(m_overallOutPacketsUnit, 3, 1);

	m_inLabel = new QLabel(this);
	m_inLabel->setText("In:");
	m_outLabel = new QLabel(this);
	m_outLabel->setText("Out:");

	m_currentHeader = new QLabel(this);
	m_currentHeader->setText( i18n("current") );
	m_currentHeader->setAlignment(Qt::AlignHCenter);
	m_maximumHeader = new QLabel(this);
	m_maximumHeader->setText( i18n("maximum") );
	m_maximumHeader->setAlignment(Qt::AlignHCenter);
	m_overallHeader = new QLabel(this);
	m_overallHeader->setText( i18n("overall") );
	m_overallHeader->setAlignment(Qt::AlignHCenter);
	
	m_mainLayout = new QGridLayout(this);
	m_mainLayout->setSpacing(2);
	m_mainLayout->setMargin(2);
	m_mainLayout->addWidget(m_inLabel, 1, 0 );
	m_mainLayout->addWidget(m_outLabel, 2, 0 );
	m_mainLayout->addWidget(m_currentHeader, 0, 1);
	m_mainLayout->addWidget(m_maximumHeader, 0, 2);
	m_mainLayout->addWidget(m_overallHeader, 0, 3);
	m_mainLayout->addMultiCellLayout(m_currentLayout, 1, 2, 1, 1);
	m_mainLayout->addMultiCellLayout(m_maximumLayout, 1, 2, 2, 2);
	m_mainLayout->addMultiCellLayout(m_overallLayout, 1, 2, 3, 3);
}
