/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   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 "gui/graphdockwidget.h"

#include "core/activity.h"
#include "core/lap.h"
#include "core/track.h"
#include "core/trackpoint.h"

#include "util/plotwidget/kplotobject.h"
#include "util/plotwidget/kplotaxis.h"

#include <QDebug>
#include <QButtonGroup>

GraphDockWidget::GraphDockWidget(QWidget* parent) :
	QWidget(parent),
	m_Activity(NULL),
	m_GraphTypeGroup(new QButtonGroup(this))
{
	setupUi(this);

	init();
	setupConnections();
}

void GraphDockWidget::init()
{
	graphWidget->setAntialiasing(true);

	graphWidget->axis(KPlotWidget::TopAxis)->setVisible(false);
	graphWidget->axis(KPlotWidget::RightAxis)->setVisible(false);

	m_GraphTypeGroup->addButton(speedRadio, 0);
	m_GraphTypeGroup->addButton(altitudeRadio, 1);
	m_GraphTypeGroup->addButton(cadenceRadio, 2);
	m_GraphTypeGroup->addButton(heartRateRadio, 3);

	speedRadio->setChecked(true);
}

void GraphDockWidget::setupConnections()
{
	connect(m_GraphTypeGroup, SIGNAL(buttonClicked(int)), SLOT(onGraphTypeButtonClicked(int)));
}

void GraphDockWidget::drawGraph()
{
	graphWidget->resetPlot();
	graphWidget->axis(KPlotWidget::LeftAxis)->setLabel(QString());

	Qt::GlobalColor cols[4] = { Qt::red, Qt::green, Qt::blue, Qt::magenta };
	QStringList titles;
	titles << tr("Speed") << tr("Altitude") << tr("Cadence") << tr("Heart Rate");

	if (m_Activity == NULL || m_GraphTypeGroup->checkedId() < 0 || m_GraphTypeGroup->checkedId() > titles.size() || !parentWidget()->isVisible())
	{
		setEnabled(false);
		return;
	}

	setEnabled(true);

	KPlotObject* plot = new KPlotObject(cols[m_GraphTypeGroup->checkedId()], KPlotObject::Lines, 3);

	quint32 count = 0;
	qint32 minY = INT_MAX;
	qint32 maxY = 0;
	quint32 numDataPoints = 0;

	foreach (const Lap* lap, m_Activity->laps())
		foreach(const Track* track, lap->tracks())
		{
			for (TrackPoints::const_iterator i = track->trackPoints().constBegin(); i != track->trackPoints().constEnd(); ++i)
			{
				if (count++ % 2 != 0)
					continue;

				const TrackPoint* tp = *i;

				double value = 0;
				switch (m_GraphTypeGroup->checkedId())
				{
					case 0:
						value = tp->speed() / 1000.0;
						break;

					case 1:
						value = tp->altitude() / 1000.0;
						break;

					case 2:
						value = tp->cadence();
						break;

					case 3:
						value = tp->heartRate();
						break;
				}

				plot->addPoint(numDataPoints++, value);

				if (value > maxY)
					maxY = value;

				if (value < minY)
					minY = value;
			}
		}

	graphWidget->setLimits(0, numDataPoints, minY - minY / 5, maxY + maxY / 5);

	graphWidget->addPlotObject(plot);

	graphWidget->axis(KPlotWidget::LeftAxis)->setLabel(titles[m_GraphTypeGroup->checkedId()]);

	graphWidget->axis(KPlotWidget::BottomAxis)->setTickLabelsShown(false);

	graphWidget->update();
}

void GraphDockWidget::onGraphTypeButtonClicked(int)
{
	drawGraph();
}
