/*
** Amster - Result view
** by Jacob Laursen <laursen@myself.com>
*/

#include "include/config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <proto/dos.h>

#include "include/mui.h"
#include <MUI/Lamp_mcc.h>
#include <MUI/NListview_mcc.h>

#include "include/gui.h"
#include "include/resultview.h"
#include "include/protos.h"
#include "include/napster.h"
#include "include/download.h"
#include "include/panel.h"
#include "include/prefs.h"
#include "include/search.h"
#include "include/hotlist.h"
#include "amster_Cat.h"
#include "include/protos.h"

/* Private prototypes */

ULONG resultview_new(struct IClass *cl, Object *obj, struct opSet *msg);
MUIF resultlistdisp(REG(a2) char **array, REG(a1) song s);
MUIF resultlistcomp(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_CompareMessage *ncm);
void resultview_clear(struct resultdata *data, int t);
void resultview_download(struct resultdata *data, int t);
void resultview_nick(struct resultdata *data, char *nick);
void resultview_stat(struct resultdata *data, int t);
void resultview_found(struct resultdata *data, song s);
void resultview_reset(struct resultdata *data);

/* Global variables */

char *gColFormat;


MUIF resultview_dispatch(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
{
	struct resultdata *data;

	switch (msg->MethodID) {
		case OM_NEW:
			return(resultview_new(cl,obj,(APTR)msg));
		case RESULTVIEW_CLEAR:
			data = INST_DATA(cl, obj);
			resultview_clear(data, (int)(((muimsg)msg)->arg1));
			return NULL;
		case RESULTVIEW_DOWNLOAD:
			data = INST_DATA(cl, obj);
			resultview_download(data, (int)(((muimsg)msg)->arg1));
			return NULL;
		case RESULTVIEW_FOUND:
			data = INST_DATA(cl, obj);
			resultview_found(data, (song)(((muimsg)msg)->arg1));
			return NULL;
		case RESULTVIEW_TOGGLE_BOTH:
			{
			u_long tmp;
			data = INST_DATA(cl, obj);
			GetAttr(MUIA_Selected, data->fptog, &tmp);
			prf->fullpath = tmp;
			DoMethod(gui->searchpanel, SEARCH_TOGGLE);
			DoMethod(gui->WI_Hotlist, HOTLIST_TOGGLE);
			return NULL;
			}
		case RESULTVIEW_TOGGLE_ONE:
			data = INST_DATA(cl, obj);
			set(data->fptog, MUIA_Selected, prf->fullpath);
			DoMethod(data->list, MUIM_NList_Redraw, MUIV_NList_Redraw_All);
			return NULL;
		case RESULTVIEW_STAT:
			data = INST_DATA(cl, obj);
			set(data->result, MUIA_Text_Contents, (char *)(((muimsg)msg)->arg1));
			return NULL;
		case RESULTVIEW_STAT2:
			data = INST_DATA(cl, obj);
			resultview_stat(data, (int)(((muimsg)msg)->arg1));
			return NULL;
		case RESULTVIEW_INSERT:
			data = INST_DATA(cl, obj);
			DoMethod(data->list, MUIM_NList_InsertSingle, (song)(((muimsg)msg)->arg1), MUIV_NList_Insert_Sorted);
			return NULL;
		case RESULTVIEW_NICK:
			{
			data = INST_DATA(cl, obj);
			resultview_nick(data, (char *)(((muimsg)msg)->arg1));
			return NULL;
			}
		case RESULTVIEW_RESETCOUNT:
			data = INST_DATA(cl, obj);
			data->resultview_count = 0;
			return NULL;
		case RESULTVIEW_GETSTATE:
			data = INST_DATA(cl, obj);
			search_state = data->resultview_state;
			return NULL;
	}
	return(DoSuperMethodA(cl, obj, msg));
}


ULONG resultview_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	static struct Hook resultlistdispHook = { {0,0}, &resultlistdisp, NULL, NULL };
	static struct Hook resultlistcompHook = { {0,0}, &resultlistcomp, NULL, NULL };
	struct resultdata *data;

	Object *result, *list;
	Object *BT_Clear, *BT_ClearAll, *BT_Download, *fptog;
/*	Object *BT_Resume;*/

	if (obj = (Object *)DoSuperNew(cl, obj,
		Child, VGroup,
			Child, HGroup,
				Child, Label(MSG_RESULT),
				Child, result = TextObject, End,
			End,
			Child, NListviewObject,
				MUIA_NListview_NList, list = NListObject,
					InputListFrame,
					MUIA_NList_Title, TRUE,
					MUIA_NList_Format, gColFormat,
					MUIA_NList_MultiSelect, MUIV_NList_MultiSelect_Default,
					MUIA_NList_DisplayHook, &resultlistdispHook,
					MUIA_NList_CompareHook2, &resultlistcompHook,
					MUIA_CycleChain, 1,
				End,
			End,
			Child, HGroup,
				Child, BT_Clear    = SimpleButton(MSG_CLEAR_GAD),
				Child, BT_ClearAll = SimpleButton(MSG_CLEARALL_GAD),
/*				Child, BT_Resume   = SimpleButton(MSG_RESUME_GAD),*/
				Child, BT_Download = SimpleButton(MSG_DOWNLOAD_GAD),
				Child, TextObject,
					MUIA_HorizWeight, 25,
					MUIA_Text_PreParse, "\33r",
					MUIA_Text_Contents, MSG_SEARCH_FULLPATH,
				End,
				Child, fptog = ImageObject,
					ButtonFrame,
					MUIA_HorizWeight, 10,
					MUIA_Background, MUII_ButtonBack,
					MUIA_InputMode, MUIV_InputMode_Toggle,
					MUIA_ShowSelState, FALSE,
					MUIA_Image_Spec, "6:15",
					MUIA_Selected, prf->fullpath,
				End,
			End,
		End,
		TAG_MORE, msg->ops_AttrList))
	{
		data = INST_DATA(cl,obj);
		data->result = result;
		data->list = list;
/*		data->BT_Resume = BT_Resume;*/
		data->BT_Clear = BT_Clear;
		data->BT_ClearAll = BT_ClearAll;
		data->BT_Download = BT_Download;
		data->fptog = fptog;

		lamp_useinlist(list);

		DoMethod(BT_Clear,    MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, RESULTVIEW_CLEAR, CLEAR_MARKED);
		DoMethod(BT_ClearAll, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, RESULTVIEW_CLEAR, CLEAR_ALL   );
		DoMethod(BT_Download, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, RESULTVIEW_DOWNLOAD, 0);
/*		DoMethod(BT_Resume, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, RESULTVIEW_DOWNLOAD, 2);*/
		DoMethod(list, MUIM_Notify, MUIA_NList_DoubleClick, MUIV_EveryTime, obj, 2, RESULTVIEW_DOWNLOAD, 1);

		DoMethod(fptog, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, obj, 1, RESULTVIEW_TOGGLE_BOTH);

		DoMethod(list, MUIM_Notify, MUIA_NList_TitleClick,  MUIV_EveryTime, list, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_Both);
		DoMethod(list, MUIM_Notify, MUIA_NList_TitleClick2, MUIV_EveryTime, list, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_2);
		DoMethod(list, MUIM_Notify, MUIA_NList_SortType,    MUIV_EveryTime, list, 3, MUIM_Set, MUIA_NList_TitleMark,  MUIV_TriggerValue);
		DoMethod(list, MUIM_Notify, MUIA_NList_SortType2,   MUIV_EveryTime, list, 3, MUIM_Set, MUIA_NList_TitleMark2, MUIV_TriggerValue);

		return((ULONG)obj);
	}
	return NULL;
}


MUIF resultlistdisp(REG(a2) char **array, REG(a1) song s)
{
	static char buf[50], buf2[50], buf3[50], time[40];

	if (s) {
		if (s->link == 0)
			*array++ = lamp_getforlist(0);
		else if(s->link < 5)
			*array++ = lamp_getforlist(1);
		else if(s->link < 8)
			*array++ = lamp_getforlist(2);
		else
			*array++ = lamp_getforlist(3);

		if (prf->fullpath)
			*array++ = s->title;
		else
			*array++ = nap_strippath(s->title);

		sprintf(buf,"\33c%d",s->bit);
		*array++ = buf;
		sprintf(buf2,"\33c%d",s->freq);
		*array++ = buf2;
		sprintf(buf3,"\33r%ld",s->size);
		*array++ = buf3;

		if (s->time>0) {
			int secs = s->size/(s->bit*125);
			if(abs(s->time - secs)*100/s->time > 3)
				sprintf(time,"\33r\33i%ld:%02ld",s->time/60,s->time%60);
			else
				sprintf(time,"\33r%ld:%02ld",s->time/60,s->time%60);
		}
		else {
			sprintf(time,"\33r0:00");
		}
		*array++ = time;

		*array++ = s->user;
		*array = nap_linktype[s->link];
	}
	else {
		*array++ = "\33c@";
		*array++ = (char *)MSG_LH_SONG;
		*array++ = (char *)MSG_LH_KBPS;
		*array++ = (char *)MSG_LH_HZ;
		*array++ = (char *)MSG_LH_SIZE;
		*array++ = (char *)MSG_LH_TIME;
		*array++ = (char *)MSG_LH_USER;
		*array = (char *)MSG_LH_LINK;
	}
	return(0);
}


void resultview_clear(struct resultdata *data, int t)
{
	u_long item;

	switch(t) {
		case CLEAR_MARKED:
		{
			u_long listid = MUIV_NList_NextSelected_Start;

			for(;;) {
				DoMethod(data->list,MUIM_NList_NextSelected,&listid);
				if(listid==MUIV_NList_NextSelected_End) break;
				DoMethod(data->list,MUIM_NList_GetEntry,listid,&item);
				if(item) {
					DoMethod(data->list,MUIM_NList_Remove,MUIV_NList_Remove_Selected);
					nap_songfree((song)item);
				}
			}
			break;
		}

		case CLEAR_ALL:
			set(data->list,MUIA_NList_Quiet, MUIV_NList_Quiet_Visual);
			while(1) {
				DoMethod(data->list,MUIM_NList_GetEntry,0,&item);
				if(!item) break;
				DoMethod(data->list,MUIM_NList_Remove,MUIV_NList_Remove_First);
				nap_songfree((song)item);
			}
			set(data->list,MUIA_NList_Quiet, MUIV_NList_Quiet_None);
			resultview_stat(data, 0);
			break;
	}
}


void resultview_download(struct resultdata *data, int t)
{
	int f=0;
	u_long item, listid;

	if (!gui_napon) return;

	switch (t) {
		case 0:
			listid = MUIV_NList_NextSelected_Start;
			for (;;) {
				DoMethod(data->list, MUIM_NList_NextSelected, &listid);
				if (listid == MUIV_NList_NextSelected_End) break;
				DoMethod(data->list, MUIM_NList_GetEntry, listid, &item);
				if (item) {
					if (!f) {
						DoMethod(gui->iconpanel, PANEL_OPENDL);
						f=1;
					}
					dl_addq((song)item);
				}
			}
			break;

		case 1:
			GetAttr(MUIA_NList_DoubleClick, data->list, &listid);
			if (listid == -1 || listid == -2) return;
			DoMethod(data->list, MUIM_NList_GetEntry, listid, &item);
			if (!item) return;
			if (!f) {
				DoMethod(gui->iconpanel, PANEL_OPENDL);
				f=1;
			}
			dl_addq((song)item);
			break;

		case 2:
			DoMethod(data->list, MUIM_NList_GetEntry, MUIV_NList_GetEntry_Active, &item);
			if (!item) return;
gui_debugf("Resume attempted for: %s", ((song)item)->title);
			break;
	}
}


void resultview_nick(struct resultdata *data, char *nick)
{
	if (!gui_napon) return;

	data->resultview_count = 0;
	if (prf->clrlist) resultview_clear(data, CLEAR_ALL);
	nap_sendbuf(NAPC_BROWSEUSER, nick);
	resultview_stat(data, 3);
}


void resultview_stat(struct resultdata *data, int t)
{
	static char buf[80];
	char *txt;

	switch(t) {
		case 0:
			txt = "";
			data->resultview_state = 0;
			break;

		case 1:
			data->resultview_state = 1;
			txt = (char *)MSG_STATUS1_SEARCHING;
			break;

		case 2:
			if (data->resultview_count == 0)
				txt = (char*)MSG_STATUS1_NOFILES;
			else if (data->resultview_count == 1)
				txt = (char*)MSG_STATUS1_ONEFOUND;
			else {
				sprintf(buf, MSG_STATUS1_FOUND, data->resultview_count);
				txt = buf;
			}
			data->resultview_state = 2;
			break;

		case 3:
			txt = (char *)MSG_STATUS1_REQLIST;
			break;
	}

	set(data->result, MUIA_Text_Contents, txt);
}


void resultview_found(struct resultdata *data, song s)
{
	if (s) {
		DoMethod(data->list, MUIM_NList_InsertSingle, s, MUIV_NList_Insert_Sorted);
		data->resultview_count++;
	}
	else {
		resultview_stat(data, 2);
	}
}


MUIF resultlistcomp(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_CompareMessage *ncm)
{
	song entry1 = ncm->entry1;
	song entry2 = ncm->entry2;
	LONG col1 = ncm->sort_type & MUIV_NList_TitleMark_ColMask;
	LONG col2 = ncm->sort_type2 & MUIV_NList_TitleMark2_ColMask;
	ULONG result = 0;

	if (ncm->sort_type == MUIV_NList_SortType_None) return (0);

	if (col1 == 1) {
		if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask) {
			if (prf->fullpath) result = (LONG) stricmp(entry2->title, entry1->title);
			else result = (LONG) stricmp(nap_strippath(entry2->title), nap_strippath(entry1->title));
		}
		else {
			if (prf->fullpath) result = (LONG) stricmp(entry1->title, entry2->title);
			else result = (LONG) stricmp(nap_strippath(entry1->title), nap_strippath(entry2->title));
		}
	}
	else if (col1 == 2) {
		if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
			result = entry2->bit - entry1->bit;
		else
			result = entry1->bit - entry2->bit;
	}
	else if (col1 == 3) {
		if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
			result = entry2->freq - entry1->freq;
		else
			result = entry1->freq - entry2->freq;
	}
	else if (col1 == 4) {
		if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
			result = entry2->size - entry1->size;
		else
			result = entry1->size - entry2->size;
	}
	else if (col1 == 5) {
		if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
			result = entry2->time - entry1->time;
		else
			result = entry1->time - entry2->time;
	}
	else if (col1 == 6) {
		if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
			result = (LONG) stricmp(entry2->user, entry1->user);
		else
			result = (LONG) stricmp(entry1->user, entry2->user);
	}
	else if (col1 == 7) {
		if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
			result = entry2->link - entry1->link;
		else
			result = entry1->link - entry2->link;
	}

	if ((result != 0) || (col1 == col2)) return (result);

	if (col2 == 1) {
		if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask) {
			if (prf->fullpath) result = (LONG) stricmp(entry2->title, entry1->title);
			else result = (LONG) stricmp(nap_strippath(entry2->title), nap_strippath(entry1->title));
		}
		else {
			if (prf->fullpath) result = (LONG) stricmp(entry1->title, entry2->title);
			else result = (LONG) stricmp(nap_strippath(entry1->title), nap_strippath(entry2->title));
		}
	}
	else if (col2 == 2) {
		if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
			result = entry2->bit - entry1->bit;
		else
			result = entry1->bit - entry2->bit;
	}
	else if (col2 == 3) {
		if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
			result = entry2->freq - entry1->freq;
		else
			result = entry1->freq - entry2->freq;
	}
	else if (col2 == 4) {
		if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
			result = entry2->size - entry1->size;
		else
			result = entry1->size - entry2->size;
	}
	else if (col2 == 5) {
		if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
			result = entry2->time - entry1->time;
		else
			result = entry1->time - entry2->time;
	}
	else if (col2 == 6) {
		if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
			result = (LONG) stricmp(entry2->user, entry1->user);
		else
			result = (LONG) stricmp(entry1->user, entry2->user);
	}
	else if (col2 == 7) {
		if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
			result = entry2->link - entry1->link;
		else
			result = entry1->link - entry2->link;
	}

	return (result);
}
