/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT 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 <glib.h>

#include <birb.h>

#include <purple.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_request_field_list_new_normal(void) {
	PurpleRequestField *field = NULL;

	field = purple_request_field_list_new("id", "label");
	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_LIST);
	g_assert_true(G_IS_LIST_MODEL(field));

	g_assert_finalize_object(field);
}

static void
test_purple_request_field_list_properties(void) {
	PurpleRequestField *field = NULL;
	GType item_type = G_TYPE_INVALID;
	char *id = NULL;
	char *label = NULL;
	gboolean multi_select = FALSE;
	guint n_items = 0;

	field = g_object_new(PURPLE_TYPE_REQUEST_FIELD_LIST,
	                     "id", "naughty",
	                     "label", "Santa's Naughty List",
	                     "multi-select", TRUE,
	                     NULL);
	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_LIST);

	g_object_get(G_OBJECT(field),
	             "id", &id,
	             "item-type", &item_type,
	             "label", &label,
	             "multi-select", &multi_select,
	             "n-items", &n_items,
	             NULL);

	g_assert_cmpstr(id, ==, "naughty");
	g_clear_pointer(&id, g_free);

	g_assert_cmpuint(item_type, ==, BIRB_TYPE_LOCALIZED_STRING);

	g_assert_cmpstr(label, ==, "Santa's Naughty List");
	g_clear_pointer(&label, g_free);

	g_assert_true(multi_select);

	g_assert_cmpuint(n_items, ==, 0);

	g_assert_finalize_object(field);
}

static void
test_purple_request_field_list_add_remove(void) {
	PurpleRequestField *field = NULL;
	BirbLocalizedString *str = NULL;
	guint counter = 0;
	gboolean removed = FALSE;

	field = purple_request_field_list_new("naughty", "Santa's Naughty List");
	birb_count_list_model_items_changed(G_LIST_MODEL(field), &counter);

	/* Make sure the list is empty before we add any items. */
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 0);

	/* Add our first item. */
	counter = 0;
	str = birb_localized_string_new("grim", "Gary");
	purple_request_field_list_add_item(PURPLE_REQUEST_FIELD_LIST(field), str);
	g_clear_object(&str);
	g_assert_cmpuint(counter, ==, 1);
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 1);

	/* Add our second item. */
	counter = 0;
	str = birb_localized_string_new("pidgy", "Pidgy");
	purple_request_field_list_add_item(PURPLE_REQUEST_FIELD_LIST(field), str);
	g_clear_object(&str);
	g_assert_cmpuint(counter, ==, 1);
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 2);

	/* Add a duplicate. */
	counter = 0;
	purple_request_field_list_add(PURPLE_REQUEST_FIELD_LIST(field),
	                              "grim", "Gary");
	g_assert_cmpuint(counter, ==, 1);
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 3);

	/* Add another unique. */
	counter = 0;
	str = birb_localized_string_new("robotichead", "robotichead");
	purple_request_field_list_add_item(PURPLE_REQUEST_FIELD_LIST(field), str);
	g_clear_object(&str);
	g_assert_cmpuint(counter, ==, 1);
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 4);

	/* Remove the first grim that is found. */
	counter = 0;
	removed = purple_request_field_list_remove_by_id(PURPLE_REQUEST_FIELD_LIST(field),
	                                                 "grim");
	g_assert_true(removed);
	g_assert_cmpuint(counter, ==, 1);
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 3);

	/* Remove pidgy. */
	counter = 0;
	removed = purple_request_field_list_remove_by_id(PURPLE_REQUEST_FIELD_LIST(field),
	                                                 "pidgy");
	g_assert_true(removed);
	g_assert_cmpuint(counter, ==, 1);
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 2);

	/* Attempt to remove a non-existent item. */
	counter = 0;
	removed = purple_request_field_list_remove_by_id(PURPLE_REQUEST_FIELD_LIST(field),
	                                                 NULL);
	g_assert_false(removed);
	g_assert_cmpuint(counter, ==, 0);
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 2);

	/* Clear the remaining. We're removing two items, but the counter should
	 * only be incremented once.
	 */
	counter = 0;
	purple_request_field_list_clear(PURPLE_REQUEST_FIELD_LIST(field));
	g_assert_cmpuint(counter, ==, 1);
	birb_assert_list_model_n_items(G_LIST_MODEL(field), 0);

	/* Clean up */
	g_assert_finalize_object(field);
}

static void
test_purple_request_field_list_single_selection(void) {
	PurpleRequestField *field = NULL;
	GListModel *selected = NULL;
	guint counter = 0;
	gboolean newly_selected = FALSE;

	/* Create the list and add some items to it. */
	field = purple_request_field_list_new("testing", "Testing");
	purple_request_field_list_add(PURPLE_REQUEST_FIELD_LIST(field),
	                              "grim", "Grim");
	purple_request_field_list_add(PURPLE_REQUEST_FIELD_LIST(field),
	                              "pidgy", "Pidgy");
	purple_request_field_list_add(PURPLE_REQUEST_FIELD_LIST(field),
	                              "csb6", "csb6");

	/* Grab the selected and add our counter to it. */
	selected = purple_request_field_list_get_selected(PURPLE_REQUEST_FIELD_LIST(field));
	g_assert_true(G_IS_LIST_MODEL(selected));
	birb_count_list_model_items_changed(selected, &counter);

	/* Clear the selection and make sure nothing happens. */
	counter = 0;
	purple_request_field_list_clear_selected(PURPLE_REQUEST_FIELD_LIST(field));
	birb_assert_list_model_n_items(selected, 0);
	g_assert_cmpuint(counter, ==, 0);

	/* Add an item to the selection and verify it. */
	counter = 0;
	newly_selected = purple_request_field_list_select_item(PURPLE_REQUEST_FIELD_LIST(field),
	                                                       "grim");
	g_assert_true(newly_selected);
	birb_assert_list_model_n_items(selected, 1);
	g_assert_cmpuint(counter, ==, 1);

	/* Try to select the item again. */
	counter = 0;
	newly_selected = purple_request_field_list_select_item(PURPLE_REQUEST_FIELD_LIST(field),
	                                                       "grim");
	g_assert_false(newly_selected);
	birb_assert_list_model_n_items(selected, 1);
	g_assert_cmpuint(counter, ==, 0);

	/* Select another item. */
	counter = 0;
	newly_selected = purple_request_field_list_select_item(PURPLE_REQUEST_FIELD_LIST(field),
	                                                       "csb6");
	g_assert_true(newly_selected);
	birb_assert_list_model_n_items(selected, 1);
	/* Counter is 2 because the previously selected item was removed separately
	 * from the newly selected item.
	 */
	g_assert_cmpuint(counter, ==, 2);

	/* Clear everything. */
	counter = 0;
	purple_request_field_list_clear_selected(PURPLE_REQUEST_FIELD_LIST(field));
	birb_assert_list_model_n_items(selected, 0);
	g_assert_cmpuint(counter, ==, 1);

	/* Clean up. */
	g_assert_finalize_object(field);
}

static void
test_purple_request_field_list_multi_selection(void) {
	PurpleRequestField *field = NULL;
	GListModel *selected = NULL;
	guint counter = 0;
	gboolean newly_selected = FALSE;

	/* Create the list and add some items to it. */
	field = purple_request_field_list_new("testing", "Testing");
	purple_request_field_list_set_multi_select(PURPLE_REQUEST_FIELD_LIST(field),
	                                           TRUE);
	purple_request_field_list_add(PURPLE_REQUEST_FIELD_LIST(field),
	                              "grim", "Grim");
	purple_request_field_list_add(PURPLE_REQUEST_FIELD_LIST(field),
	                              "pidgy", "Pidgy");
	purple_request_field_list_add(PURPLE_REQUEST_FIELD_LIST(field),
	                              "csb6", "csb6");

	/* Grab the selected and add our counter to it. */
	selected = purple_request_field_list_get_selected(PURPLE_REQUEST_FIELD_LIST(field));
	g_assert_true(G_IS_LIST_MODEL(selected));
	birb_count_list_model_items_changed(selected, &counter);

	/* Clear the selection and make sure nothing happens. */
	counter = 0;
	purple_request_field_list_clear_selected(PURPLE_REQUEST_FIELD_LIST(field));
	birb_assert_list_model_n_items(selected, 0);
	g_assert_cmpuint(counter, ==, 0);

	/* Add an item to the selection and verify it. */
	counter = 0;
	newly_selected = purple_request_field_list_select_item(PURPLE_REQUEST_FIELD_LIST(field),
	                                                       "grim");
	g_assert_true(newly_selected);
	birb_assert_list_model_n_items(selected, 1);
	g_assert_cmpuint(counter, ==, 1);

	/* Try to select the item again. */
	counter = 0;
	newly_selected = purple_request_field_list_select_item(PURPLE_REQUEST_FIELD_LIST(field),
	                                                       "grim");
	g_assert_false(newly_selected);
	birb_assert_list_model_n_items(selected, 1);
	g_assert_cmpuint(counter, ==, 0);

	/* Select another item. */
	counter = 0;
	newly_selected = purple_request_field_list_select_item(PURPLE_REQUEST_FIELD_LIST(field),
	                                                       "csb6");
	g_assert_true(newly_selected);
	birb_assert_list_model_n_items(selected, 2);
	g_assert_cmpuint(counter, ==, 1);

	/* Clear everything. */
	counter = 0;
	purple_request_field_list_clear_selected(PURPLE_REQUEST_FIELD_LIST(field));
	birb_assert_list_model_n_items(selected, 0);
	g_assert_cmpuint(counter, ==, 1);

	/* Clean up. */
	g_assert_finalize_object(field);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
int
main(int argc, char **argv) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/request/field/list/new/normal",
	                test_purple_request_field_list_new_normal);
	g_test_add_func("/request/field/list/properties",
	                test_purple_request_field_list_properties);

	g_test_add_func("/request/field/list/add-remove",
	                test_purple_request_field_list_add_remove);

	g_test_add_func("/request/field/list/single-selection",
	                test_purple_request_field_list_single_selection);
	g_test_add_func("/request/field/list/multi-selection",
	                test_purple_request_field_list_multi_selection);

	return g_test_run();
}

