/*
 * Copyright (C) 2009 Matthew Gates
 *
 * 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 "TuiNodeDateTime.hpp"
#include "StelNavigator.hpp"
#include "StelUtils.hpp"
#include <QKeyEvent>
#include <QDebug>
#include <QStringList>

TuiNodeDateTime::TuiNodeDateTime(const QString& text, QObject* receiver, const char* method, double defValue, TuiNode* parent, TuiNode* prev)
	: TuiNodeEditable(text, parent, prev), value(defValue), editingPart(0), typing(false)
{
	this->connect(this, SIGNAL(setValue(double)), receiver, method);
}

TuiNodeResponse TuiNodeDateTime::handleEditingKey(int key)
{
	TuiNodeResponse response;
	response.accepted = false;
	response.newNode = this;
	if (key==Qt::Key_Left)
	{
		typing = false;
		if (editingPart==0)
		{
			editing = false;
		}
		else
		{
			editingPart--;
		}
		response.accepted = true;
		response.newNode = this;
		if (!editing)
		{
			emit(setValue(value));
		}
		return response;
	}
	if (key==Qt::Key_Return)
	{
		typing = false;
		editing = false;
		editingPart = 0;
		response.accepted = true;
		response.newNode = this;
		emit(setValue(value));
		return response;
	}
	if (key==Qt::Key_Right)
	{
		typing = false;
		if (editingPart<5)
		{
			editingPart++;
		}
		response.accepted = true;
		response.newNode = this;
		return response;
	}
	if (key==Qt::Key_Up)
	{
		typing = false;
		incPart(editingPart, true);
		response.accepted = true;
		response.newNode = this;
		emit(setValue(value));
		return response;
	}
	if (key==Qt::Key_Down)
	{
		typing = false;
		incPart(editingPart, false);
		response.accepted = true;
		response.newNode = this;
		emit(setValue(value));
		return response;
	}
	if (key>=Qt::Key_0 && key<=Qt::Key_9 && editingPart==0)
	{
		QString s;
		if (!typing)
		{
			s = QString("%1").arg(key - Qt::Key_0);
			typing = true;
		}
		else
		{
			s = QString("%1%2").arg(getParts(value).at(editingPart)).arg(key - Qt::Key_0);
		}
		response.accepted = true;
		response.newNode = this;
		if (setPart(editingPart, s.toInt()))
		{
			emit(setValue(value));
		}
		return response;
	}
	return response;
}

QString TuiNodeDateTime::getDisplayText() 
{
	QList<int> parts = getParts(value);
	if (editing && editingPart==0)
		return displayText + QString(":  >%1<-%2-%3 %4:%5:%6 UTC").arg(parts.at(0)).arg(parts.at(1), 2, 10, QChar('0')).arg(parts.at(2), 2, 10, QChar('0')).arg(parts.at(3), 2, 10, QChar('0')).arg(parts.at(4), 2, 10, QChar('0')).arg(parts.at(5), 2, 10, QChar('0'));
	else if (editing && editingPart==1)
		return displayText + QString(":  %1->%2<-%3 %4:%5:%6 UTC").arg(parts.at(0)).arg(parts.at(1), 2, 10, QChar('0')).arg(parts.at(2), 2, 10, QChar('0')).arg(parts.at(3), 2, 10, QChar('0')).arg(parts.at(4), 2, 10, QChar('0')).arg(parts.at(5), 2, 10, QChar('0'));
	else if (editing && editingPart==2)
		return displayText + QString(":  %1-%2->%3< %4:%5:%6 UTC").arg(parts.at(0)).arg(parts.at(1), 2, 10, QChar('0')).arg(parts.at(2), 2, 10, QChar('0')).arg(parts.at(3), 2, 10, QChar('0')).arg(parts.at(4), 2, 10, QChar('0')).arg(parts.at(5), 2, 10, QChar('0'));
	else if (editing && editingPart==3)
		return displayText + QString(":  %1-%2-%3 >%4<:%5:%6 UTC").arg(parts.at(0)).arg(parts.at(1), 2, 10, QChar('0')).arg(parts.at(2), 2, 10, QChar('0')).arg(parts.at(3), 2, 10, QChar('0')).arg(parts.at(4), 2, 10, QChar('0')).arg(parts.at(5), 2, 10, QChar('0'));
	else if (editing && editingPart==4)
		return displayText + QString(":  %1-%2-%3 %4:>%5<:%6 UTC").arg(parts.at(0)).arg(parts.at(1), 2, 10, QChar('0')).arg(parts.at(2), 2, 10, QChar('0')).arg(parts.at(3), 2, 10, QChar('0')).arg(parts.at(4), 2, 10, QChar('0')).arg(parts.at(5), 2, 10, QChar('0'));
	else if (editing && editingPart==5)
		return displayText + QString(":  %1-%2-%3 %4:%5:>%6< UTC").arg(parts.at(0)).arg(parts.at(1), 2, 10, QChar('0')).arg(parts.at(2), 2, 10, QChar('0')).arg(parts.at(3), 2, 10, QChar('0')).arg(parts.at(4), 2, 10, QChar('0')).arg(parts.at(5), 2, 10, QChar('0'));
	else
		return displayText + QString(":  %1-%2-%3 %4:%5:%6 UTC").arg(parts.at(0)).arg(parts.at(1), 2, 10, QChar('0')).arg(parts.at(2), 2, 10, QChar('0')).arg(parts.at(3), 2, 10, QChar('0')).arg(parts.at(4), 2, 10, QChar('0')).arg(parts.at(5), 2, 10, QChar('0'));
}

void TuiNodeDateTime::incPart(int part, bool add)
{
	Q_UNUSED(part);
	int diff = 1;
	if (!add)
		diff *= -1;

	QList<int> parts = getParts(value);

	if (editingPart==0)
	{
		double newDate;
		if (StelUtils::getJDFromDate(&newDate, parts.at(editingPart) + diff, parts.at(1), parts.at(2), parts.at(3), parts.at(4), parts.at(5)))
			value = newDate;
	}
	else if (editingPart==1)
	{
		double newDate;
		int m = ((parts.at(editingPart)-1+diff) % 12) + 1;
		if (m==0) m=12;
		qDebug() << "new m is" << m;
		if (StelUtils::getJDFromDate(&newDate, parts.at(0), m, parts.at(2), parts.at(3), parts.at(4), parts.at(5)))
			value = newDate;
	}
	else if (editingPart==2)
	{
		value += (diff * JD_DAY);
	}
	else if (editingPart==3)
	{
		value += (diff * JD_HOUR);
	}
	else if (editingPart==4)
	{
		value += (diff * JD_MINUTE);
	}
	else if (editingPart==5)
	{
		value += (diff * JD_SECOND);
	}
}

QList<int> TuiNodeDateTime::getParts(double jd)
{
	int year, month, day, hour, minute, second;
	StelUtils::getDateFromJulianDay(jd, &year, &month, &day);
	StelUtils::getTimeFromJulianDay(jd, &hour, &minute, &second);
	QList<int> parts;
	parts << year << month << day << hour << minute << second;
	return parts;
}

bool TuiNodeDateTime::setPart(int part, int val)
{
	QList<int> parts = getParts(value);
	parts.replace(part, val);
	double newDate;
	if (StelUtils::getJDFromDate(&newDate, parts.at(0), parts.at(1), parts.at(2), parts.at(3), parts.at(4), parts.at(5)))
	{
		value = newDate;
		return true;
	}
	else
	{
		return false;
	}
}

