/*
 * 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 library 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 library 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 library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib/gi18n-lib.h>

#include "purplerequestfield.h"
#include "purplerequestfieldlist.h"
#include "purplekeyvaluepair.h"

struct _PurpleRequestFieldList {
	PurpleRequestField parent;

	GPtrArray *items;

	gboolean multi_select;
	GListStore *selected;
};

enum {
	PROP_0,
	PROP_ITEM_TYPE,
	PROP_MULTI_SELECT,
	PROP_N_ITEMS,
	PROP_SELECTED,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

/******************************************************************************
 * GListModel Implementation
 *****************************************************************************/
static GType
purple_request_field_list_get_item_type(G_GNUC_UNUSED GListModel *model) {
	return BIRB_TYPE_LOCALIZED_STRING;
}

static guint
purple_request_field_list_get_n_items(GListModel *model) {
	PurpleRequestFieldList *field = PURPLE_REQUEST_FIELD_LIST(model);

	return field->items->len;
}

static gpointer
purple_request_field_list_get_item(GListModel *model, guint index) {
	PurpleRequestFieldList *field = PURPLE_REQUEST_FIELD_LIST(model);
	BirbLocalizedString *str = NULL;

	if(index < field->items->len) {
		str = g_ptr_array_index(field->items, index);
		g_object_ref(str);
	}

	return str;
}

static void
purple_request_field_list_model_init(GListModelInterface *iface) {
	iface->get_item_type = purple_request_field_list_get_item_type;
	iface->get_item = purple_request_field_list_get_item;
	iface->get_n_items = purple_request_field_list_get_n_items;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE_WITH_CODE(PurpleRequestFieldList,
                              purple_request_field_list,
                              PURPLE_TYPE_REQUEST_FIELD,
                              G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL,
                                                    purple_request_field_list_model_init))

static void
purple_request_field_list_get_property(GObject *obj,
                                       guint param_id,
                                       GValue *value,
                                       GParamSpec *pspec)
{
	PurpleRequestFieldList *field = PURPLE_REQUEST_FIELD_LIST(obj);
	GListModel *model = G_LIST_MODEL(field);

	switch(param_id) {
	case PROP_ITEM_TYPE:
		g_value_set_gtype(value,
		                  purple_request_field_list_get_item_type(model));
		break;
	case PROP_MULTI_SELECT:
		g_value_set_boolean(value,
		                    purple_request_field_list_get_multi_select(field));
		break;
	case PROP_N_ITEMS:
		g_value_set_uint(value, purple_request_field_list_get_n_items(model));
		break;
	case PROP_SELECTED:
		g_value_set_object(value,
		                   purple_request_field_list_get_selected(field));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_request_field_list_set_property(GObject *obj,
                                       guint param_id,
                                       const GValue *value,
                                       GParamSpec *pspec)
{
	PurpleRequestFieldList *field = PURPLE_REQUEST_FIELD_LIST(obj);

	switch(param_id) {
	case PROP_MULTI_SELECT:
		purple_request_field_list_set_multi_select(field,
		                                           g_value_get_boolean(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_request_field_list_finalize(GObject *obj) {
	PurpleRequestFieldList *field = PURPLE_REQUEST_FIELD_LIST(obj);

	g_clear_pointer(&field->items, g_ptr_array_unref);
	g_clear_object(&field->selected);

	G_OBJECT_CLASS(purple_request_field_list_parent_class)->finalize(obj);
}

static void
purple_request_field_list_init(PurpleRequestFieldList *field) {
	field->items = g_ptr_array_new_full(1, g_object_unref);
	field->selected = g_list_store_new(BIRB_TYPE_LOCALIZED_STRING);
}

static void
purple_request_field_list_class_init(PurpleRequestFieldListClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_request_field_list_finalize;
	obj_class->get_property = purple_request_field_list_get_property;
	obj_class->set_property = purple_request_field_list_set_property;

	/**
	 * PurpleRequestFieldList:item-type:
	 *
	 * The type of items. See [vfunc@Gio.ListModel.get_item_type].
	 *
	 * Since: 3.0
	 */
	properties[PROP_ITEM_TYPE] = g_param_spec_gtype(
		"item-type", NULL, NULL,
		G_TYPE_OBJECT,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleRequestFieldList:multi-select:
	 *
	 * Whether the field should allow multiple selections.
	 *
	 * Since: 3.0
	 */
	properties[PROP_MULTI_SELECT] = g_param_spec_boolean(
		"multi-select", NULL, NULL,
		FALSE,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleRequestFieldList:n-items:
	 *
	 * The number of items. See [vfunc@Gio.ListModel.get_n_items].
	 *
	 * Since: 3.0
	 */
	properties[PROP_N_ITEMS] = g_param_spec_uint(
		"n-items", NULL, NULL,
		0, G_MAXUINT, 0,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleRequestFieldList:selected:
	 *
	 * The items that are selected.
	 *
	 * Since: 3.0
	 */
	properties[PROP_SELECTED] = g_param_spec_object(
		"selected", NULL, NULL,
		G_TYPE_LIST_MODEL,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
void
purple_request_field_list_add(PurpleRequestFieldList *field,
                                   const char *id,
                                   const char *label)
{
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field));

	purple_request_field_list_take(field,
	                               birb_localized_string_new(id, label));
}

void
purple_request_field_list_add_item(PurpleRequestFieldList *field,
                                   BirbLocalizedString *str)
{
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field));
	g_return_if_fail(BIRB_IS_LOCALIZED_STRING(str));

	purple_request_field_list_take(field, g_object_ref(str));
}

void
purple_request_field_list_clear(PurpleRequestFieldList *field) {
	guint n_items = 0;

	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field));

	purple_request_field_list_clear_selected(field);

	n_items = field->items->len;
	if(n_items > 0) {
		g_ptr_array_remove_range(field->items, 0, n_items);
		g_list_model_items_changed(G_LIST_MODEL(field), 0, n_items, 0);
		g_object_notify_by_pspec(G_OBJECT(field), properties[PROP_N_ITEMS]);
	}
}

void
purple_request_field_list_clear_selected(PurpleRequestFieldList *field) {
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field));

	/* g_list_store_remove_all always calls g_list_model_items_changed. */
	if(g_list_model_get_n_items(G_LIST_MODEL(field->selected)) > 0) {
		g_list_store_remove_all(field->selected);
	}
}

gboolean
purple_request_field_list_get_multi_select(PurpleRequestFieldList *field) {
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field), FALSE);

	return field->multi_select;
}

GListModel *
purple_request_field_list_get_selected(PurpleRequestFieldList *field) {
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field), NULL);

	return G_LIST_MODEL(field->selected);
}

PurpleRequestField *
purple_request_field_list_new(const char *id, const char *label) {
	g_return_val_if_fail(!birb_str_is_empty(id), NULL);

	return g_object_new(PURPLE_TYPE_REQUEST_FIELD_LIST,
	                    "id", id,
	                    "label", label,
	                    NULL);
}

gboolean
purple_request_field_list_remove_by_id(PurpleRequestFieldList *field,
                                       const char *id)
{
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field), FALSE);

	for(guint i = 0; i < field->items->len; i++) {
		BirbLocalizedString *str = NULL;
		const char *candidate_id = NULL;

		str = g_ptr_array_index(field->items, i);
		candidate_id = birb_localized_string_get_id(str);

		if(birb_str_equal(id, candidate_id)) {
			g_ptr_array_remove_index(field->items, i);

			g_list_model_items_changed(G_LIST_MODEL(field), i, 1, 0);
			g_object_notify_by_pspec(G_OBJECT(field),
			                         properties[PROP_N_ITEMS]);

			return TRUE;
		}
	}

	return FALSE;
}

gboolean
purple_request_field_list_select_item(PurpleRequestFieldList *field,
                                      const char *id)
{
	guint n_items = 0;

	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field), FALSE);

	n_items = g_list_model_get_n_items(G_LIST_MODEL(field->selected));

	/* First check if the item is already selected. */
	for(guint i = 0; i < n_items; i++) {
		BirbLocalizedString *str = NULL;
		const char *candidate_id = NULL;

		str = g_list_model_get_item(G_LIST_MODEL(field->selected), i);
		candidate_id = birb_localized_string_get_id(str);
		if(birb_str_equal(candidate_id, id)) {
			g_clear_object(&str);

			return FALSE;
		}

		g_clear_object(&str);
	}

	/* If this is single selection list, we need to clear the selection. */
	if(!field->multi_select && n_items > 0) {
		g_list_store_remove_all(field->selected);
	}

	/* See if the items exists and if so select it!. */
	for(guint i = 0; i < field->items->len; i++) {
		BirbLocalizedString *str = NULL;
		const char *candidate_id = NULL;

		str = g_ptr_array_index(field->items, i);
		candidate_id = birb_localized_string_get_id(str);
		if(birb_str_equal(candidate_id, id)) {
			g_list_store_append(field->selected, str);

			return TRUE;
		}
	}

	return FALSE;
}

void
purple_request_field_list_set_multi_select(PurpleRequestFieldList *field,
                                           gboolean multi_select)
{
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field));

	if(field->multi_select != multi_select) {
		field->multi_select = multi_select;
		g_object_notify_by_pspec(G_OBJECT(field),
		                         properties[PROP_MULTI_SELECT]);
	}
}

void
purple_request_field_list_take(PurpleRequestFieldList *field,
                               BirbLocalizedString *str)
{
	guint n_items = 0;

	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_LIST(field));
	g_return_if_fail(BIRB_IS_LOCALIZED_STRING(str));

	n_items = field->items->len;

	g_ptr_array_add(field->items, str);

	g_list_model_items_changed(G_LIST_MODEL(field), n_items, 0, 1);
	g_object_notify_by_pspec(G_OBJECT(field), properties[PROP_N_ITEMS]);
}

