/*
** Amster - Search
** Copyright © 1999-2000 by Gürer Özen & Jacob Laursen
**
** Maintained by Jacob Laursen <laursen@myself.com>
**
** 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
*/

#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 <MUI/textinput_mcc.h>

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

ULONG search_new(struct IClass *cl, Object *obj, struct opSet *msg);
void search_go(struct searchdata *data, struct SearchArgs *RexxArgs);
void search_reset(struct searchdata *data);
void search_set(struct searchdata *data);
void search_use(struct searchdata *data);

int search_state = 0;


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

	switch (msg->MethodID) {
		case OM_NEW:
			return(search_new(cl,obj,(APTR)msg));
		case SEARCH_GO:
			data = INST_DATA(cl, obj);
			search_go(data, (struct SearchArgs *)(((muimsg)msg)->arg1));
			return NULL;
		case SEARCH_NICK:
			DoMethod(gui->WI_Hotlist, HOTLIST_NICK, (char *)(((muimsg)msg)->arg1));
			return NULL;
		case SEARCH_FOUND:
			data = INST_DATA(cl, obj);
			DoMethod(data->LV_Search, RESULTVIEW_FOUND, (song)(((muimsg)msg)->arg1));
			return NULL;
		case SEARCH_CLEAR:
			data = INST_DATA(cl, obj);
			DoMethod(data->LV_Search, RESULTVIEW_CLEAR, (((muimsg)msg)->arg1));
			return NULL;
		case SEARCH_RESET:
			data = INST_DATA(cl, obj);
			search_reset(data);
			return NULL;
		case SEARCH_SET:
			data = INST_DATA(cl, obj);
			search_set(data);
			return NULL;
		case SEARCH_GET:
			data = INST_DATA(cl, obj);
			search_use(data);
			return NULL;
		case SEARCH_USE:
			data = INST_DATA(cl, obj);
			search_use(data);
			prf_save("ENV:"AMSTER_PREFS);
			DoMethod(data->pop, MUIM_Popstring_Close, TRUE);
			return NULL;
		case SEARCH_GETSTATE:
			data = INST_DATA(cl, obj);
			DoMethod(data->LV_Search, RESULTVIEW_GETSTATE);
			return NULL;
		case SEARCH_TOGGLE:
			data = INST_DATA(cl, obj);
			DoMethod(data->LV_Search, RESULTVIEW_TOGGLE_ONE);
			return NULL;
		case SEARCH_FILLSTEM:
			data = INST_DATA(cl, obj);
			DoMethod(data->LV_Search, RESULTVIEW_FILLSTEM, (((muimsg)msg)->arg1));
			return NULL;
		case SEARCH_DOWNLOAD:
			data = INST_DATA(cl, obj);
			DoMethod(data->LV_Search, RESULTVIEW_DOWNLOAD, 3, (((muimsg)msg)->arg1));
			return NULL;
		case SEARCH_ADDTOHOTLIST:
			data = INST_DATA(cl, obj);
			DoMethod(data->LV_Search, RESULTVIEW_ADDTOHOTLIST);
			return NULL;
		case SEARCH_NICKFROMLIST:
			data = INST_DATA(cl, obj);
			DoMethod(data->LV_Search, RESULTVIEW_NICKFROMLIST);
			return NULL;
	}
	return(DoSuperMethodA(cl, obj, msg));
}


ULONG search_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	static char *numbers = "0123456789";
	static char *opts[] = {
		(char*)_MSG_COMPARE_NONE,
		(char*)_MSG_COMPARE_ATLEAST,
		(char*)_MSG_COMPARE_EQUALTO,
		(char*)_MSG_COMPARE_ATBEST,
		NULL
	};
	struct searchdata *data;
	Object *str, *maxstr, *sbut;
	Object *pop;
	Object *popspeed, *popspeedval, *popbit, *popbitval, *popfreq, *popfreqval;
	Object *popuse, *popreset;
	Object *LV_Search;

	if (UseLamps || UseTWF) gColFormat = "COL=0 BAR, COL=1 BAR, COL=2 BAR, COL=3 BAR, COL=4 BAR, COL=5 BAR, COL=6 BAR, COL=7 BAR";
	else gColFormat = "COL=1 BAR, COL=2 BAR, COL=3 BAR, COL=4 BAR, COL=5 BAR, COL=6 BAR, COL=7 BAR";

	if (opts[0] == (char *)_MSG_COMPARE_NONE)
		localize_array(opts);

	if (obj = (Object *)DoSuperNew(cl,obj,
		Child, VGroup,
			Child, ColGroup(2),
				Child, Label2(MSG_SEARCH),
				Child, HGroup,
					Child, str = TextinputObject,
						StringFrame,
                        MUIA_Textinput_Multiline, FALSE,
						MUIA_HorizWeight, 400,
						MUIA_String_MaxLen, 80,
						MUIA_CycleChain, 1,
					End,
					Child, sbut = SimpleButton(MSG_SEARCH_GAD),
					Child, Label2(MSG_MAX),
					Child, maxstr = TextinputObject,
						StringFrame,
                        MUIA_Textinput_Multiline, FALSE,
						MUIA_String_Accept, numbers,
						MUIA_String_Integer, 100,
						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,
									MUIA_CycleChain, 1,
								End,
								Child, popspeedval = CycleObject,
									MUIA_Cycle_Active, 1,
									MUIA_Cycle_Entries, nap_linktype,
									MUIA_CycleChain, 1,
								End,
								Child, Label2(MSG_BITRATE),
								Child, popbit = CycleObject,
									MUIA_Cycle_Active, 0,
									MUIA_Cycle_Entries, opts,
									MUIA_CycleChain, 1,
								End,
								Child, HGroup,
									Child, popbitval = TextinputObject,
										StringFrame,
                                        MUIA_Textinput_Multiline, FALSE,
										MUIA_String_Accept, numbers,
										MUIA_String_Integer, 128,
										MUIA_String_MaxLen, 4,
										MUIA_CycleChain, 1,
									End,
									Child, Label2(MSG_KBPS),
								End,
								Child, Label2(MSG_FREQUENCY),
								Child, popfreq = CycleObject,
									MUIA_Cycle_Active, 0,
									MUIA_Cycle_Entries, opts,
									MUIA_CycleChain, 1,
								End,
								Child, HGroup,
									Child, popfreqval = TextinputObject,
										StringFrame,
                                        MUIA_Textinput_Multiline, FALSE,
										MUIA_String_Accept, numbers,
										MUIA_String_Integer, 44100,
										MUIA_String_MaxLen, 6,
										MUIA_CycleChain, 1,
									End,
									Child, Label2(MSG_HZ),
								End,
							End,
							Child, HGroup,
								Child, popuse = SimpleButton(MSG_ADVUSE_GAD),
								Child, HSpace(0),
								Child, popreset = SimpleButton(MSG_ADVRESET_GAD),
							End,
						End,
					End,
				End,
			End,
			Child, LV_Search = NewObject(gui->resultview_mcc->mcc_Class, NULL, MUIA_Amster_ContextMenu, TRUE, TAG_DONE),
		End,
		TAG_MORE, msg->ops_AttrList))
	{
		data = INST_DATA(cl, obj);
		data->str = str;
		data->maxstr = maxstr;
		data->pop = pop;
		data->popspeed = popspeed;
		data->popspeedval = popspeedval;
		data->popbit = popbit;
		data->popbitval = popbitval;
		data->popfreq = popfreq;
		data->popfreqval = popfreqval;
		data->LV_Search = LV_Search;

		DoMethod(str,   MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime, obj, 2, SEARCH_GO, NULL);
		DoMethod(sbut,  MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, SEARCH_GO, NULL);

		DoMethod(popuse,   MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, SEARCH_USE  );
		DoMethod(popreset, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, SEARCH_RESET);

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


void search_go(struct searchdata *data, struct SearchArgs *RexxArgs)
{
	static char *opts[] = {
		"AT LEAST",
		"EQUAL TO",
		"AT BEST"
	};
	char *p;
	u_long String, MaxResults;
	u_long tmp, tmp2;

	if (gui_onlinestate < ONLINE) return;

	if (RexxArgs) {
		String = (u_long)RexxArgs->String;

		if (RexxArgs->MaxResults != -1) MaxResults = (u_long)RexxArgs->MaxResults;
		else GetAttr(MUIA_String_Integer, data->maxstr, &MaxResults);
	}
	else {
		GetAttr(MUIA_String_Contents, data->str, &String);
		GetAttr(MUIA_String_Integer, data->maxstr, &MaxResults);
	}

	if (!String || ((char *)String)[0] == 0) return;
	if (MaxResults < 0) MaxResults = 0;

	p = nap_buf + sprintf(nap_buf, "FILENAME CONTAINS \"%s\" MAX_RESULTS %d", String, MaxResults);

	GetAttr(MUIA_Cycle_Active, data->popspeed, &tmp);
	GetAttr(MUIA_Cycle_Active, data->popspeedval, &tmp2);
	if(tmp) p=p+sprintf(p, " LINESPEED \"%s\" %d", opts[tmp-1], tmp2);

	GetAttr(MUIA_Cycle_Active, data->popbit, &tmp);
	GetAttr(MUIA_String_Integer, data->popbitval, &tmp2);
	if(tmp) p=p+sprintf(p, " BITRATE \"%s\" %d", opts[tmp-1], tmp2);

	GetAttr(MUIA_Cycle_Active, data->popfreq, &tmp);
	GetAttr(MUIA_String_Integer, data->popfreqval, &tmp2);
	if(tmp) sprintf(p, " FREQ \"%s\" %d", opts[tmp-1], tmp2);

	if (prf->clrlist) DoMethod(data->LV_Search, RESULTVIEW_CLEAR, CLEAR_ALL);
	DoMethod(data->LV_Search, RESULTVIEW_RESETCOUNT);

	DoMethod(data->LV_Search, RESULTVIEW_STAT2, 1);
	nap_send(NAPC_SEARCH);
}


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);
}


void search_set(struct searchdata *data)
{
	set(data->maxstr,      MUIA_String_Integer, prf->max);
	set(data->popspeed,    MUIA_Cycle_Active,   prf->speedqual);
	set(data->popspeedval, MUIA_Cycle_Active,   prf->speedval);
	set(data->popbit,      MUIA_Cycle_Active,   prf->bitratequal);
	set(data->popbitval,   MUIA_String_Integer, prf->bitrateval);
	set(data->popfreq,     MUIA_Cycle_Active,   prf->freqqual);
	set(data->popfreqval,  MUIA_String_Integer, prf->freqval);
}


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

	GetAttr(MUIA_Cycle_Active,data->popspeed,&tmp);
	prf->speedqual = tmp;

	GetAttr(MUIA_Cycle_Active,data->popspeedval,&tmp);
	prf->speedval = tmp;

	GetAttr(MUIA_Cycle_Active,data->popbit,&tmp);
	prf->bitratequal = tmp;

	GetAttr(MUIA_String_Integer,data->popbitval,&tmp);
	prf->bitrateval = (int)tmp;

	GetAttr(MUIA_Cycle_Active,data->popfreq,&tmp);
	prf->freqqual = tmp;

	GetAttr(MUIA_String_Integer,data->popfreqval,&tmp);
	prf->freqval = (int)tmp;

	GetAttr(MUIA_String_Integer,data->maxstr,&tmp);
	prf->max = (int)tmp;
}
