/*
 * Copyright (C) 2024 Birb Developers
 *
 * Birb is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the AUTHORS file distributed
 * with this source distribution
 *
 * 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 2 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 <https://www.gnu.org/licenses/>.
 */

#include "birbcustomfilter.h"

struct _BirbCustomFilter {
	BirbFilter parent;

	BirbCustomFilterFunc match_func;
	gpointer data;
	GDestroyNotify destroy_notify;
};

/******************************************************************************
 * BirbFilter Implementation
 *****************************************************************************/
static gboolean
birb_custom_filter_matches(BirbFilter *filter, GObject *item) {
	BirbCustomFilter *custom = BIRB_CUSTOM_FILTER(filter);

	if(custom->match_func == NULL) {
		return TRUE;
	}

	return custom->match_func(item, custom->data);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_TYPE(BirbCustomFilter, birb_custom_filter, BIRB_TYPE_FILTER)

static void
birb_custom_filter_dispose(GObject *obj) {
	BirbCustomFilter *filter = BIRB_CUSTOM_FILTER(obj);

	if(filter->destroy_notify != NULL) {
		filter->destroy_notify(filter->data);
	}

	G_OBJECT_CLASS(birb_custom_filter_parent_class)->dispose(obj);
}

static void
birb_custom_filter_init(G_GNUC_UNUSED BirbCustomFilter *filter) {
}

static void
birb_custom_filter_class_init(BirbCustomFilterClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	BirbFilterClass *filter_class = BIRB_FILTER_CLASS(klass);

	obj_class->dispose = birb_custom_filter_dispose;

	filter_class->matches = birb_custom_filter_matches;
}

/******************************************************************************
 * Public API
 *****************************************************************************/
BirbCustomFilter *
birb_custom_filter_new(BirbCustomFilterFunc match_func, gpointer data,
                       GDestroyNotify destroy_notify)
{
	BirbCustomFilter *filter = g_object_new(BIRB_TYPE_CUSTOM_FILTER, NULL);

	birb_custom_filter_set_filter_func(filter, match_func, data,
	                                   destroy_notify);

	return filter;
}

void
birb_custom_filter_set_filter_func(BirbCustomFilter *filter,
                                   BirbCustomFilterFunc match_func,
                                   gpointer data,
                                   GDestroyNotify destroy_notify)
{
	g_return_if_fail(BIRB_IS_CUSTOM_FILTER(filter));

	if(filter->destroy_notify != NULL) {
		filter->destroy_notify(filter->data);
	}

	filter->match_func = match_func;
	filter->data = data;
	filter->destroy_notify = destroy_notify;

	birb_filter_changed(BIRB_FILTER(filter));
}
