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

#include "purpleaccountsettingstringlist.h"

struct _PurpleAccountSettingStringList {
	PurpleAccountSetting parent;

	char *active_item;
	GPtrArray *items;
};

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

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

static guint
purple_account_setting_string_list_get_n_items(GListModel *model) {
	PurpleAccountSettingStringList *list = NULL;

	list = PURPLE_ACCOUNT_SETTING_STRING_LIST(model);

	return list->items->len;
}

static gpointer
purple_account_setting_string_list_get_item(GListModel *model, guint position)
{
	PurpleAccountSettingStringList *list = NULL;
	BirbLocalizedString *str = NULL;

	list = PURPLE_ACCOUNT_SETTING_STRING_LIST(model);

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

	return str;
}

static void
purple_account_setting_string_list_list_model_iface_init(GListModelInterface *iface) {
	iface->get_item_type = purple_account_setting_string_list_get_item_type;
	iface->get_n_items = purple_account_setting_string_list_get_n_items;
	iface->get_item = purple_account_setting_string_list_get_item;
}


/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE_WITH_CODE(PurpleAccountSettingStringList,
                              purple_account_setting_string_list,
                              PURPLE_TYPE_ACCOUNT_SETTING,
                              G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL,
                                                    purple_account_setting_string_list_list_model_iface_init))

static void
purple_account_setting_string_list_finalize(GObject *obj) {
	PurpleAccountSettingStringList *list = NULL;

	list = PURPLE_ACCOUNT_SETTING_STRING_LIST(obj);

	g_clear_pointer(&list->active_item, g_free);
	g_clear_pointer(&list->items, g_ptr_array_unref);

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

static void
purple_account_setting_string_list_get_property(GObject *obj, guint param_id,
                                                GValue *value,
                                                GParamSpec *pspec)
{
	PurpleAccountSettingStringList *list = NULL;
	GListModel *model = NULL;

	list = PURPLE_ACCOUNT_SETTING_STRING_LIST(obj);
	model = G_LIST_MODEL(obj);

	switch(param_id) {
	case PROP_ACTIVE_ITEM:
		g_value_set_string(value,
		                   purple_account_setting_string_list_get_active_item(list));
		break;
	case PROP_ITEM_TYPE:
		g_value_set_gtype(value, g_list_model_get_item_type(model));
		break;
	case PROP_N_ITEMS:
		g_value_set_uint(value, g_list_model_get_n_items(model));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_account_setting_string_list_set_property(GObject *obj, guint param_id,
                                                const GValue *value,
                                                GParamSpec *pspec)
{
	PurpleAccountSettingStringList *list = NULL;

	list = PURPLE_ACCOUNT_SETTING_STRING_LIST(obj);

	switch(param_id) {
	case PROP_ACTIVE_ITEM:
		purple_account_setting_string_list_set_active_item(list,
		                                                   g_value_get_string(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_account_setting_string_list_init(PurpleAccountSettingStringList *list) {
	list->items = g_ptr_array_new_full(1, g_object_unref);
}

static void
purple_account_setting_string_list_class_init(PurpleAccountSettingStringListClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_account_setting_string_list_finalize;
	obj_class->get_property = purple_account_setting_string_list_get_property;
	obj_class->set_property = purple_account_setting_string_list_set_property;

	/**
	 * PurpleAccountSettingStringList:active-item:
	 *
	 * The id of the active item.
	 *
	 * Since: 3.0
	 */
	properties[PROP_ACTIVE_ITEM] = g_param_spec_string(
		"active-item", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

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

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

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
gboolean
purple_account_setting_string_list_add_item(PurpleAccountSettingStringList *list,
                                            const char *id,
                                            const char *label)
{
	BirbLocalizedString *str = NULL;

	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING_STRING_LIST(list), FALSE);

	/* Look for existing items. */
	for(guint i = 0; i < list->items->len; i++) {
		BirbLocalizedString *item = NULL;
		const char *item_id = NULL;

		item = g_ptr_array_index(list->items, i);
		item_id = birb_localized_string_get_id(item);

		if(birb_str_equal(item_id, id)) {
			return FALSE;
		}
	}

	/* There's no existing item with the id so add it. */
	str = birb_localized_string_new(id, label);
	g_ptr_array_add(list->items, str);

	g_list_model_items_changed(G_LIST_MODEL(list), list->items->len - 1, 0,
	                           1);

	return TRUE;
}

const char *
purple_account_setting_string_list_get_active_item(PurpleAccountSettingStringList *setting)
{
	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING_STRING_LIST(setting),
	                     NULL);

	return setting->active_item;
}

PurpleAccountSetting *
purple_account_setting_string_list_new(const char *id, const char *label) {
	return g_object_new(
		PURPLE_TYPE_ACCOUNT_SETTING_STRING_LIST,
		"id", id,
		"label", label,
		NULL);
}

void
purple_account_setting_string_list_set_active_item(PurpleAccountSettingStringList *list,
                                                   const char *id)
{
	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING_STRING_LIST(list));

	if(id == NULL) {
		if(g_set_str(&list->active_item, id)) {
			g_object_notify_by_pspec(G_OBJECT(list),
			                         properties[PROP_ACTIVE_ITEM]);
		}

		return;
	}

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

		str = g_ptr_array_index(list->items, i);
		item_id = birb_localized_string_get_id(str);

		if(birb_str_equal(item_id, id)) {
			if(g_set_str(&list->active_item, item_id)) {
				g_object_notify_by_pspec(G_OBJECT(list),
				                         properties[PROP_ACTIVE_ITEM]);
			}

			break;
		}
	}
}

