/*
 * 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.
 */

#include <QFileInfo>

#include "src/datovka_shared/log/log.h"
#include "src/io/message_db_set_container.h"

DbContainer::DbContainer(bool onDisk, const QString &connectionPrefix)
    : QObject(Q_NULLPTR),
    QMap<AcntId, MessageDbSet *>(),
    m_onDisk(onDisk),
    m_connectionPrefix(connectionPrefix)
{
}

DbContainer::~DbContainer(void)
{
	QMap<AcntId, MessageDbSet *>::iterator i;

	for (i = this->begin(); i != this->end(); ++i) {
		MessageDbSet *dbSet = i.value();
		/* No need to call disconnect here. */
		//dbSet->disconnect(SLOT(opened(QString, bool)),
		//    this, SIGNAL(watchOpened(QString, bool)));
		delete dbSet;
	}
}

MessageDbSet *DbContainer::accessDbSet(const QString &locDir,
    const AcntId &acntId, MessageDbSet::Organisation organisation,
    enum MessageDbSet::CreationManner manner)
{
	MessageDbSet *dbSet = Q_NULLPTR;

	if (Q_UNLIKELY(!acntId.isValid())) {
		Q_ASSERT(0);
		return Q_NULLPTR;
	}

	/* Already opened. */
	if (this->find(acntId) != this->end()) {
		return (*this)[acntId];
	}

	dbSet = MessageDbSet::createNew(locDir, acntId.username(),
	    acntId.testing(), organisation, m_onDisk, m_connectionPrefix,
	    manner);
	if (Q_UNLIKELY(Q_NULLPTR == dbSet)) {
		return Q_NULLPTR;
	}

	connect(dbSet, SIGNAL(opened(QString, bool)),
	    this, SLOT(watchOpened(QString, bool)));

	this->insert(acntId, dbSet);
	return dbSet;
}

bool DbContainer::deleteDbSet(MessageDbSet *dbSet)
{
	if (Q_NULLPTR == dbSet) {
		Q_ASSERT(0);
		return false;
	}

	/* Find entry. */
	QMap<AcntId, MessageDbSet *>::iterator it = this->begin();
	while ((it != this->end()) && (it.value() != dbSet)) {
		++it;
	}
	/* Must exist. */
	if (this->end() == it) {
		Q_ASSERT(0);
		return false;
	}

	/* Remove from container. */
	this->erase(it);

	/* Delete files. */
	dbSet->deleteLocation();

	dbSet->disconnect(SLOT(opened(QString, bool)),
	    this, SIGNAL(watchOpened(QString, bool)));

	/* Close database. */
	delete dbSet;

	return true;
}

void DbContainer::watchOpened(const QString &primaryKey, bool testing)
{
	emit opened(AcntId(primaryKey, testing));
}

const QString DbContainer::dbDriverType("QSQLITE");
