/*
 * Copyright (C) 2014-2022 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" /* dateTimeDisplayFormat */
#include "src/gui/icon_container.h"
#include "src/models/helper.h"
#include "src/models/message_found_model.h"

static IconContainer inconContainer; /* Local icon container. */

MsgFoundModel::MsgFoundModel(QObject *parent)
    : QAbstractTableModel(parent),
    m_entries()
{
}

int MsgFoundModel::rowCount(const QModelIndex &parent) const
{
	if (parent.isValid()) {
		return 0;
	} else {
		return m_entries.size();
	}
}

int MsgFoundModel::columnCount(const QModelIndex &parent) const
{
	if (parent.isValid()) {
		return 0;
	} else {
		return MAX_COLNUM;
	}
}

QVariant MsgFoundModel::data(const QModelIndex &index, int role) const
{
	const int row = index.row();
	const int col = index.column();
	if (Q_UNLIKELY((row < 0) || (row >= rowCount()))) {
		Q_ASSERT(0);
		return QVariant();
	}
	if (Q_UNLIKELY((col < 0) || (col >= columnCount()))) {
		Q_ASSERT(0);
		return QVariant();
	}

	switch (role) {
	case Qt::DisplayRole:
		switch (col) {
		case COL_ACCOUNT_ID:
			return m_entries.at(row).identif.acntIdDb.username();
			break;
		case COL_MESSAGE_ID:
			return m_entries.at(row).identif.msgId.dmId();
			break;
		case COL_ANNOTATION:
			return m_entries.at(row).dmAnnotation;
			break;
		case COL_SENDER:
			return m_entries.at(row).dmSender;
			break;
		case COL_RECIPIENT:
			return m_entries.at(row).dmRecipient;
			break;
		case COL_DELIVERY_TIME:
			{
				const QDateTime &deliveryTime =
				    m_entries.at(row).identif.msgId.deliveryTime();
				return deliveryTime.isValid() ?
				    deliveryTime.toString(dateTimeDisplayFormat) :
				    QString();
			}
			break;
		case COL_ACCEPT_TIME:
			{
				const QDateTime &acceptanceTime =
				    m_entries.at(row).dmAcceptanceTime;
				return acceptanceTime.isValid() ?
				    acceptanceTime.toString(dateTimeDisplayFormat) :
				    QString();
			}
			break;
		case COL_ATTACH_DOWNLOADED:
			/* Hide text. */
			return QVariant();
			break;
		default:
			Q_ASSERT(0);
			return QVariant();
			break;
		}
		break;

	case Qt::DecorationRole:
		if (col == COL_ATTACH_DOWNLOADED) {
			if (m_entries.at(row).identif.downloaded) {
				return inconContainer.icon(
				    IconContainer::ICON_ATTACHMENT);
			}
		}
		return QVariant();
		break;

	case Qt::ToolTipRole:
		if (col == COL_MESSAGE_ID) {
			return m_entries.at(row).msgDetail;
		}
		return QVariant();
		break;

	case Qt::AccessibleTextRole:
		switch (col) {
		case COL_ACCOUNT_ID:
			return tr("Account username") + QLatin1String(" ") +
			    data(index).toString();
			break;
		case COL_MESSAGE_ID:
		case COL_ANNOTATION:
		case COL_SENDER:
		case COL_RECIPIENT:
		case COL_DELIVERY_TIME:
		case COL_ACCEPT_TIME:
			return headerData(index.column(), Qt::Horizontal).toString() +
			    QLatin1String(" ") + data(index).toString();
			break;
		case COL_ATTACH_DOWNLOADED:
			if (m_entries.at(row).identif.downloaded) {
				return tr("attachments downloaded");
			} else {
				return tr("attachments not downloaded");
			}
			break;
		default:
			Q_ASSERT(0);
			return QVariant();
			break;
		};
		break;

	case ROLE_PROXYSORT:
		switch (col) {
		case COL_ACCOUNT_ID:
			return ModelHelper::sortRank(
			    m_entries.at(row).identif.acntIdDb.username(),
			    m_entries.at(row).identif.msgId.dmId());
			break;
		case COL_MESSAGE_ID:
			return m_entries.at(row).identif.msgId.dmId();
			break;
		case COL_ANNOTATION:
			return m_entries.at(row).dmAnnotation;
			break;
		case COL_SENDER:
			return m_entries.at(row).dmSender;
			break;
		case COL_RECIPIENT:
			return m_entries.at(row).dmRecipient;
			break;
		case COL_DELIVERY_TIME:
			{
				const QDateTime &deliveryTime =
				    m_entries.at(row).identif.msgId.deliveryTime();
				return deliveryTime.isValid() ?
				    deliveryTime.toString(dateTimeDisplayFormat) :
				    QString();
			}
			break;
		case COL_ACCEPT_TIME:
			{
				const QDateTime &acceptanceTime =
				    m_entries.at(row).dmAcceptanceTime;
				return acceptanceTime.isValid() ?
				    acceptanceTime.toString(dateTimeDisplayFormat) :
				    QString();
			}
			break;
		case COL_ATTACH_DOWNLOADED:
			return ModelHelper::sortRank(
			    m_entries.at(row).identif.downloaded ? 1 : 0,
			    m_entries.at(row).identif.msgId.dmId());
			break;
		default:
			Q_ASSERT(0);
			return QVariant();
			break;
		}
		break;

	default:
		return QVariant();
		break;
	}
}

QVariant MsgFoundModel::headerData(int section, Qt::Orientation orientation,
    int role) const
{
	Q_UNUSED(orientation);

	switch (role) {
	case Qt::DisplayRole:
		switch (section) {
		case COL_ACCOUNT_ID:
			return tr("Account");
			break;
		case COL_MESSAGE_ID:
			return tr("Message ID");
			break;
		case COL_ANNOTATION:
			return tr("Subject");
			break;
		case COL_SENDER:
			return tr("Sender");
			break;
		case COL_RECIPIENT:
			return tr("Recipient");
			break;
		case COL_DELIVERY_TIME:
			return tr("Delivery Time");
			break;
		case COL_ACCEPT_TIME:
			return tr("Acceptance Time");
			break;
		case COL_ATTACH_DOWNLOADED:
			return QVariant();
			break;
		default:
			Q_ASSERT(0);
			return QVariant();
			break;
		}
		break;

	case Qt::DecorationRole:
		if (section == COL_ATTACH_DOWNLOADED) {
			return inconContainer.icon(IconContainer::ICON_ATTACHMENT);
		}
		return QVariant();
		break;

	case Qt::ToolTipRole:
		if (section == COL_ATTACH_DOWNLOADED) {
			return tr("Attachments downloaded");
		}
		return QVariant();
		break;

	default:
		return QVariant();
		break;
	}
}

void MsgFoundModel::appendData(const AcntIdDb &acntIdDb,
    const QList<MessageDb::SoughtMsg> &entryList)
{
	if (Q_UNLIKELY(entryList.isEmpty())) {
		return;
	}

	beginInsertRows(QModelIndex(), rowCount(),
	    rowCount() + entryList.size() - 1);

	foreach (const MessageDb::SoughtMsg &entry, entryList) {
		Entry e(acntIdDb, entry.mId, entry.type, entry.isDownloaded,
		    entry.dmAnnotation, entry.dmSender, entry.dmRecipient,
		    entry.dmAcceptanceTime, QString());
		m_entries.append(e);
	}

	endInsertRows();
}

void MsgFoundModel::setMessageDetail(const AcntId &acntId, const MsgId &msgId,
    const QString &detail)
{
	int foundRow = -1;
	for (int row = 0; row < rowCount(); ++row) {
		const MsgIdentification &identif = m_entries.at(row).identif;
		if ((((AcntId)identif.acntIdDb) == acntId) &&
		    (identif.msgId.dmId() == msgId.dmId())) {
			foundRow = row;
			break;
		}
	}

	if (foundRow < 0) {
		return;
	}

	m_entries[foundRow].msgDetail = detail;

	emit dataChanged(index(foundRow, COL_MESSAGE_ID),
	    index(foundRow, COL_MESSAGE_ID), QVector<int>() << Qt::ToolTipRole);
}

void MsgFoundModel::removeAllRows(void)
{
	beginResetModel();
	m_entries.clear();

	endResetModel();
}

void MsgFoundModel::remove(const AcntId &acntId, const MsgId &msgId)
{
	int foundRow = findRow(acntId, msgId.dmId());
	if (foundRow < 0) {
		return;
	}

	beginRemoveRows(QModelIndex(), foundRow, foundRow);

	m_entries.removeAt(foundRow);

	endRemoveRows();
}

bool MsgFoundModel::overrideDownloaded(const AcntId &acntId, const MsgId &msgId,
   bool forceDownloaded)
{
	int foundRow = findRow(acntId, msgId.dmId());
	if (foundRow < 0) {
		return false;
	}

	m_entries[foundRow].identif.downloaded = forceDownloaded;

	const QModelIndex changedIdx = index(foundRow, COL_ATTACH_DOWNLOADED);
	emit dataChanged(changedIdx, changedIdx);

	return true;
}

const MsgFoundModel::MsgIdentification &MsgFoundModel::msgIdentification(
    int row) const
{
	static const MsgIdentification invalidIdentif;

	if (Q_UNLIKELY(row > rowCount())) {
		Q_ASSERT(0);
		return invalidIdentif;
	}

	return m_entries.at(row).identif;
}

int MsgFoundModel::findRow(const AcntId &acntId, qint64 dmId) const
{
	int foundRow = -1;
	for (int row = 0; row < rowCount(); ++row) {
		const MsgIdentification &identif = m_entries.at(row).identif;
		if ((((AcntId)identif.acntIdDb) == acntId) &&
		    (identif.msgId.dmId() == dmId)) {
			foundRow = row;
			break;
		}
	}
	return foundRow;
}
