/***************************************************************************
*   Copyright (C) 2006 by Oscar Ljunggren   *
*   oscar.ljunggren@gmail.com   *
*                                                                         *
*   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.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "reports_handler.h"
#include <kdatewidget.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlistbox.h>
#include <qfile.h>
#include <kapp.h>
#include "settings.h"
#include <kmessagebox.h>
#include <klocale.h>


reports_handler::reports_handler(QWidget *parent, const char *name)
:reports(parent, name)
{
	dateEdit->setDate(QDate::currentDate());
	kcfg_ReportList->clear();
	kcfg_ReportList->insertStringList(Settings::reportList() );
	//readFromFile();
}

void reports_handler::quitButton_clicked()
{
}

void reports_handler::addButton_clicked()
{

QString reportData;
	if(restingPulse->text()!=""){
		reportData.append(dateEdit->date().toString(Qt::ISODate));
		reportData.append("  Resting Pulse(");
		reportData.append(restingPulse->text());
		reportData.append(")");
		if(weight->text()!=""){
			reportData.append("  Weight(");
			reportData.append(weight->text());
			reportData.append(")");
		}
		if(other->text()!=""){
			reportData.append("  | ");
			reportData.append(other->text());
		}
		kcfg_ReportList->insertItem(reportData);
	}
	else if(restingPulse->text()==""){
		if(weight->text()!=""){	
			reportData.append(dateEdit->date().toString());
			reportData.append("  Weight(");
			reportData.append(weight->text());
			reportData.append(")");
			if(other->text()!=""){
				reportData.append("  | ");
				reportData.append(other->text());
			}
			kcfg_ReportList->insertItem(reportData);		
		}
	
	}

	// 2. Clear the fields
	
	restingPulse->setText("");
	weight->setText("");
	other->setText("");
}


void reports_handler::removeButton_clicked()
{
	int reply = KMessageBox::questionYesNo(this, i18n("Do you really want to delete the selected report?"),"Message", KStdGuiItem::yes(), KStdGuiItem::no());
	if (reply==4)
		return;
	kcfg_ReportList->removeItem(kcfg_ReportList->currentItem());
	settingsChanged();
	
}
void reports_handler::settingsChanged() 
{
	
	QStringList list;
	for (int i=0; i< (kcfg_ReportList->numRows()); i++)
		list.append(kcfg_ReportList->text(i));	
	
	Settings::setReportList(list);
	Settings::writeConfig();
	
	kcfg_ReportList->clear();
	kcfg_ReportList->insertStringList(Settings::reportList() );
}


#include "reports_handler.moc"
