/*
 * Gnophone: A client for the Asterisk PBX
 *
 * Copyright (C) 2000-2005, Digium, Inc.
 *
 * Written by Mark Spencer
 *
 * Linux/UNIX version distributed under the terms of
 * the GNU General Public License
 *
 * conversation.c The conversation (text) stuff
 *
 * This file was originally created by Rob Flynn.
 */
#define ICON_CANCEL
#include "gnophone.h"

static GList *conversations = NULL;

extern struct peerwindow* find_peerwindow(int);

struct conversation *find_conversation(int pcid)
{
	GList *tmp = conversations;
	struct conversation *cnv;

	while (tmp) {
		cnv = (struct conversation *)tmp->data;

		if (cnv->pcid == pcid)
			return cnv;

		tmp = tmp->next;
	}
	
	return NULL;
}

void send_message_cb(GtkWidget *w, gpointer data)
{
	struct conversation *c = (struct conversation *)data;
	gchar line[1024 * 9];
	gchar *text = (gchar*)gtk_entry_get_text(GTK_ENTRY(c->entry));
	GdkColor col;
	
	col.red = 0;
	col.green = 0;
	col.blue = 56000;

	gdk_colormap_alloc_color(gtk_widget_get_colormap(GTK_WIDGET(c->text)), &col, FALSE, TRUE);

	if (strlen(text) > 0) {
		g_snprintf(line, sizeof(line), "%s: ", my_callerid);
		gtk_text_insert(GTK_LABEL(c->text), NULL, &col, NULL, line, strlen(line));
		g_snprintf(line, sizeof(line), "%s\n", text);
		gtk_text_insert(GTK_LABEL(c->text), NULL, NULL, NULL, line, strlen(line));

		iax_send_text(c->pcid, text);
	}

	/* Now, we should clear out the box */
	gtk_entry_set_text(GTK_ENTRY(c->entry), "");
}

void close_convo_cb(GtkWidget *w, gpointer data)
{
	struct conversation *c = (struct conversation *)data;

	if (c != NULL) {
		/* Remove the widget */
		gtk_widget_destroy(c->window);

		/* And remove it from our list of goodies */
		conversations = g_list_remove(conversations, c);
	} else {
		fprintf(stderr, "Trying to close a nonexistent conversaiton: HUH?\n");
	}
}

void open_conversation(int pcid)
{
	struct conversation *cnv;
	struct peerwindow *p;

	GtkWidget *vbox;
	GtkWidget *hbox;
	GtkWidget *sw;
	char buf[4096];

	p = find_peerwindow(pcid);

	if (p == NULL)
		return;

	cnv = find_conversation(pcid);

	if (cnv != NULL)
		return;

	cnv = (struct conversation *)g_new0(struct conversation, 1);

	conversations = g_list_append(conversations, cnv);
	
	cnv->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_usize(GTK_WIDGET(cnv->window), 400, 275);
	g_snprintf(buf, sizeof(buf), "Conversation with %s", p->cid);
	gtk_window_set_title(GTK_WINDOW(cnv->window), buf);
	gtk_widget_realize(cnv->window);

	vbox = gtk_vbox_new(FALSE, 5);
	gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
	gtk_widget_show(vbox);
	gtk_container_add(GTK_CONTAINER(cnv->window), vbox);

	sw = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_widget_show(sw);

	cnv->text = gtk_text_view_new();
	gtk_widget_show(cnv->text);
	gtk_container_add(GTK_CONTAINER(sw), cnv->text);
	gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0);

	cnv->entry = gtk_entry_new_with_max_length(8192);
	gtk_signal_connect(GTK_OBJECT(cnv->entry), "activate", 
			GTK_SIGNAL_FUNC(send_message_cb), cnv);
	gtk_box_pack_start(GTK_BOX(vbox), cnv->entry, FALSE, FALSE, 0);
	gtk_widget_show(cnv->entry);

	hbox = gtk_hbox_new(FALSE, 10);
	gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
	gtk_widget_show(hbox);
	
	cnv->close = image_button_new(cnv->window, IMAGE_BUTTON_TYPE_BUTTON, "Close", cancel_xpm);
	gtk_widget_set_usize(cnv->close, 90, 40);
	gtk_signal_connect(GTK_OBJECT(cnv->close), "clicked", GTK_SIGNAL_FUNC(close_convo_cb), cnv);
	gtk_box_pack_end(GTK_BOX(hbox), cnv->close, FALSE, FALSE, 0);
	
	gtk_widget_show(cnv->window);
}

void conversation_inactivate(int pcid)
{
	struct conversation *c = NULL;

	c = find_conversation(pcid);

	if (c != NULL) {
		gtk_widget_set_sensitive(GTK_WIDGET(c->entry), FALSE);
	}
}
