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

#include "purpleprotocol.h"
#include "purpleprotocolwhiteboard.h"

typedef struct {
	int state;

	PurpleAccount *account;
	char *id;

	GList *draw_list;
} PurpleWhiteboardPrivate;

/* GObject Property enums */
enum {
	PROP_0,
	PROP_STATE,
	PROP_ACCOUNT,
	PROP_ID,
	PROP_DRAW_LIST,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

G_DEFINE_TYPE_WITH_PRIVATE(PurpleWhiteboard, purple_whiteboard, G_TYPE_OBJECT)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
purple_whiteboard_set_account(PurpleWhiteboard *whiteboard,
                              PurpleAccount *account)
{
	PurpleWhiteboardPrivate *priv = NULL;

	priv = purple_whiteboard_get_instance_private(whiteboard);

	if(g_set_object(&priv->account, account)) {
		g_object_notify_by_pspec(G_OBJECT(whiteboard),
		                         properties[PROP_ACCOUNT]);
	}
}

static void
purple_whiteboard_set_id(PurpleWhiteboard *whiteboard, const char *id) {
	PurpleWhiteboardPrivate *priv = NULL;

	priv = purple_whiteboard_get_instance_private(whiteboard);

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

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_whiteboard_set_property(GObject *obj, guint param_id,
                               const GValue *value, GParamSpec *pspec)
{
	PurpleWhiteboard *whiteboard = PURPLE_WHITEBOARD(obj);

	switch(param_id) {
	case PROP_STATE:
		purple_whiteboard_set_state(whiteboard, g_value_get_int(value));
		break;
	case PROP_ACCOUNT:
		purple_whiteboard_set_account(whiteboard, g_value_get_object(value));
		break;
	case PROP_ID:
		purple_whiteboard_set_id(whiteboard, g_value_get_string(value));
		break;
	case PROP_DRAW_LIST:
		purple_whiteboard_set_draw_list(whiteboard,
		                                g_value_get_pointer(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_whiteboard_get_property(GObject *obj, guint param_id, GValue *value,
                               GParamSpec *pspec)
{
	PurpleWhiteboard *whiteboard = PURPLE_WHITEBOARD(obj);

	switch (param_id) {
	case PROP_STATE:
		g_value_set_int(value, purple_whiteboard_get_state(whiteboard));
		break;
	case PROP_ACCOUNT:
		g_value_set_object(value, purple_whiteboard_get_account(whiteboard));
		break;
	case PROP_ID:
		g_value_set_string(value, purple_whiteboard_get_id(whiteboard));
		break;
	case PROP_DRAW_LIST:
		g_value_set_pointer(value,
		                    purple_whiteboard_get_draw_list(whiteboard));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_whiteboard_init(G_GNUC_UNUSED PurpleWhiteboard *whiteboard) {
}

static void
purple_whiteboard_finalize(GObject *object) {
	PurpleWhiteboard *whiteboard = PURPLE_WHITEBOARD(object);
	PurpleWhiteboardPrivate *priv = NULL;

	priv = purple_whiteboard_get_instance_private(whiteboard);

	g_clear_object(&priv->account);
	g_clear_pointer(&priv->id, g_free);

	G_OBJECT_CLASS(purple_whiteboard_parent_class)->finalize(object);
}

static void
purple_whiteboard_class_init(PurpleWhiteboardClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->get_property = purple_whiteboard_get_property;
	obj_class->set_property = purple_whiteboard_set_property;
	obj_class->finalize = purple_whiteboard_finalize;

	/**
	 * PurpleWhiteboard:state:
	 *
	 * The state of the whiteboard.
	 *
	 * Since: 3.0
	 */
	properties[PROP_STATE] = g_param_spec_int(
		"state", NULL, NULL,
		G_MININT, G_MAXINT, 0,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleWhiteboard:account:
	 *
	 * The account this whiteboard belongs to.
	 *
	 * Since: 3.0
	 */
	properties[PROP_ACCOUNT] = g_param_spec_object(
		"account", NULL, NULL,
		PURPLE_TYPE_ACCOUNT,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleWhiteboard:id:
	 *
	 * The unique identifier of the whiteboard.
	 *
	 * 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);

	/**
	 * PurpleWhiteboard:draw-list:
	 *
	 * The list of draw actions for the whiteboard.
	 *
	 * Since: 3.0
	 */
	properties[PROP_DRAW_LIST] = g_param_spec_pointer(
		"draw-list", NULL, NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * API
 *****************************************************************************/
PurpleAccount *
purple_whiteboard_get_account(PurpleWhiteboard *whiteboard) {
	PurpleWhiteboardPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_WHITEBOARD(whiteboard), NULL);

	priv = purple_whiteboard_get_instance_private(whiteboard);

	return priv->account;
}

const char *
purple_whiteboard_get_id(PurpleWhiteboard *whiteboard) {
	PurpleWhiteboardPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_WHITEBOARD(whiteboard), NULL);

	priv = purple_whiteboard_get_instance_private(whiteboard);

	return priv->id;
}

void
purple_whiteboard_set_state(PurpleWhiteboard *whiteboard, int state) {
	PurpleWhiteboardPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	priv = purple_whiteboard_get_instance_private(whiteboard);

	priv->state = state;

	g_object_notify_by_pspec(G_OBJECT(whiteboard), properties[PROP_STATE]);
}

int
purple_whiteboard_get_state(PurpleWhiteboard *whiteboard) {
	PurpleWhiteboardPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_WHITEBOARD(whiteboard), -1);

	priv = purple_whiteboard_get_instance_private(whiteboard);

	return priv->state;
}

void
purple_whiteboard_start(PurpleWhiteboard *whiteboard) {
	PurpleWhiteboardClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->start != NULL) {
		klass->start(whiteboard);
	}
}

void
purple_whiteboard_draw_list_destroy(GList *draw_list) {
	g_list_free(draw_list);
}

gboolean
purple_whiteboard_get_dimensions(PurpleWhiteboard *whiteboard, int *width,
                                 int *height)
{
	PurpleWhiteboardClass *klass = NULL;

	g_return_val_if_fail(PURPLE_IS_WHITEBOARD(whiteboard), FALSE);

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->get_dimensions != NULL) {
		return klass->get_dimensions(whiteboard, width, height);
	}

	return FALSE;
}

void
purple_whiteboard_set_dimensions(PurpleWhiteboard *whiteboard, int width,
                                 int height)
{
	PurpleWhiteboardClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->set_dimensions != NULL) {
		klass->set_dimensions(whiteboard, width, height);
	}
}

void
purple_whiteboard_send_draw_list(PurpleWhiteboard *whiteboard, GList *list) {
	PurpleWhiteboardClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->send_draw_list != NULL) {
		klass->send_draw_list(whiteboard, list);
	}
}

void
purple_whiteboard_draw_point(PurpleWhiteboard *whiteboard, int x, int y,
                             int color, int size)
{
	PurpleWhiteboardClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->draw_point != NULL) {
		klass->draw_point(whiteboard, x, y, color, size);
	}
}

void
purple_whiteboard_draw_line(PurpleWhiteboard *whiteboard, int x1, int y1,
                            int x2, int y2, int color, int size)
{
	PurpleWhiteboardClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->draw_line != NULL) {
		klass->draw_line(whiteboard, x1, y1, x2, y2, color, size);
	}
}

void
purple_whiteboard_clear(PurpleWhiteboard *whiteboard) {
	PurpleWhiteboardClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->clear != NULL) {
		klass->clear(whiteboard);
	}
}

gboolean
purple_whiteboard_get_brush(PurpleWhiteboard *whiteboard, int *size,
                            int *color)
{
	PurpleWhiteboardClass *klass = NULL;

	g_return_val_if_fail(PURPLE_IS_WHITEBOARD(whiteboard), FALSE);

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->get_brush != NULL) {
		klass->get_brush(whiteboard, size, color);
	}

	return FALSE;
}

void
purple_whiteboard_set_brush(PurpleWhiteboard *whiteboard, int size, int color)
{
	PurpleWhiteboardClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	klass = PURPLE_WHITEBOARD_GET_CLASS(whiteboard);
	if(klass != NULL && klass->set_brush != NULL) {
		klass->set_brush(whiteboard, size, color);
	}
}

GList *
purple_whiteboard_get_draw_list(PurpleWhiteboard *whiteboard) {
	PurpleWhiteboardPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_WHITEBOARD(whiteboard), NULL);

	priv = purple_whiteboard_get_instance_private(whiteboard);

	return priv->draw_list;
}

void
purple_whiteboard_set_draw_list(PurpleWhiteboard *whiteboard,
                                GList* draw_list)
{
	PurpleWhiteboardPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_WHITEBOARD(whiteboard));

	priv = purple_whiteboard_get_instance_private(whiteboard);
	priv->draw_list = draw_list;

	g_object_notify_by_pspec(G_OBJECT(whiteboard), properties[PROP_DRAW_LIST]);
}

gboolean
purple_whiteboard_equal(PurpleWhiteboard *whiteboard1,
                        PurpleWhiteboard *whiteboard2)
{
	PurpleWhiteboardPrivate *priv1 = NULL, *priv2 = NULL;

	if(whiteboard1 == NULL) {
		return (whiteboard2 == NULL);
	} else if(whiteboard2 == NULL) {
		return FALSE;
	}

	priv1 = purple_whiteboard_get_instance_private(whiteboard1);
	priv2 = purple_whiteboard_get_instance_private(whiteboard2);

	return birb_str_equal(priv1->id, priv2->id);
}
