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

#include "purplerequestfield.h"
#include "purplerequestfieldaccount.h"

struct _PurpleRequestFieldAccount {
	PurpleRequestField parent;

	PurpleAccount *account;
	GListModel *model;
};

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

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleRequestFieldAccount, purple_request_field_account,
                    PURPLE_TYPE_REQUEST_FIELD)

static void
purple_request_field_account_get_property(GObject *obj,
                                          guint param_id,
                                          GValue *value,
                                          GParamSpec *pspec)
{
	PurpleRequestFieldAccount *field = PURPLE_REQUEST_FIELD_ACCOUNT(obj);

	switch(param_id) {
	case PROP_ACCOUNT:
		g_value_set_object(value,
		                   purple_request_field_account_get_account(field));
		break;
	case PROP_MODEL:
		g_value_set_object(value,
		                   purple_request_field_account_get_model(field));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_request_field_account_set_property(GObject *obj,
                                          guint param_id,
                                          const GValue *value,
                                          GParamSpec *pspec)
{
	PurpleRequestFieldAccount *field = PURPLE_REQUEST_FIELD_ACCOUNT(obj);

	switch(param_id) {
	case PROP_ACCOUNT:
		purple_request_field_account_set_account(field,
		                                         g_value_get_object(value));
		break;
	case PROP_MODEL:
		purple_request_field_account_set_model(field,
		                                       g_value_get_object(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_request_field_account_finalize(GObject *obj) {
	PurpleRequestFieldAccount *field = PURPLE_REQUEST_FIELD_ACCOUNT(obj);

	g_clear_object(&field->account);
	g_clear_object(&field->model);

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

static void
purple_request_field_account_init(G_GNUC_UNUSED PurpleRequestFieldAccount *field)
{
}

static void
purple_request_field_account_class_init(PurpleRequestFieldAccountClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_request_field_account_finalize;
	obj_class->get_property = purple_request_field_account_get_property;
	obj_class->set_property = purple_request_field_account_set_property;

	/**
	 * PurpleRequestFieldAccount:account:
	 *
	 * The account of the field.
	 *
	 * Since: 3.0
	 */
	properties[PROP_ACCOUNT] = g_param_spec_object(
		"account", NULL, NULL,
		PURPLE_TYPE_ACCOUNT,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleRequestFieldAccount:model:
	 *
	 * A [iface@Gio.ListModel] of [class@Account]'s that should be presented
	 * as options.
	 *
	 * Since: 3.0
	 */
	properties[PROP_MODEL] = g_param_spec_object(
		"model", NULL, NULL,
		G_TYPE_LIST_MODEL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleAccount *
purple_request_field_account_get_account(PurpleRequestFieldAccount *field) {
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_ACCOUNT(field), NULL);

	return field->account;
}

GListModel *
purple_request_field_account_get_model(PurpleRequestFieldAccount *field) {
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_ACCOUNT(field), NULL);

	return field->model;
}

PurpleRequestField *
purple_request_field_account_new(const char *id,
                                 const char *label,
                                 GListModel *model)
{
	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_ACCOUNT,
	                    "id", id,
	                    "label", label,
	                    "model", model,
	                    NULL);
}

void
purple_request_field_account_set_account(PurpleRequestFieldAccount *field,
                                         PurpleAccount *account)
{
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_ACCOUNT(field));

	if(g_set_object(&field->account, account)) {
		GObject *obj = G_OBJECT(field);

		g_object_freeze_notify(obj);
		g_object_notify_by_pspec(obj, properties[PROP_ACCOUNT]);
		g_object_notify(obj, "valid");
		g_object_thaw_notify(obj);
	}
}

void
purple_request_field_account_set_model(PurpleRequestFieldAccount *field,
                                       GListModel *model)
{
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_ACCOUNT(field));

	if(g_set_object(&field->model, model)) {
		g_object_notify_by_pspec(G_OBJECT(field), properties[PROP_MODEL]);
	}
}
