/*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
                       Matthias Münch (matthias@amigaworld.de)

    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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    In addition, as a special exception, Tom Parker and Matthias Münch give
    permission to link the code of this program with a TCP stack of your 
    choice, any official MUI libraries or classes and any custom MUI classes
    that should be necessary for the operation of this program.  This 
    exception also gives you permission to distribute linked combinations 
    including this software with any of the before-mentioned libraries and 
    classes.  You must obey the GNU General Public License in all respects for
    all of the code used other than that provided by the before-mentioned 
    libraries and classes.  As part of this exception you are obliged to 
    follow the license terms of the before-mentioned libraries, this license
    does not compel you to follow those terms, but if you do not then you may
    not link with those libraries.  If you modify this file, you may extend
    this exception to your version of the file, but you are not obligated to
    do so.  If you do not wish to do so, delete this exception statement from
    your version.
*/
/*
** agent management
*/

#include "common.h"

#include <MUI/NListview_mcc.h>
#include <MUI/NFloattext_mcc.h>

static ULONG agents_new(struct IClass *cl, Object *obj, struct opSet *msg);
static MUI_LIST_DISP_DECL(listdisp, jagent a);
static void agents_parse(struct agentsdata *data, ikspak *pak);
static void agents_updateinfo(struct agentsdata *data);
static void agents_register(struct agentsdata *data);
static void agents_search(struct agentsdata *data);


MUI_DISPATCH(agents_dispatch)
{
	switch(msg->MethodID)
	{
	case OM_NEW:
		return agents_new(cl,obj,(APTR)msg);

	case AGENTS_PARSE:
		agents_parse(INST_DATA(cl,obj), (ikspak *)MARG1);
		return 0;

	case AGENTS_UPDATEINFO:
		agents_updateinfo(INST_DATA(cl,obj));
		return 0;

	case AGENTS_REG:
		agents_register(INST_DATA(cl,obj));
		return 0;

	case AGENTS_SEARCH:
		agents_search(INST_DATA(cl,obj));
		return 0;

	}
	return DoSuperMethodA(cl, obj, msg);
}


static ULONG agents_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	static struct Hook dispHook = { {0,0}, &listdisp, NULL, NULL };
	struct agentsdata *data;
	Object *list, *url, *desc, *regbut, *editbut, *searchbut, *urlbut;

	obj = (Object *)DoSuperNew(cl,obj,
		MUIA_Window_ID, MAKE_ID('A', 'G', 'E', 'N'),
		MUIA_Window_Title, MSG_AGENTS_TITLE,
		WindowContents, VGroup,
			Child, NListviewObject,
				MUIA_NListview_NList, list = NListObject,
					InputListFrame,
					MUIA_NList_Title, TRUE,
					MUIA_NList_Format, "BAR, BAR, BAR, BAR",
					MUIA_NList_DisplayHook, &dispHook,
					MUIA_CycleChain, 1,
				End,
			End,
			Child, VGroup,
				MUIA_Group_Columns, 2,
				Child, Label1(MSG_AGENTS_URL),
				Child, HGroup,
					Child, url = TextObject,
						TextFrame,
						MUIA_Background, MUII_TextBack,
					End,
					Child, urlbut = mui_button(MSG_AGENTS_VISIT_GAD),
				End,
				Child, VGroup,
					MUIA_HorizWeight, 0,
					Child, Label1(MSG_AGENTS_DESCRIPTION),
					Child, RectangleObject, End,
				End,
				Child, NListviewObject,
					MUIA_FixHeightTxt, "\n\n\n",
					MUIA_NListview_NList, desc = NFloattextObject,
						ReadListFrame,
					End,
				End,
			End,
			Child, HGroup,
				Child, regbut = mui_button(MSG_AGENTS_REGISTER_GAD),
				Child, searchbut = mui_button(MSG_AGENTS_SEARCH_GAD),
				Child, editbut = mui_button(MSG_AGENTS_CONFERENCE_GAD),
			End,
		End,
		TAG_MORE, msg->ops_AttrList);

	if(!obj) return(0);

	data = INST_DATA(cl,obj);
	data->list = list;
	data->url = url;
	data->desc = desc;
	data->regbut = regbut;
	data->searchbut = searchbut;
	data->urlbut = urlbut;

	set(urlbut, MUIA_HorizWeight, 0);

	DoMethod(list, MUIM_Notify, MUIA_NList_EntryClick, MUIV_EveryTime, obj, 1, AGENTS_UPDATEINFO);
	DoMethod(regbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, AGENTS_REG);
	DoMethod(editbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, AGENTS_REG);
	DoMethod(searchbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, AGENTS_SEARCH);


	DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);

	return (ULONG)obj;
}


MUI_LIST_DISP_STATIC(listdisp, jagent a)
{
	if(a) {
		*array++ = a->name;
		if(a->reg) *array++ = MSG_AGENTS_YES; else *array++ = MSG_AGENTS_NO;
		if(a->search) *array++ = MSG_AGENTS_YES; else *array++ = MSG_AGENTS_NO;
		if(a->chat) *array = MSG_AGENTS_YES; else *array = MSG_AGENTS_NO;
	} else {
		*array++ = "\33cName";
		*array++ = "\33cRegister";
		*array++ = "\33cSearch";
		*array = "\33cConferencing";
	}

	return 0;
}


void agents_fetch(void)
{
	iks *x;

	x = iks_make_iq(IKS_TYPE_GET, IKS_NS_AGENTS);
	iks_send(net.parser, x);
	iks_delete(x);
}


static void agents_parse(struct agentsdata *data, ikspak *pak)
{
	ikspool *p;
	jagent a;
	iks *x;

	x = iks_child(iks_find(pak->x, "query"));
	while(x)
	{
		if(iks_type(x) == IKS_TAG)
		{
			p = iks_pool_new(512);
			if(!p) break;
			a = iks_pool_alloc(p, sizeof(jagent_struct));
			if(!a) break;
			memset(a, 0, sizeof(jagent_struct));

			a->jid = iks_pool_strdup(p, iks_find_attrib(x, "jid"));
			a->name = iks_pool_strdup(p, iks_find_cdata(x, "name"));
			a->url = iks_pool_strdup(p, iks_find_cdata(x, "url"));
			a->desc = iks_pool_strdup(p, iks_find_cdata(x, "description"));
			a->transport = iks_pool_strdup(p, iks_find_cdata(x, "transport"));
			a->service = iks_pool_strdup(p, iks_find_cdata(x, "service"));

			if(iks_find(x, "register")) a->reg=1;
			if(iks_find(x, "search")) a->search=1;
			if(iks_find(x, "groupchat")) a->chat=1;

			DoMethod(data->list, MUIM_NList_InsertSingle, a, MUIV_NList_Insert_Bottom);
		}

		x = iks_next(x);
	}

}


static void agents_updateinfo(struct agentsdata *data)
{
	u_long t;
	jagent a;

	GetAttr(MUIA_NList_EntryClick, data->list, &t);
	if(t == -1 || t == -2) return;
	DoMethod(data->list, MUIM_NList_GetEntry, t, &a);

	set(data->url, MUIA_Text_Contents, a->url);
	set(data->desc, MUIA_NFloattext_Text, a->desc);

	if(a->reg) set(data->regbut, MUIA_Disabled, FALSE); else set(data->regbut, MUIA_Disabled, TRUE);
	if(a->search) set(data->searchbut, MUIA_Disabled, FALSE); else set(data->searchbut, MUIA_Disabled, TRUE);
	if(a->url) set(data->urlbut, MUIA_Disabled, FALSE); else set(data->urlbut, MUIA_Disabled, TRUE);
}


static void agents_register(struct agentsdata *data)
{
	u_long t;
	jagent a;
	iks *x;

	if(net.state != NET_ON) return;

	GetAttr(MUIA_NList_EntryClick, data->list, &t);
	if(t == -1 || t == -2) return;
	DoMethod(data->list, MUIM_NList_GetEntry, t, &a);

	if(!a->jid) return;

	x = iks_make_iq(IKS_TYPE_GET, IKS_NS_REGISTER);
	iks_insert_attrib(x, "to", a->jid);
	iks_send(net.parser, x);
	iks_delete(x);
}


static void agents_search(struct agentsdata *data)
{
	u_long t;
	jagent a;
	iks *x;

	if(net.state != NET_ON) return;

	GetAttr(MUIA_NList_EntryClick, data->list, &t);
	if(t == -1 || t == -2) return;
	DoMethod(data->list, MUIM_NList_GetEntry, t, &a);

	if(!a->jid) return;

	x = iks_make_iq(IKS_TYPE_GET, IKS_NS_SEARCH);
	iks_insert_attrib(x, "to", a->jid);
	iks_send(net.parser, x);
	iks_delete(x);
}
