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

struct _PurpleRequestFieldImage {
	PurpleRequestField parent;

	PurpleImage *image;
};

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

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleRequestFieldImage, purple_request_field_image,
                    PURPLE_TYPE_REQUEST_FIELD)

static void
purple_request_field_image_get_property(GObject *obj, guint param_id,
                                        GValue *value, GParamSpec *pspec)
{
	PurpleRequestFieldImage *field = PURPLE_REQUEST_FIELD_IMAGE(obj);

	switch(param_id) {
	case PROP_IMAGE:
		g_value_set_object(value, purple_request_field_image_get_image(field));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_request_field_image_set_property(GObject *obj, guint param_id,
                                        const GValue *value, GParamSpec *pspec)
{
	PurpleRequestFieldImage *field = PURPLE_REQUEST_FIELD_IMAGE(obj);

	switch(param_id) {
	case PROP_IMAGE:
		purple_request_field_image_set_image(field, g_value_get_object(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_request_field_image_finalize(GObject *obj) {
	PurpleRequestFieldImage *field = PURPLE_REQUEST_FIELD_IMAGE(obj);

	g_clear_object(&field->image);

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

static void
purple_request_field_image_init(G_GNUC_UNUSED PurpleRequestFieldImage *field) {
}

static void
purple_request_field_image_class_init(PurpleRequestFieldImageClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_request_field_image_finalize;
	obj_class->get_property = purple_request_field_image_get_property;
	obj_class->set_property = purple_request_field_image_set_property;

	/**
	 * PurpleRequestFieldImage:image:
	 *
	 * The image for this field.
	 *
	 * Since: 3.0
	 */
	properties[PROP_IMAGE] = g_param_spec_object(
		"image", NULL, NULL,
		PURPLE_TYPE_IMAGE,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleImage *
purple_request_field_image_get_image(PurpleRequestFieldImage *image) {
	g_return_val_if_fail(PURPLE_IS_REQUEST_FIELD_IMAGE(image), NULL);

	return image->image;
}

PurpleRequestField *
purple_request_field_image_new(const char *id,
                               const char *label,
                               PurpleImage *image)
{
	g_return_val_if_fail(!birb_str_is_empty(id), NULL);

	return g_object_new(PURPLE_TYPE_REQUEST_FIELD_IMAGE,
	                    "id", id,
	                    "image", image,
	                    "label", label,
	                    NULL);
}

void
purple_request_field_image_set_image(PurpleRequestFieldImage *field,
                                     PurpleImage *image)
{
	g_return_if_fail(PURPLE_IS_REQUEST_FIELD_IMAGE(field));

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