/*  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.
*/
/*
** message read window
*/

#include "common.h"

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

static ULONG read_new(struct IClass *cl, Object *obj, struct opSet *msg);
static void read_show(struct readdata *data, jmessage *m);
static void read_stat(struct readdata *data);


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

	case READ_CLOSE:
	{
		struct readdata *data = INST_DATA(cl,obj);
		data->user->readwin = NULL;
		DoMethod(gui.app, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
		return 0;
	}

	case READ_WRITE:
	{
		struct readdata *data = INST_DATA(cl,obj);
		jmessage *m = data->user->readmsg;
		if(!m) return(0);
		write_to(iks_id_print(data->user->id), iks_find_cdata(m->pak->x, "thread"), NULL);
		return 0;
	}

	case READ_REPLY:
	{
		struct readdata *data = INST_DATA(cl,obj);
		jmessage *m = data->user->readmsg;
		if(!m) return(0);
		write_to(iks_id_print(data->user->id), iks_find_cdata(m->pak->x, "thread"), convert_locale(iks_find_cdata(m->pak->x, "body")));
		return 0;
	}

	case READ_NEXT:
	{
		struct readdata *data = INST_DATA(cl,obj);
		jmessage *m = NULL;
		if(!data->user->readmsg)
		{
			if(data->user->msgs) data->user->readmsg = list_first(&data->user->msgs);
		}
		if(data->user->readmsg) m = list_next(data->user->readmsg);
		if(m) read_show(data, m);
		return 0;
	}

	case READ_PREV:
	{
		struct readdata *data = INST_DATA(cl,obj);
		jmessage *m = NULL;
		if(data->user->readmsg) m = list_prev(data->user->readmsg);
		if(m) read_show(data, m);
		return 0;
	}

	case READ_UPDATE:
		read_stat(INST_DATA(cl,obj));
		return 0;

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


static ULONG read_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	struct readdata *data;
	Object *prevbut, *stat, *nextbut;
	Object *errorlab, *errortxt;
	Object *fromtxt, *thread, *text;
	Object *writebut, *repbut, *closebut;

	obj = (Object *)DoSuperNew(cl,obj,
		MUIA_Window_Title, "Read messages",
		WindowContents, VGroup,
			Child, HGroup,
				Child, prevbut = mui_button(MSG_READ_PREV),
				Child, stat = TextObject,
					TextFrame,
					MUIA_Background, MUII_TextBack,
				End,
				Child, nextbut = mui_button(MSG_READ_NEXT),
			End,
			Child, ColGroup(2),
				Child, Label1(MSG_READ_FROM),
				Child, fromtxt = TextObject,
					TextFrame,
					MUIA_Background, MUII_TextBack,
				End,
				Child, Label1(MSG_READ_THREAD),
				Child, thread = TextObject,
					TextFrame,
					MUIA_Background, MUII_TextBack,
				End,
				Child, errorlab = Label1(MSG_READ_ERROR),
				Child, errortxt = TextObject,
					TextFrame,
					MUIA_Background, MUII_TextBack,
				End,
			End,
			Child, NListviewObject,
					MUIA_NListview_NList, text = MailtextObject,
						MUIA_Mailtext_Text,              text,
						MUIA_Mailtext_ForbidContextMenu, FALSE,
						MUIA_Frame,                      MUIV_Frame_InputList,
						MUIA_NList_Input,                FALSE,
						MUIA_NList_MultiSelect,          FALSE,
					End,
					MUIA_CycleChain, TRUE,
			End,
			Child, HGroup,
				Child, writebut = mui_button(MSG_READ_WRITE),
				Child, repbut = mui_button(MSG_READ_REPLY),
				Child, closebut = mui_button(MSG_READ_CLOSE),
			End,
		End,
		TAG_MORE, msg->ops_AttrList);

	if(!obj) {
		con_debug("error creating new read window!");
		return(0);
	}
	
/*
	set(errorlab, MUIA_ShowMe, FALSE);
	set(errortxt, MUIA_ShowMe, FALSE);
*/
	data = INST_DATA(cl,obj);
	data->user = (juser)GetTagData(READ_FROM, NULL, msg->ops_AttrList);
	data->fromtxt = fromtxt;
	data->thread = thread;
	data->text = text;
	data->prevbut = prevbut;
	data->stat = stat;
	data->nextbut = nextbut;
	data->errorlab = errorlab;
	data->errortxt = errortxt;

	if(data->user->readmsg)
	{
		if(data->user->readmsg->flags & MSG_READ && list_next(data->user->readmsg))
			data->user->readmsg = list_next(data->user->readmsg);
		read_show(data, data->user->readmsg);
	}
	else
	{
		if(data->user->msgs)
			read_show(data, list_first(&data->user->msgs));
	}

	DoMethod(prevbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, READ_PREV);
	DoMethod(repbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, READ_REPLY);
	DoMethod(writebut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, READ_WRITE);
	DoMethod(nextbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, READ_NEXT);

	DoMethod(closebut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, READ_CLOSE);
	DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 1, READ_CLOSE);
	
	return((ULONG)obj);
}


static void read_show(struct readdata *data, jmessage *m)
{
	juser u = data->user;
	int more = 0;

	if(!m) return;

	m->flags |= MSG_READ;
	if(u->msgs)
	{
		jmessage *tm;

		tm = list_first(&u->msgs);
		while(tm)
		{
			if(!(tm->flags & MSG_READ)) more++;
			tm = list_next(tm);
		}
	}

	if(more == 0)
	{
		u->event = 0;
		roster_refresh(u);
	}

	u->readmsg = m;

	if(u->name)
		set(data->fromtxt, MUIA_Text_Contents, my_printf("%s (%s)", u->name, iks_id_print(m->pak->from)));
	else
		set(data->fromtxt, MUIA_Text_Contents, iks_id_print(m->pak->from));

	set(data->thread, MUIA_Text_Contents, iks_find_cdata(m->pak->x, "thread"));
	set(data->text, MUIA_Mailtext_Text, convert_locale(iks_find_cdata(m->pak->x, "body")));

	if(m->pak->subtype == IKS_TYPE_ERROR)
	{
		set(data->errortxt, MUIA_Text_Contents, iks_find_cdata(m->pak->x, "error"));
/*		set(data->errorlab, MUIA_ShowMe, TRUE);
		set(data->errortxt, MUIA_ShowMe, TRUE);
*/	}
	else
	{
		set(data->errortxt, MUIA_Text_Contents, NULL);
/*		set(data->errorlab, MUIA_ShowMe, FALSE);
		set(data->errortxt, MUIA_ShowMe, FALSE);
*/	}

	read_stat(data);

}


static void read_stat(struct readdata *data)
{
	u_long tmp;
	jmessage *m = data->user->readmsg;

	if(!m) return;

	if(list_next(m)) tmp = FALSE; else tmp = TRUE;
	set(data->nextbut, MUIA_Disabled, tmp);
	if(list_prev(m)) tmp = FALSE; else tmp = TRUE;
	set(data->prevbut, MUIA_Disabled, tmp);
}


void read_from(juser u)
{
	Object *win;
	if(u->readwin) {
		DoMethod(u->readwin, MUIM_Window_ToFront);
		return;
	}

	win = NewObject(gui.read_mcc->mcc_Class, NULL, READ_FROM, u, TAG_DONE);
	if(win) {
		DoMethod(gui.app, OM_ADDMEMBER, win);
		set(win, MUIA_Window_Open, TRUE);
		u->readwin = win;
	}
}
