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

struct _PurpleAccountSettingInt {
	PurpleAccountSetting parent;

	int value;
};

enum {
	PROP_0,
	PROP_VALUE,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleAccountSettingInt, purple_account_setting_int,
                    PURPLE_TYPE_ACCOUNT_SETTING)

static void
purple_account_setting_int_get_property(GObject *obj, guint param_id,
                                        GValue *value, GParamSpec *pspec)
{
	PurpleAccountSettingInt *setting = PURPLE_ACCOUNT_SETTING_INT(obj);

	switch(param_id) {
	case PROP_VALUE:
		g_value_set_int(value, purple_account_setting_int_get_value(setting));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_account_setting_int_set_property(GObject *obj, guint param_id,
                                        const GValue *value, GParamSpec *pspec)
{
	PurpleAccountSettingInt *setting = PURPLE_ACCOUNT_SETTING_INT(obj);

	switch(param_id) {
	case PROP_VALUE:
		purple_account_setting_int_set_value(setting, g_value_get_int(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_account_setting_int_init(G_GNUC_UNUSED PurpleAccountSettingInt *setting)
{
}

static void
purple_account_setting_int_class_init(PurpleAccountSettingIntClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->get_property = purple_account_setting_int_get_property;
	obj_class->set_property = purple_account_setting_int_set_property;

	/**
	 * PurpleAccountSettingInt:value:
	 *
	 * The integer value of the setting.
	 *
	 * Since: 3.0
	 */
	properties[PROP_VALUE] = g_param_spec_int(
		"value", 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);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
int
purple_account_setting_int_get_value(PurpleAccountSettingInt *setting) {
	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING_INT(setting), 0);

	return setting->value;
}

PurpleAccountSetting *
purple_account_setting_int_new(const char *id, const char *label,
                               int default_value)
{
	g_return_val_if_fail(!birb_str_is_empty(id), NULL);

	return g_object_new(
		PURPLE_TYPE_ACCOUNT_SETTING_INT,
		"id", id,
		"label", label,
		"value", default_value,
		NULL);
}

void
purple_account_setting_int_set_value(PurpleAccountSettingInt *setting,
                                     int value)
{
	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING_INT(setting));

	if(setting->value != value) {
		setting->value = value;

		g_object_notify_by_pspec(G_OBJECT(setting), properties[PROP_VALUE]);

		purple_account_setting_changed(PURPLE_ACCOUNT_SETTING(setting));
	}
}
