/*******************************************************************************
***  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 "AccountNumber.h"
using ::quint32;
using ::quint8;
using drn::accounting::AccountNumber;

#include <QObject>
using ::QObject;
#include <QString>
using ::QString;

#include <ostream>
using std::ostream;
#include <utility>
using std::move;

#include <foundation/Error.h>
using drn::foundation::Error;


AccountNumber::AccountNumber() :
	value_{AccountNumber::invalid_}
{}

AccountNumber::AccountNumber(AccountNumber::ValueType value) :
	value_{move(value)}
{
	if (this->value_ == AccountNumber::invalid_)
		throw Error{QObject::tr("The account number cannot be initialised to be invalid.")};
}

const AccountNumber::ValueType& AccountNumber::integer() const
{
	return this->value_;
}

const AccountNumber::ValueType AccountNumber::invalid_{0u};
const uint8_t AccountNumber::fieldWidth_{4u};

bool drn::accounting::operator<(const AccountNumber& lhs, const AccountNumber& rhs)
{
	return lhs.integer() < rhs.integer();
}

bool drn::accounting::operator>(const AccountNumber& lhs, const AccountNumber& rhs)
{
	return lhs.integer() > rhs.integer();
}

bool drn::accounting::operator==(const AccountNumber& lhs, const AccountNumber& rhs)
{
	return lhs.integer() == rhs.integer();
}

bool drn::accounting::operator!=(const AccountNumber& lhs, const AccountNumber& rhs)
{
	return ! (lhs == rhs);
}

bool drn::accounting::operator==(const AccountNumber::ValueType& lhs, const AccountNumber& rhs)
{
	return lhs == rhs.integer();
}

bool drn::accounting::operator!=(const AccountNumber::ValueType& lhs, const AccountNumber& rhs)
{
	return ! (lhs == rhs);
}

ostream& drn::accounting::operator<<(ostream& o, const AccountNumber& an)
{
	return o << "Value (" << an.integer() << ')';
}

QString drn::accounting::presentationText(const AccountNumber& an)
{
	return QString{"%1"}.arg(an.integer(), AccountNumber::fieldWidth_, 10, QLatin1Char{'0'});
}
