/*
 * 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 "purplerequestfieldchoice.h"

struct _PurpleRequestFieldChoice {
	PurpleRequestField parent;

	GPtrArray *items;
	guint selected;
};

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

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

static guint
purple_request_field_choice_get_n_items(GListModel *model) {
	PurpleRequestFieldChoice *field = PURPLE_REQUEST_FIELD_CHOICE(model);

	return field->items->len;
}

static gpointer
purple_request_field_choice_get_item(GListModel *model, guint index) {
	PurpleRequestFieldChoice *field = PURPLE_REQUEST_FIELD_CHOICE(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_choice_model_init(GListModelInterface *iface) {
	iface->get_item_type = purple_request_field_choice_get_item_type;
	iface->get_item = purple_request_field_choice_get_item;
	iface->get_n_items = purple_request_field_choice_get_n_items;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE_WITH_CODE(PurpleRequestFieldChoice,
                              purple_request_field_choice,
                              PURPLE_TYPE_REQUEST_FIELD,
                              G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL,
                                                    purple_request_field_choice_model_init))

static void
purple_request_field_choice_get_property(GObject *obj, guint param_id,
                                         GValue *value, GParamSpec *pspec)
{
	PurpleRequestFieldChoice *field = PURPLE_REQUEST_FIELD_CHOICE(obj);
	GListModel *model = G_LIST_MODEL(obj);

	switch(param_id) {
	case PROP_ITEM_TYPE:
		g_value_set_gtype(value,
		                  purple_request_field_choice_get_item_type(model));
		break;
	case PROP_N_ITEMS:
		g_value_set_uint(value,
		                 purple_request_field_choice_get_n_items(model));
		break;
	case PROP_SELECTED:
		g_value_set_uint(value,
		                 purple_request_field_choice_get_selected(field));
		break;
	case PROP_SELECTED_ITEM:
		g_value_set_object(value,
		                   purple_request_field_choice_get_selected_item(field));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_request_field_choice_set_property(GObject *obj, guint param_id,
                                         const GValue *value, GParamSpec *pspec)
{
	PurpleRequestFieldChoice *field = PURPLE_REQUEST_FIELD_CHOICE(obj);

	switch(param_id) {
	case PROP_SELECTED:
		purple_request_field_choice_set_selected(field,
		                                         g_value_get_uint(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_request_field_choice_finalize(GObject *obj) {
	PurpleRequestFieldChoice *field = PURPLE_REQUEST_FIELD_CHOICE(obj);

	g_clear_pointer(&field->items, g_ptr_array_unref);

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

static void
purple_request_field_choice_init(PurpleRequestFieldChoice *field) {
	field->items = g_ptr_array_new_full(1, g_object_unref);
}

static void
purple_request_field_choice_class_init(PurpleRequestFieldChoiceClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_request_field_choice_finalize;
	obj_class->get_property = purple_request_field_choice_get_property;
	obj_class->set_property = purple_request_field_choice_set_property;

	/**
	 * PurpleRequestFieldChoice: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);

	/**
	 * PurpleRequestFieldChoice: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);

	/**
	 * PurpleRequestFieldChoice:selected:
	 *
	 * The position of the currently selected item.
	 *
	 * Since: 3.0
	 */
	properties[PROP_SELECTED] = g_param_spec_uint(
		"selected", NULL, NULL,
		0, G_MAXUINT, 0,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleRequestFieldChoice:selected-item:
	 *
	 * The currently selected item.
	 *
	 * Since: 3.0
	 */
	properties[PROP_SELECTED_ITEM] = g_param_spec_object(
		"selected-item", NULL, NULL,
		BIRB_TYPE_LOCALIZED_STRING,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
void
purple_request_field_choice_add(PurpleRequestFieldChoice *field,
                                const char *id,
                                const char *label)
{
	BirbLocalizedString *item = NULL;

	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field));
	g_return_if_fail(!birb_str_is_empty(id));
	g_return_if_fail(!birb_str_is_empty(label));

	item = birb_localized_string_new(id, label);
	purple_request_field_choice_take(field, item);
}

void
purple_request_field_choice_add_item(PurpleRequestFieldChoice *field,
                                     BirbLocalizedString *item)
{
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field));
	g_return_if_fail(BIRB_IS_LOCALIZED_STRING(item));

	purple_request_field_choice_take(field, g_object_ref(item));
}

void
purple_request_field_choice_clear(PurpleRequestFieldChoice *field) {
	guint n_items = 0;

	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field));

	n_items = field->items->len;
	if(n_items > 0) {
		GObject *obj = G_OBJECT(field);

		field->selected = 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_freeze_notify(obj);
		g_object_notify_by_pspec(obj, properties[PROP_N_ITEMS]);
		g_object_notify_by_pspec(obj, properties[PROP_SELECTED]);
		g_object_notify_by_pspec(obj, properties[PROP_SELECTED_ITEM]);
		g_object_thaw_notify(obj);
	}
}

guint
purple_request_field_choice_get_selected(PurpleRequestFieldChoice *field) {
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field), G_MAXUINT);

	if(field->items->len == 0) {
		return G_MAXUINT;
	}

	return field->selected;
}

BirbLocalizedString *
purple_request_field_choice_get_selected_item(PurpleRequestFieldChoice *field)
{
	BirbLocalizedString *item = NULL;

	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field), NULL);

	if(field->items->len > 0 && field->selected < field->items->len) {
		item = g_ptr_array_index(field->items, field->selected);
	}

	return item;
}

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

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

gboolean
purple_request_field_choice_remove(PurpleRequestFieldChoice *field,
                                   guint position)
{
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field), FALSE);

	if(field->items->len > 0 && position < field->items->len) {
		GObject *obj = G_OBJECT(field);

		g_ptr_array_remove_index(field->items, position);

		g_list_model_items_changed(G_LIST_MODEL(field), position, 1, 0);

		g_object_freeze_notify(obj);
		g_object_notify_by_pspec(obj, properties[PROP_N_ITEMS]);

		if(position == field->selected) {
			field->selected = 0;
			g_object_notify_by_pspec(obj, properties[PROP_SELECTED]);
			g_object_notify_by_pspec(obj, properties[PROP_SELECTED_ITEM]);
		}

		g_object_thaw_notify(obj);

		return TRUE;
	}

	return FALSE;
}

gboolean
purple_request_field_choice_remove_by_id(PurpleRequestFieldChoice *field,
                                         const char *id)
{
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(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(candidate_id, id)) {
			return purple_request_field_choice_remove(field, i);
		}
	}

	return FALSE;
}

gboolean
purple_request_field_choice_remove_item(PurpleRequestFieldChoice *field,
                                        BirbLocalizedString *str)
{
	const char *id = NULL;

	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field), FALSE);
	g_return_val_if_fail(BIRB_IS_LOCALIZED_STRING(str), FALSE);

	id = birb_localized_string_get_id(str);

	return purple_request_field_choice_remove_by_id(field, id);
}

void
purple_request_field_choice_set_selected(PurpleRequestFieldChoice *field,
                                         guint selected)
{
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field));

	if(selected != field->selected && selected < field->items->len) {
		GObject *obj = G_OBJECT(field);

		field->selected = selected;

		g_object_freeze_notify(obj);
		g_object_notify_by_pspec(obj, properties[PROP_SELECTED]);
		g_object_notify_by_pspec(obj, properties[PROP_SELECTED_ITEM]);
		g_object_thaw_notify(obj);
	}
}

void
purple_request_field_choice_take(PurpleRequestFieldChoice *field,
                                 BirbLocalizedString *item)
{
	guint n_items = 0;

	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_CHOICE(field));
	g_return_if_fail(BIRB_IS_LOCALIZED_STRING(item));

	n_items = field->items->len;

	g_ptr_array_add(field->items, item);

	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]);
}
