/*
 * Copyright (C) 2014-2021 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.
 */

#include "src/common.h"
#include "src/datovka_shared/isds/error.h"
#include "src/datovka_shared/log/log.h"
#include "src/global.h"
#include "src/io/imports.h"
#include "src/io/isds_sessions.h"
#include "src/isds/initialisation.h"
#include "src/isds/services.h"
#include "src/isds/session.h"
#include "src/models/accounts_model.h"
#include "src/settings/prefs_specific.h"

IsdsSessions::IsdsSessions(void)
    : m_sessions()
{
	/* Initialise libdatovka. */
	Isds::init();
}

IsdsSessions::~IsdsSessions(void)
{
	/* Free all contexts. */
	foreach (Isds::Session *session, m_sessions) {
		delete session;
	}

	Isds::cleanup();
}

bool IsdsSessions::holdsSession(const QString &userName) const
{
	return Q_NULLPTR != m_sessions.value(userName, Q_NULLPTR);
}

Isds::Session *IsdsSessions::session(const QString &userName) const
{
	return m_sessions.value(userName, Q_NULLPTR);
}

bool IsdsSessions::isConnectedToIsds(const QString &userName)
{
	if (!holdsSession(userName)) {
		return false;
	}

	Isds::Error pingErr;

	setSessionTimeout(userName, ISDS_PING_TIMEOUT_MS);
	pingErr = Isds::Service::dummyOperation(session(userName));
	setSessionTimeout(userName,
	    PrefsSpecific::isdsDownloadTimeoutMs(*GlobInstcs::prefsPtr));

	return Isds::Type::ERR_SUCCESS == pingErr.code();
}

Isds::Session *IsdsSessions::createCleanSession(const QString &userName,
    unsigned int connectionTimeoutMs)
{
	/* User name should not exist. */
	if (Q_UNLIKELY(holdsSession(userName))) {
		Q_ASSERT(0);
		return Q_NULLPTR;
	}

	Isds::Session *session = Isds::Session::createSession(connectionTimeoutMs);
	if (Q_UNLIKELY(session == Q_NULLPTR)) {
		logErrorNL("Error creating ISDS session for user '%s'.",
		    userName.toUtf8().constData());
		return Q_NULLPTR;
	}

	m_sessions.insert(userName, session);
	return session;
}

bool IsdsSessions::setSessionTimeout(const QString &userName,
    unsigned int timeoutMs)
{
	Isds::Session *session = m_sessions.value(userName, Q_NULLPTR);
	if (Q_UNLIKELY(Q_NULLPTR == session)) {
		Q_ASSERT(0);
		return false;
	}

	bool ret = session->setTimeout(timeoutMs);
	if (Q_UNLIKELY(!ret)) {
		logErrorNL("Error setting time-out for user '%s'.",
		    userName.toUtf8().constData());
		return false;
	}

	return true;
}
