/*
 * Copyright (C) 2014-2019 CZ.NIC
 *
 * 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#pragma once

#include <QMap>
#include <QObject>
#include <QString>
#include <QStringList>

#include "src/datovka_shared/identifiers/account_id.h"

class QSettings; /* Forward declaration. */

/*!
 * It is not possible to mix templates wit Q_OBJECT,
 * see https://stackoverflow.com/a/19138963 .
 */
class AcntContainer : public QObject {
	Q_OBJECT
public:

signals:
	/*!
	 * @brief Notifies that account data have changed.
	 *
	 * @note Currently the signal must be triggered manually.
	 *
	 * @param[in] acntId Account identifier.
	 */
	void accountDataChanged(const AcntId &acntId);
};

/*!
 * @brief Associative array mapping a username to some settings.
 */
template <class T>
class AcntSettingsMap : public AcntContainer, public QMap<AcntId, T> {
public:
	/*!
	 * @brief Load data from supplied settings.
	 *
	 * @param[in] confDir Configuration directory path.
	 * @param[in] settings Settings structure to load data from.
	 */
	void loadFromSettings(const QString &confDir,
	    const QSettings &settings);

	/*!
	 * @brief Decrypts all encrypted passwords.
	 *
	 * @param[in] pinVal Pin value.
	 */
	void decryptAllPwds(const QString &pinVal);

	/*!
	 * @brief Returns the first found account identifier that matches the
	 *     supplied username.
	 *
	 * @note The usage if this method should be avoided because it ignores
	 *     the testing value.
	 *
	 * @param[in] username Username to search for.
	 * @return Matching account identifier of invalid instance if not found.
	 */
	AcntId acntIdFromUsername(const QString &username) const;

	/*!
	 * @brief return a (numerically) sorted list of credential groups.
	 *
	 * @param[in] settings Setting to read from.
	 * @return Numerically sorted credential groups.
	 */
	static
	QStringList sortedCredentialGroups(const QSettings &settings);

	/*!
	 * @brief Create credential group name.
	 *
	 * @param[in] num Group number (greater than 0).
	 * @return Credential group name.
	 */
	static
	QString credentialGroupName(int num);

	/*!
	 * @brief Add an account containing only a description and a username.
	 *
	 * @param[in] setting Setting to be modified.
	 * @param[in] accountName Account name.
	 * @param[in] acntId Account identifier.
	 */
	static
	void addCredentialsTorso(QSettings &settings,
	    const QString &accountName, const AcntId &acntId);
};
