/*
 * Copyright (C) 2014-2026 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/models/datetime_filter_proxy_model.h"

DateTimeFilterProxyModel::DateTimeFilterProxyModel(QObject *parent)
    : QSortFilterProxyModel(parent),
    m_from(),
    m_to()
{
}

void DateTimeFilterProxyModel::setFilterDateTimeRange(const QDateTime &from,
    const QDateTime &to)
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 10, 0))
	beginFilterChange();
#endif /* >= Qt-6.10 */

	m_from = from;
	m_to = to;

#if (QT_VERSION >= QT_VERSION_CHECK(6, 10, 0))
	endFilterChange(QSortFilterProxyModel::Direction::Rows);
#elif (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
	invalidateRowsFilter();
#else /* < Qt-6.0 */
	invalidateFilter();
#endif /* >= Qt-6.10 */
}

bool DateTimeFilterProxyModel::filterAcceptsRow(int sourceRow,
    const QModelIndex &sourceParent) const
{
	/*
	 * Adapted from
	 * qtbase/src/corelib/itemmodels/qsortfilterproxymodel.cpp .
	 */

	if ((!m_from.isValid()) && (!m_to.isValid())) {
		return true;
	}

	if (-1 == filterKeyColumn()) {
		int columnCnt = columnCount();
		for (int column = 0; column < columnCnt; ++column) {
			QModelIndex sourceIndex(sourceModel()->index(sourceRow,
			    column, sourceParent));
			if (filterAcceptsItem(sourceIndex)) {
				return true;
			}
		}
		return false;
	}

	int column = filterKeyColumn();
	{
		QModelIndex sourceIndex(
		    sourceModel()->index(sourceRow, column, sourceParent));
		/* The column may not exist. */
		if (filterAcceptsItem(sourceIndex)) {
			return true;
		}
	}

	return false;
}

bool DateTimeFilterProxyModel::filterAcceptsItem(
    const QModelIndex &sourceIdx) const
{
	if (Q_UNLIKELY(!sourceIdx.isValid())) {
		return false;
	}

	const QVariant data(sourceModel()->data(sourceIdx, filterRole()));

	QDateTime dateTime;
	if (data.canConvert<QDateTime>()) {
		dateTime = data.toDateTime();
	}

	if (Q_UNLIKELY((m_from.isValid() || m_to.isValid()) && (!dateTime.isValid()))) {
		return false;
	}
	if (m_from.isValid() && dateTime.isValid() && (m_from > dateTime)) {
		return false;
	}
	if (m_to.isValid() && dateTime.isValid() && (dateTime >= m_to)) {
		return false;
	}
	return true;
}
