/*  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.
*/
/*
** xml console
*/

#include "common.h"

#include <time.h>

#include <MUI/TextEditor_mcc.h>

static Object *c_list;
static int c_watchflag = 0;

ULONG con_new(struct IClass *cl, Object *obj, struct opSet *msg);
static void con_send(struct condata *data);
static void log_to_file(int type, char *data);


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

	case CON_SEND:
		con_send(INST_DATA(cl,obj));
		return 0;

	case CON_SAVE:
		return 0;

	case CON_TOGGLEXML:
	{
		struct condata *data = INST_DATA(cl,obj);
		u_long tmp;
		GetAttr(MUIA_Selected, data->xmltog, &tmp);
		c_watchflag = tmp;
		return 0;
	}


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


ULONG con_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	struct condata *data;
	Object *xmltog, *savebut, *flushbut, *list;
	Object *texted, *slider, *clrbut, *sendbut;

	obj = (Object *)DoSuperNew(cl,obj,
		MUIA_Window_ID, MAKE_ID('C','O','N','S'),
		MUIA_Window_Title, "XML Console",
		MUIA_HelpNode, "window-console",
		WindowContents, VGroup,
			Child, HGroup,
				Child, mui_toggle("Watch XML Traffic", &xmltog),
				Child, savebut = SimpleButton(MSG_CONSOLE_SAVE),
				Child, flushbut = SimpleButton(MSG_CONSOLE_FLUSH),
			End,
			Child, list = NewObject(gui.chatarea_mcc->mcc_Class, NULL, TAG_DONE),
			Child, HGroup,
				Child, HGroup,
					MUIA_Group_HorizSpacing, 0,
					MUIA_HorizWeight, 300,
					Child, texted = TextEditorObject,
						MUIA_CycleChain, 1,
					End,
					Child, slider = ScrollbarObject, End,
				End,
				Child, VGroup,
					Child, clrbut = SimpleButton(MSG_CONSOLE_CLEAR),
					Child, sendbut = SimpleButton(MSG_CONSOLE_SEND),
				End,
			End,
		End,
		TAG_MORE, msg->ops_AttrList);

	if(!obj) return(0);

	data = INST_DATA(cl,obj);
	data->list = list;
	c_list = list;
	data->texted = texted;
	data->xmltog = xmltog;

	set(texted,MUIA_TextEditor_Slider,slider);


	DoMethod(xmltog, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, obj, 1, CON_TOGGLEXML);

	DoMethod(savebut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, CON_SAVE);
	DoMethod(flushbut, MUIM_Notify, MUIA_Pressed, FALSE, list, 1, CHATAREA_FLUSH);

	DoMethod(clrbut,MUIM_Notify,MUIA_Pressed,FALSE,texted,1,MUIM_TextEditor_ClearText);
	DoMethod(sendbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,CON_SEND);

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

	return((ULONG)obj);
}


static void con_send(struct condata *data)
{
	char *text;

	if(net.state <= NET_CONNECT)
	{
		con_debug("not connected yet!");
		return;
	}

	text = (char *)DoMethod(data->texted, MUIM_TextEditor_ExportText);
	iks_send_raw(net.parser, text);
	FreeVec(text);
}


#ifdef NOMORPHOS
static void console_logger_gate(void);

struct EmulLibEntry console_logger =
{
	TRAP_LIB, 0, (void (*)(void)) console_logger_gate
};

static void console_logger_gate(void)
{
	ULONG *lala = (ULONG *)REG_A7;
	void *udata = *lala++;
	char *xml = *lala++;
	int dir = *lala++;
	REG_A7 = lala;

#else
void SAVEDS console_logger(void *udata, char *xml, int dir)
{
#endif
	if(dir == 0 && prf.logflags & 1) log_to_file(0, xml);
	if(dir != 0 && prf.logflags & 2) log_to_file(1, xml);

	if(c_watchflag)
	{
		char *tmp;
		if(dir == 0) tmp = "Recv"; else tmp = "Send";
		DoMethod(c_list, CHATAREA_ADD, dir, tmp, xml);
	}
}


void con_debug(char *msg, ...)
{
	static char buf2[13];
	char *buf;
	va_list ap;
	time_t t;
	struct tm *ut;

	time(&t);
	ut = localtime(&t);
	sprintf(buf2, "%02d:%02d:%02d", ut->tm_hour, ut->tm_min, ut->tm_sec);

	va_start(ap,msg);
	buf = my_vprintf(msg, ap);
	va_end(ap);

	if(prf.logflags & 4) log_to_file(2, buf);
	DoMethod(c_list, CHATAREA_ADD, 2, buf2, buf);
}


static void log_to_file(int type, char *data)
{
	FILE *f;

	if(!prf.logfile) return;

	f = fopen(prf.logfile, "a");
	if(f)
	{
		switch(type)
		{
		case 0: fputs("* XML Received:\n", f); break;
		case 1: fputs("* XML Sent:\n", f); break;
		case 2: fputs("* Debug:\n", f); break;
		}
		fputs(data, f);
		fputs("\n\n", f);
		fclose(f);
	}
}
