/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <birb.h>

#include <purple.h>

/******************************************************************************
 * Normal PurpleUi Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_UI (test_purple_ui_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleUi, test_purple_ui, TEST_PURPLE, UI, PurpleUi)

struct _TestPurpleUi {
	PurpleUi parent;
};

static PurpleAccountManagerBackend *
test_purple_ui_get_account_manager_backend(PurpleUi *ui, GError **error) {
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);
	g_assert_nonnull(error);

	return purple_account_manager_seagull_backend_new(":memory:", error);
}

static PurpleContactManagerBackend *
test_purple_ui_get_contact_manager_backend(PurpleUi *ui, GError **error) {
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);
	g_assert_nonnull(error);

	return purple_contact_manager_seagull_backend_new(":memory:", error);
}

static PurpleConversationManagerBackend *
test_purple_ui_get_conversation_manager_backend(PurpleUi *ui, GError **error) {
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);
	g_assert_nonnull(error);

	return purple_conversation_manager_seagull_backend_new(":memory:", error);
}

static PurplePresenceManagerBackend *
test_purple_ui_get_presence_manager_backend(PurpleUi *ui, GError **error) {
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);
	g_assert_nonnull(error);

	return purple_presence_manager_seagull_backend_new(":memory:", error);
}

static PurpleSchedulerBackend *
test_purple_ui_get_scheduler_backend(PurpleUi *ui, GError **error) {
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);
	g_assert_nonnull(error);

	return NULL;
}

static char *
test_purple_ui_get_display_version(PurpleUi *ui) {
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	return g_strdup("Test Ui 1234");
}

static char *
test_purple_ui_get_user_agent(PurpleUi *ui) {
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	return g_strdup("Test Ui/1234");
}

G_DEFINE_FINAL_TYPE(TestPurpleUi, test_purple_ui, PURPLE_TYPE_UI)

static void
test_purple_ui_init(G_GNUC_UNUSED TestPurpleUi *ui) {
}

static void
test_purple_ui_class_init(TestPurpleUiClass *klass) {
	PurpleUiClass *ui_class = PURPLE_UI_CLASS(klass);

	ui_class->get_account_manager_backend =
		test_purple_ui_get_account_manager_backend;
	ui_class->get_contact_manager_backend =
		test_purple_ui_get_contact_manager_backend;
	ui_class->get_conversation_manager_backend =
		test_purple_ui_get_conversation_manager_backend;
	ui_class->get_presence_manager_backend =
		test_purple_ui_get_presence_manager_backend;
	ui_class->get_scheduler_backend = test_purple_ui_get_scheduler_backend;

	ui_class->get_display_version = test_purple_ui_get_display_version;
	ui_class->get_user_agent = test_purple_ui_get_user_agent;
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_ui_get_account_manager_backend_test(void) {
	PurpleAccountManagerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_account_manager_backend(ui, &error);
	g_assert_no_error(error);

	g_assert_true(PURPLE_IS_ACCOUNT_MANAGER_BACKEND(backend));
	g_assert_finalize_object(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_contact_manager_backend_test(void) {
	PurpleContactManagerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_contact_manager_backend(ui, &error);
	g_assert_no_error(error);

	g_assert_true(PURPLE_IS_CONTACT_MANAGER_BACKEND(backend));
	g_assert_finalize_object(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_conversation_manager_backend_test(void) {
	PurpleConversationManagerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_conversation_manager_backend(ui, &error);
	g_assert_no_error(error);

	g_assert_true(PURPLE_IS_CONVERSATION_MANAGER_BACKEND(backend));
	g_assert_finalize_object(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_presence_manager_backend_test(void) {
	PurplePresenceManagerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_presence_manager_backend(ui, &error);
	g_assert_no_error(error);

	g_assert_true(PURPLE_IS_PRESENCE_MANAGER_BACKEND(backend));
	g_assert_finalize_object(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_scheduler_backend_test(void) {
	PurpleSchedulerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_scheduler_backend(ui, &error);
	g_assert_no_error(error);

	/* We don't have a backend to verify yet, so this will be null. */
	g_assert_null(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_display_version_test(void) {
	PurpleUi *ui = NULL;
	char *display_version = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	display_version = purple_ui_get_display_version(ui);
	g_assert_cmpstr(display_version, ==, "Test Ui 1234");
	g_clear_pointer(&display_version, g_free);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_user_agent_test(void) {
	PurpleUi *ui = NULL;
	char *user_agent = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	user_agent = purple_ui_get_user_agent(ui);
	g_assert_cmpstr(user_agent, ==, "Test Ui/1234");
	g_clear_pointer(&user_agent, g_free);

	g_assert_finalize_object(ui);
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char *argv[]) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/ui/normal/get-account-manager-backend",
	                test_purple_ui_get_account_manager_backend_test);
	g_test_add_func("/ui/normal/get-contact-manager-backend",
	                test_purple_ui_get_contact_manager_backend_test);
	g_test_add_func("/ui/normal/get-conversation-manager-backend",
	                test_purple_ui_get_conversation_manager_backend_test);
	g_test_add_func("/ui/normal/get-presence-manager-backend",
	                test_purple_ui_get_presence_manager_backend_test);
	g_test_add_func("/ui/normal/get-scheduler-backend",
	                test_purple_ui_get_scheduler_backend_test);

	g_test_add_func("/ui/normal/get-display-version",
	                test_purple_ui_get_display_version_test);
	g_test_add_func("/ui/normal/get-user-agent",
	                test_purple_ui_get_user_agent_test);

	return g_test_run();
}
