/*******************************************************************************
***  This file is part of Dux Rei Nummariae.                                 ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Affero General Public License as published   ***
***  by the Free Software Foundation, either version 3 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 Affero General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Affero General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include "XmlAttribute.h"
using ::QLatin1String;
using ::QObject;
using ::QXmlStreamReader;
using drn::file_storage::AttributeError;
using drn::file_storage::internal::readAttributeString;

#include <QDate>
using ::QDate;
#include <QXmlStreamWriter>
using ::QXmlStreamWriter;


QString drn::file_storage::internal::readAttributeString(
	QXmlStreamReader& xml,
	const QLatin1String& attribute
)
{
	const auto value{xml.attributes().value(attribute).toString()};

	if (value.isEmpty())
		throw AttributeError{
			xml.name().toString(),
			attribute,
			xml.errorString(),
			xml.lineNumber(),
			xml.columnNumber(),
			QObject::tr("The attribute value must not be empty when required.")
		};
	return value;
}

void drn::file_storage::internal::writeAttributeBoolean(
	QXmlStreamWriter& xml,
	const QLatin1String& attribute,
	const bool value
)
{
	xml.writeAttribute(attribute, value ? "true" : "false");
}

bool drn::file_storage::internal::readAttributeBoolean(
	QXmlStreamReader& xml,
	const QLatin1String& attribute
)
{
	const auto value{xml.attributes().value(attribute).toString()};

	if (value == "true")
		return true;

	if (value == "false")
		return false;
	throw AttributeError{
		xml.name().toString(),
		attribute,
		xml.errorString(),
		xml.lineNumber(),
		xml.columnNumber(),
		QObject::tr("The attribute value must 'true' or 'false'.")
	};
}

QDate drn::file_storage::internal::readAttributeDate(
	QXmlStreamReader& xml,
	const QLatin1String& attribute
)
{
	const auto dateStr{readAttributeString(xml, attribute)};
	const auto date{QDate::fromString(dateStr, Qt::ISODate)};

	if (date.isValid())
		return date;
	throw AttributeError{
		xml.name().toString(),
		attribute,
		xml.errorString(),
		xml.lineNumber(),
		xml.columnNumber(),
		QObject::tr("The read in attribute value, %1, is not a valid date.").arg(dateStr)
	};
}

void drn::file_storage::internal::writeAttributeDate(
	QXmlStreamWriter& xml,
	const QLatin1String& writeTag,
	const QLatin1String& attribute,
	const QDate& date
)
{
	if ( ! date.isValid())
		throw AttributeError{
			writeTag,
			attribute,
			{},
			{},
			{},
			QObject::tr("The attribute value to write, %1, is not a valid date.")
				.arg(date.toString())
		};
	const auto dateStr{date.toString(Qt::ISODate)};
	xml.writeAttribute(attribute, dateStr);
}
