/***************************************************************************
 *   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/editprofileathletepage.h"

#include "core/profile.h"

#include "veneer/filedialog.h"
#include "veneer/settings.h"
#include "veneer/messagebox.h"
#include "veneer/url.h"

#include "util/unit.h"

#include <QDebug>
#include <QButtonGroup>

EditProfileAthletePage::EditProfileAthletePage(QWidget* parent) :
	QWidget(parent),
	m_Profile(NULL),
	m_GenderGroup(new QButtonGroup(this))
{
	setupUi(this);
}

void EditProfileAthletePage::setupPage(Profile* profile)
{
	m_GenderGroup->addButton(maleRadio, Profile::Male);
	m_GenderGroup->addButton(femaleRadio, Profile::Female);

	m_Profile = profile;
	m_AthletePhoto = m_Profile->photo();

	athleteNameEdit->setText(m_Profile->name().isEmpty() ? tr("(unnamed)", "profile name") : m_Profile->name());
	maleRadio->setChecked(m_Profile->gender() == Profile::Male);
	femaleRadio->setChecked(m_Profile->gender() == Profile::Female);
	Unit::setSpinBoxHeight(*heightSpinBox, m_Profile->height());
	athletePhotoButton->setIcon(m_Profile->photo());

	birthDateEdit->setDate(m_Profile->birthDate());
}

void EditProfileAthletePage::setupConnections()
{
	connect(athleteNameEdit, SIGNAL(textChanged(const QString&)), SLOT(onNameChanged(const QString&)));
	connect(birthDateEdit, SIGNAL(dateChanged(const QDate&)), SLOT(onBirthDateChanged(const QDate&)));
	connect(m_GenderGroup, SIGNAL(buttonClicked(int)), SLOT(onGenderChanged(int)));
	connect(heightSpinBox, SIGNAL(valueChanged(double)), SLOT(onHeightChanged(double)));
	connect(athletePhotoButton, SIGNAL(clicked(bool)), SLOT(onAthletePhotoButton(bool)));
}

bool EditProfileAthletePage::accept()
{
	if (athleteNameEdit->text().isEmpty())
	{
		Veneer::messageBoxInformation(this, tr("The profile name cannot be empty."), tr("Error"));
		return false;
	}

	m_Profile->setName(athleteNameEdit->text());
	m_Profile->setBirthDate(birthDateEdit->date());

	if (maleRadio->isChecked())
		m_Profile->setGender(Profile::Male);
	else if (femaleRadio->isChecked())
		m_Profile->setGender(Profile::Female);
	else
		m_Profile->setGender(Profile::Unknown);

	double height = heightSpinBox->value();

	if (Veneer::Settings::self()->measureSystem() == Veneer::Settings::EnumMeasureSystem::Imperial)
		height = Unit::toMetricHeight(height);

	m_Profile->setHeight(height * 1000.0);

	m_Profile->setPhoto(m_AthletePhoto);

	return true;
}

void EditProfileAthletePage::onNameChanged(const QString&)
{
	emit modified();
}

void EditProfileAthletePage::onBirthDateChanged(const QDate&)
{
	emit modified();
}

void EditProfileAthletePage::onGenderChanged(int)
{
	emit modified();
}

void EditProfileAthletePage::onHeightChanged(double)
{
	emit modified();
}

void EditProfileAthletePage::onAthletePhotoButton(bool)
{
	const QString fileName = Veneer::getOpenFileName(this, "editProfileAthletePhoto");
	if (fileName.isEmpty())
		return;

	if (!m_AthletePhoto.load(fileName))
		qWarning() << "could not load photo";

	m_AthletePhoto = m_AthletePhoto.scaled(athletePhotoButton->width() - 10, athletePhotoButton->height() - 10, Qt::KeepAspectRatio);
	athletePhotoButton->setIcon(m_AthletePhoto);

	emit modified();
}

