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

typedef struct {
	char *id;
	char *label;

	char *hint;
	gboolean advanced;
	int weight;
} PurpleAccountSettingPrivate;

enum {
	PROP_0,
	PROP_ADVANCED,
	PROP_HINT,
	PROP_ID,
	PROP_LABEL,
	PROP_WEIGHT,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

enum {
	SIG_CHANGED,
	N_SIGNALS,
};
static guint signals[N_SIGNALS] = {0, };

G_DEFINE_TYPE_WITH_PRIVATE(PurpleAccountSetting, purple_account_setting,
                           G_TYPE_OBJECT)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
purple_account_setting_set_id(PurpleAccountSetting *setting, const char *id) {
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting));
	g_return_if_fail(!birb_str_is_empty(id));

	priv = purple_account_setting_get_instance_private(setting);
	if(g_set_str(&priv->id, id)) {
		g_object_notify_by_pspec(G_OBJECT(setting), properties[PROP_ID]);
	}
}

static void
purple_account_setting_set_label(PurpleAccountSetting *setting,
                                 const char *label)
{
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting));
	g_return_if_fail(!birb_str_is_empty(label));

	priv = purple_account_setting_get_instance_private(setting);
	if(g_set_str(&priv->label, label)) {
		g_object_notify_by_pspec(G_OBJECT(setting), properties[PROP_LABEL]);
	}
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_account_setting_finalize(GObject *obj) {
	PurpleAccountSetting *setting = PURPLE_ACCOUNT_SETTING(obj);
	PurpleAccountSettingPrivate *priv = NULL;

	priv = purple_account_setting_get_instance_private(setting);

	g_clear_pointer(&priv->id, g_free);
	g_clear_pointer(&priv->label, g_free);
	g_clear_pointer(&priv->hint, g_free);

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

static void
purple_account_setting_get_property(GObject *obj, guint param_id,
                                    GValue *value, GParamSpec *pspec)
{
	PurpleAccountSetting *setting = PURPLE_ACCOUNT_SETTING(obj);

	switch(param_id) {
	case PROP_ADVANCED:
		g_value_set_boolean(value,
		                    purple_account_setting_get_advanced(setting));
		break;
	case PROP_HINT:
		g_value_set_string(value, purple_account_setting_get_hint(setting));
		break;
	case PROP_ID:
		g_value_set_string(value, purple_account_setting_get_id(setting));
		break;
	case PROP_LABEL:
		g_value_set_string(value, purple_account_setting_get_label(setting));
		break;
	case PROP_WEIGHT:
		g_value_set_int(value, purple_account_setting_get_weight(setting));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_account_setting_set_property(GObject *obj, guint param_id,
                                    const GValue *value, GParamSpec *pspec)
{
	PurpleAccountSetting *setting = PURPLE_ACCOUNT_SETTING(obj);

	switch(param_id) {
	case PROP_ADVANCED:
		purple_account_setting_set_advanced(setting,
		                                    g_value_get_boolean(value));
		break;
	case PROP_HINT:
		purple_account_setting_set_hint(setting, g_value_get_string(value));
		break;
	case PROP_ID:
		purple_account_setting_set_id(setting, g_value_get_string(value));
		break;
	case PROP_LABEL:
		purple_account_setting_set_label(setting, g_value_get_string(value));
		break;
	case PROP_WEIGHT:
		purple_account_setting_set_weight(setting, g_value_get_int(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_account_setting_init(G_GNUC_UNUSED PurpleAccountSetting *setting) {
}

static void
purple_account_setting_class_init(PurpleAccountSettingClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_account_setting_finalize;
	obj_class->get_property = purple_account_setting_get_property;
	obj_class->set_property = purple_account_setting_set_property;

	/**
	 * PurpleAccountSetting:advanced:
	 *
	 * Whether or not the setting is an "advanced" setting.
	 *
	 * Advanced settings are usually hidden behind an advanced configuration
	 * page but that may vary across user interfaces.
	 *
	 * Since: 3.0
	 */
	properties[PROP_ADVANCED] = g_param_spec_boolean(
		"advanced", NULL, NULL,
		FALSE,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleAccountSetting:hint:
	 *
	 * The hint text for the setting.
	 *
	 * User interfaces can optionally use this to add additional context to the
	 * setting via something like a place holder or tool tip.
	 *
	 * Since: 3.0
	 */
	properties[PROP_HINT] = g_param_spec_string(
		"hint", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleAccountSetting:id:
	 *
	 * The identifier of the setting.
	 *
	 * This is used to read and write the setting.
	 *
	 * Since: 3.0
	 */
	properties[PROP_ID] = g_param_spec_string(
		"id", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS |
		G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleAccountSetting:label:
	 *
	 * The label that should be displayed to the user.
	 *
	 * Since: 3.0
	 */
	properties[PROP_LABEL] = g_param_spec_string(
		"label", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS |
		G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleAccountSetting:weight:
	 *
	 * The weight of the setting.
	 *
	 * User interfaces will use this to determine the order to present the
	 * settings going from lowest to highest.
	 *
	 * Since: 3.0
	 */
	properties[PROP_WEIGHT] = g_param_spec_int(
		"weight", NULL, NULL,
		G_MININT, G_MAXINT, 0,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	/**
	 * PurpleAccountSetting::changed:
	 *
	 * Emitted when the value of the setting has changed.
	 *
	 * Since: 3.0
	 */
	signals[SIG_CHANGED] = g_signal_new_class_handler(
		"changed",
		G_OBJECT_CLASS_TYPE(klass),
		G_SIGNAL_RUN_LAST,
		NULL,
		NULL,
		NULL,
		NULL,
		G_TYPE_NONE,
		0);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
void
purple_account_setting_changed(PurpleAccountSetting *setting) {
	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting));

	g_signal_emit(setting, signals[SIG_CHANGED], 0);
}

gboolean
purple_account_setting_get_advanced(PurpleAccountSetting *setting) {
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting), FALSE);

	priv = purple_account_setting_get_instance_private(setting);

	return priv->advanced;
}

const char *
purple_account_setting_get_id(PurpleAccountSetting *setting) {
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting), NULL);

	priv = purple_account_setting_get_instance_private(setting);

	return priv->id;
}

const char *
purple_account_setting_get_label(PurpleAccountSetting *setting) {
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting), NULL);

	priv = purple_account_setting_get_instance_private(setting);

	return priv->label;
}

const char *
purple_account_setting_get_hint(PurpleAccountSetting *setting) {
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting), NULL);

	priv = purple_account_setting_get_instance_private(setting);

	return priv->hint;
}

int
purple_account_setting_get_weight(PurpleAccountSetting *setting) {
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting), G_MININT);

	priv = purple_account_setting_get_instance_private(setting);

	return priv->weight;
}

void
purple_account_setting_set_advanced(PurpleAccountSetting *setting,
                                    gboolean advanced)
{
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting));

	priv = purple_account_setting_get_instance_private(setting);
	if(priv->advanced != advanced) {
		priv->advanced = advanced;

		g_object_notify_by_pspec(G_OBJECT(setting), properties[PROP_ADVANCED]);
	}
}

void
purple_account_setting_set_hint(PurpleAccountSetting *setting,
                                const char *hint)
{
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting));

	priv = purple_account_setting_get_instance_private(setting);
	if(g_set_str(&priv->hint, hint)) {
		g_object_notify_by_pspec(G_OBJECT(setting), properties[PROP_HINT]);
	}
}

void
purple_account_setting_set_weight(PurpleAccountSetting *setting, int weight) {
	PurpleAccountSettingPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING(setting));

	priv = purple_account_setting_get_instance_private(setting);
	if(priv->weight != weight) {
		priv->weight = weight;

		g_object_notify_by_pspec(G_OBJECT(setting), properties[PROP_WEIGHT]);
	}
}
