/*
** Search
*/

#include "config.h"

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

#include <proto/dos.h>

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

#include "gui.h"
#include "search.h"
#include "napster.h"
#include "download.h"
#include "prefs.h"
#include "amster_Cat.h"

ULONG search_new(struct IClass *cl, Object *obj, struct opSet *msg);
MUIF songlistdisp(REG(a2) char **array, REG(a1) songdata sd);
char *strippath(char *buf);
void search_clear(struct searchdata *data, long t);
void search_download(struct searchdata *data);
void search_go(struct searchdata *data);
void search_nick(struct searchdata *data);
void search_stat(struct searchdata *data, int t);
void search_found(struct searchdata *data, songdata song);
void search_reset(struct searchdata *data);

int search_count = 0;


MUIF search_dispatch(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
{
	struct searchdata *data = INST_DATA(cl,obj);

	switch(msg->MethodID) {
		case OM_NEW: return(search_new(cl,obj,(APTR)msg));
		case SEARCH_CLEAR:
			search_clear(data,(long)(((muimsg)msg)->arg1));
			return(0);
		case SEARCH_DOWNLOAD:
			search_download(data);
			return(0);
		case SEARCH_GO:
			search_go(data);
			return(0);
		case SEARCH_NICK:
			search_nick(data);
			return(0);
		case SEARCH_FOUND:
			search_found(data,(songdata)(((muimsg)msg)->arg1));
			return(0);
		case SEARCH_RESET:
			search_reset(data);
			return(0);
		case SEARCH_REFRESH:
			DoMethod(data->list,MUIM_NList_Redraw,MUIV_NList_Redraw_All);
			return(0);
	}
	return(DoSuperMethodA(cl,obj,msg));
}


ULONG search_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	static char *numbers = "0123456789";
	static char *opts[5];
	static struct Hook songlistdispHook = { {0,0}, &songlistdisp, NULL, NULL };
	struct searchdata *data;
	Object *str,*maxstr,*snick,*sbut,*nbut;
	Object *pop;
	Object *popspeed,*popspeedval,*popbit,*popbitval,*popfreq,*popfreqval;
	Object *popuse,*popreset;
	Object *result,*list;
	Object *rembut,*clrbut,*dlbut;

	opts[0] = (char*)_MSG_COMPARE_NONE,
	opts[1] = (char*)_MSG_COMPARE_ATLEAST,
	opts[2] = (char*)_MSG_COMPARE_EQUALTO,
	opts[3] = (char*)_MSG_COMPARE_ATBEST,
	opts[4] = NULL;
	localize_array(opts);

	if (obj = (Object *)DoSuperNew(cl,obj,
		Child, VGroup,
			Child, HGroup,
				Child, Label2(MSG_SEARCH),
				Child, str = StringObject,
					StringFrame,
					MUIA_HorizWeight, 400,
					MUIA_String_MaxLen, 80,
					MUIA_CycleChain, 1,
				End,
				Child, sbut = SimpleButton(MSG_SEARCH_GAD),
			End,
			Child, HGroup,
				Child, Label2(MSG_SEARCHUSER),
				Child, snick = StringObject,
					StringFrame,
					MUIA_HorizWeight, 400,
					MUIA_String_MaxLen, 20,
					MUIA_CycleChain, 1,
					MUIA_ShortHelp, MSG_SEARCHUSER_HELP,
				End,
				Child, nbut = SimpleButton(MSG_SEARCHUSER_GAD),
				Child, Label2(MSG_MAX),
				Child, maxstr = StringObject,
					StringFrame,
					MUIA_String_Accept, numbers,
					MUIA_String_Integer, 42,
					MUIA_String_MaxLen, 4,
					MUIA_CycleChain, 1,
				End,
				Child, pop = PopobjectObject,
					MUIA_Popstring_Button, PopButton(MUII_PopUp),
					MUIA_Popobject_Object, VGroup,
						MUIA_Frame, MUIV_Frame_PopUp,
						Child, TextObject,
							MUIA_Text_Contents, MSG_ADVSEARCH,
						End,
						Child, ColGroup(3),
							Child, Label2(MSG_LINESPEED),
							Child, popspeed = CycleObject,
								MUIA_Cycle_Active, 0,
								MUIA_Cycle_Entries, opts,
							End,
							Child, popspeedval = CycleObject,
								MUIA_Cycle_Active, 1,
								MUIA_Cycle_Entries, nap_linktype,
							End,
							Child, Label2(MSG_BITRATE),
							Child, popbit = CycleObject,
								MUIA_Cycle_Active, 0,
								MUIA_Cycle_Entries, opts,
							End,
							Child, HGroup,
								Child, popbitval = StringObject,
									StringFrame,
									MUIA_String_Accept, numbers,
									MUIA_String_Integer, 128,
									MUIA_String_MaxLen, 4,
									MUIA_CycleChain, 1,
								End,
								Child, Label2("kbps"),
							End,
							Child, Label2(MSG_FREQUENCY),
							Child, popfreq = CycleObject,
								MUIA_Cycle_Active, 0,
								MUIA_Cycle_Entries, opts,
							End,
							Child, HGroup,
								Child, popfreqval = StringObject,
									StringFrame,
									MUIA_String_Accept, numbers,
									MUIA_String_Integer, 44100,
									MUIA_String_MaxLen, 6,
									MUIA_CycleChain, 1,
								End,
								Child, Label2("kHz"),
							End,
						End,
						Child, HGroup,
							Child, popuse = SimpleButton(MSG_ADVUSE_GAD),
							Child, HSpace(0),
							Child, popreset = SimpleButton(MSG_ADVRESET_GAD),
						End,
					End,
				End,
			End,
			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, "BAR, BAR, BAR, BAR, BAR, BAR",
					MUIA_NList_MultiSelect, MUIV_NList_MultiSelect_Default,
					MUIA_NList_DisplayHook, &songlistdispHook,
					MUIA_CycleChain, 1,
				End,
			End,
			Child, HGroup,
				Child, rembut = SimpleButton(MSG_CLEAR_GAD),
				Child, clrbut = SimpleButton(MSG_CLEARALL_GAD),
				Child, dlbut = SimpleButton(MSG_DOWNLOAD_GAD),
			End,
		End,
		TAG_MORE, msg->ops_AttrList))
	{
		data = INST_DATA(cl,obj);
		data->str = str;
		data->maxstr = maxstr;
		data->snick = snick;
		data->pop = pop;
		data->popspeed = popspeed;
		data->popspeedval = popspeedval;
		data->popbit = popbit;
		data->popbitval = popbitval;
		data->popfreq = popfreq;
		data->popfreqval = popfreqval;
		data->result = result;
		data->list = list;
		data->rembut = rembut;
		data->clrbut = clrbut;
		data->dlbut = dlbut;

		DoMethod(list,MUIM_NList_UseImage,gui->redlamp,1,0);
		DoMethod(list,MUIM_NList_UseImage,gui->yellowlamp,2,0);
		DoMethod(list,MUIM_NList_UseImage,gui->greenlamp,3,0);

		DoMethod(str,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,obj,1,SEARCH_GO);
		DoMethod(sbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,SEARCH_GO);
		DoMethod(snick,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,obj,1,SEARCH_NICK);
		DoMethod(nbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,SEARCH_NICK);

		DoMethod(popuse,MUIM_Notify,MUIA_Pressed,FALSE,pop,2,MUIM_Popstring_Close,TRUE);
		DoMethod(popreset,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,SEARCH_RESET);

		DoMethod(rembut,MUIM_Notify,MUIA_Pressed,FALSE,obj,2,SEARCH_CLEAR,0);
		DoMethod(clrbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,2,SEARCH_CLEAR,1);
		DoMethod(dlbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,SEARCH_DOWNLOAD);

		return((ULONG)obj);
	}
	return(0);
}


MUIF songlistdisp(REG(a2) char **array, REG(a1) songdata sd)
{
	static char buf[50],buf2[50];

	if (sd) {
		if(sd->link < 5)
			*array++ = "\33c\33o[1]";
		else if(sd->link < 8)
			*array++ = "\33c\33o[2]";
		else
			*array++ = "\33c\33o[3]";

		if(pref_fullpath)
			*array++ = sd->title;
		else
			*array++ = strippath(sd->title);

		sprintf(buf,"\33c%d/%d",sd->bitrate,sd->freq);
		*array++ = buf;
		sprintf(buf2,"\33r%ld",sd->size);
		*array++ = buf2;
		*array++ = sd->user;
		*array = nap_getlinktype(sd->link);
	} else {
		*array++ = "\33c@";
		*array++ = (char *)MSG_LISTHEADER_SONG;
		*array++ = "\33ckbps/kHz";
		*array++ = (char *)MSG_LISTHEADER_SIZE;
		*array++ = (char *)MSG_LISTHEADER_USER;
		*array = (char *)MSG_LISTHEADER_LINK;
	}
	return(0);
}


char *strippath(char *buf)
{
	char tmp;
	int i;

	for(i=strlen(buf)-1;i>0;i--) {
		tmp = buf[i];
		if(tmp==':' || tmp=='/' || tmp=='\\') return(buf+i+1);
	}
	return(buf);
}


void search_clear(struct searchdata *data, long t)
{
	u_long item;

	switch(t) {
		case 0:
			DoMethod(data->list,MUIM_NList_GetEntry,MUIV_NList_GetEntry_Active,&item);
			if(item) {
				DoMethod(data->list,MUIM_NList_Remove,MUIV_NList_Remove_Active);
				free((songdata)item);
			}
			break;

		case 1:
			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);
				free((songdata)item);
			}
			set(data->list,MUIA_NList_Quiet, MUIV_NList_Quiet_None);
			break;
	}
}


void search_download(struct searchdata *data)
{
	u_long item;
	long listid;

	if(!gui_napon) return;
	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) nap_request((songdata)item);
	}
}


void search_go(struct searchdata *data)
{
	u_long tmp,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8;

	if(!gui_napon) return;
	GetAttr(MUIA_String_Acknowledge,data->str,&tmp);
	GetAttr(MUIA_String_Integer,data->maxstr,&tmp2);
	GetAttr(MUIA_Cycle_Active,data->popspeed,&tmp3);
	GetAttr(MUIA_Cycle_Active,data->popspeedval,&tmp4);
	GetAttr(MUIA_Cycle_Active,data->popbit,&tmp5);
	GetAttr(MUIA_String_Integer,data->popbitval,&tmp6);
	GetAttr(MUIA_Cycle_Active,data->popfreq,&tmp7);
	GetAttr(MUIA_String_Integer,data->popfreqval,&tmp8);

	if(tmp2<3) tmp2=3;
	if(tmp2>100) tmp2=100;

	if(tmp) {
		search_count=0;
		if(pref_clrlist) search_clear(data,1);
		nap_search((char *)tmp,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8);
		search_stat(data,1);
	}
}


void search_nick(struct searchdata *data)
{
	u_long tmp;

	if(!gui_napon) return;
	GetAttr(MUIA_String_Acknowledge,data->snick,&tmp);
	if(tmp) {
		search_count=0;
		if(pref_clrlist) search_clear(data,1);
		nap_browseuser((char *)tmp);
		search_stat(data,3);
	}
}


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

	switch(t) {
		case 0:
			txt = "";
			break;

		case 1:
			txt = (char*)MSG_STATUS1_SEARCHING;
			break;

		case 2:
			if(search_count==0)
				txt = (char*)MSG_STATUS1_NOFILES;
			else {
				sprintf(buf,MSG_STATUS1_FOUND,search_count);
				txt = buf;
			}
			break;

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

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


void search_found(struct searchdata *data, songdata song)
{
	if(song) {
		DoMethod(data->list,MUIM_NList_InsertSingle,song,MUIV_NList_Insert_Bottom);
		search_count++;
	} else {
		search_stat(data,2);
	}
}


void search_reset(struct searchdata *data)
{
	set(data->popspeed,MUIA_Cycle_Active,0);
	set(data->popspeedval,MUIA_Cycle_Active,1);
	set(data->popbit,MUIA_Cycle_Active,0);
	set(data->popbitval,MUIA_String_Integer,128);
	set(data->popfreq,MUIA_Cycle_Active,0);
	set(data->popfreqval,MUIA_String_Integer,44100);
}
