/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Pidgin 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 program 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 program 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 program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib/gi18n-lib.h>

#include <purple.h>

#include <adwaita.h>

#include "pidginnetworkprefs.h"

struct _PidginNetworkPrefs {
	AdwPreferencesPage parent;

	GtkWidget *stun_server;
	GtkWidget *auto_ip;
	GtkWidget *public_ip;
	GtkWidget *map_ports;
	GtkWidget *ports_range_use;
	GtkWidget *ports_range_start;
	GtkWidget *ports_range_end;
};

G_DEFINE_FINAL_TYPE(PidginNetworkPrefs, pidgin_network_prefs,
                    ADW_TYPE_PREFERENCES_PAGE)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
network_ip_changed(GtkEditable *editable, G_GNUC_UNUSED gpointer data)
{
	const gchar *text = gtk_editable_get_text(editable);
	GtkWidget *widget = GTK_WIDGET(editable);

	if (text && *text) {
		if (g_hostname_is_ip_address(text)) {
			purple_network_set_public_ip(text);
			gtk_widget_remove_css_class(widget, "error");
			gtk_widget_add_css_class(widget, "success");
		} else {
			gtk_widget_remove_css_class(widget, "success");
			gtk_widget_add_css_class(widget, "error");
		}

	} else {
		purple_network_set_public_ip("");
		gtk_widget_remove_css_class(widget, "success");
		gtk_widget_remove_css_class(widget, "error");
	}
}

static void
auto_ip_button_clicked_cb(G_GNUC_UNUSED GObject *obj,
                          G_GNUC_UNUSED GParamSpec *pspec, gpointer data)
{
	PidginNetworkPrefs *prefs = PIDGIN_NETWORK_PREFS(data);
	GSettings *settings = NULL;
	const char *ip;
	char *auto_ip_text;
	GList *list = NULL;

	/* Make a lookup for the auto-detected IP ourselves. */
	settings = g_settings_new_with_backend("im.pidgin.Purple.Network.IP",
	                                       purple_core_get_settings_backend());
	if(g_settings_get_boolean(settings, "autodetect")) {
		ip = "0.0.0.0";
	} else {
		ip = _("Disabled");
	}

	auto_ip_text = g_strdup_printf(_("Use _automatically detected IP address: %s"), ip);
	adw_preferences_row_set_title(ADW_PREFERENCES_ROW(prefs->auto_ip),
	                              auto_ip_text);
	g_free(auto_ip_text);
	g_list_free_full(list, g_free);
	g_object_unref(settings);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
pidgin_network_prefs_class_init(PidginNetworkPrefsClass *klass)
{
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	gtk_widget_class_set_template_from_resource(
		widget_class,
		"/im/pidgin/Pidgin3/Prefs/network.ui"
	);

	gtk_widget_class_bind_template_child(widget_class, PidginNetworkPrefs,
	                                     stun_server);
	gtk_widget_class_bind_template_child(widget_class, PidginNetworkPrefs,
	                                     auto_ip);
	gtk_widget_class_bind_template_child(widget_class, PidginNetworkPrefs,
	                                     public_ip);
	gtk_widget_class_bind_template_child(widget_class, PidginNetworkPrefs,
	                                     map_ports);
	gtk_widget_class_bind_template_child(widget_class, PidginNetworkPrefs,
	                                     ports_range_use);
	gtk_widget_class_bind_template_child(widget_class, PidginNetworkPrefs,
	                                     ports_range_start);
	gtk_widget_class_bind_template_child(widget_class, PidginNetworkPrefs,
	                                     ports_range_end);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        auto_ip_button_clicked_cb);
	gtk_widget_class_bind_template_callback(widget_class, network_ip_changed);
}

static void
pidgin_network_prefs_init(PidginNetworkPrefs *prefs)
{
	GSettings *settings = NULL;
	char *ip = NULL;

	gtk_widget_init_template(GTK_WIDGET(prefs));

	settings = g_settings_new_with_backend("im.pidgin.Purple.Network.Stun",
	                                       purple_core_get_settings_backend());
	g_settings_bind(settings, "server", prefs->stun_server, "text",
	                G_SETTINGS_BIND_DEFAULT);
	g_clear_object(&settings);

	settings = g_settings_new_with_backend("im.pidgin.Purple.Network.IP",
	                                       purple_core_get_settings_backend());
	g_settings_bind(settings, "autodetect", prefs->auto_ip, "active",
	                G_SETTINGS_BIND_DEFAULT);
	g_clear_object(&settings);

	ip = purple_network_get_public_ip();
	gtk_editable_set_text(GTK_EDITABLE(prefs->public_ip), ip);
	g_free(ip);

	settings = g_settings_new_with_backend("im.pidgin.Purple.Network.Ports",
	                                       purple_core_get_settings_backend());
	g_settings_bind(settings, "use-discovery", prefs->map_ports, "active",
	                G_SETTINGS_BIND_DEFAULT);
	g_clear_object(&settings);

	settings = g_settings_new_with_backend("im.pidgin.Purple.Network.Ports.Manual",
	                                       purple_core_get_settings_backend());

	g_settings_bind(settings, "enabled",
	                prefs->ports_range_use, "enable-expansion",
	                G_SETTINGS_BIND_DEFAULT);

	g_settings_bind(settings, "start", prefs->ports_range_start, "value",
	                G_SETTINGS_BIND_DEFAULT);
	g_settings_bind(settings, "end", prefs->ports_range_end, "value",
	                G_SETTINGS_BIND_DEFAULT);

	g_clear_object(&settings);
}

/******************************************************************************
 * API
 *****************************************************************************/
GtkWidget *
pidgin_network_prefs_new(void) {
	return g_object_new(PIDGIN_TYPE_NETWORK_PREFS, NULL);
}
