#include "tn_adplug.h"
#include <string.h>

#define AUDIO_BUFFER_SIZE 4096

const char * const extensions[] = {
	"a2m", "adl", "adlib", "agd", "amd", "bam", "cff", "cmf", "d00", "d01",
	"dfm", "dmo", "dro", "dtl", "dtm", "edl", "fmk", "hsc", "hsp", "imf",
	"ksm", "laa", "lds", "m", "mad", "mid", "mkj", "msc", "mtk", "mtr", "mus",
	"rad", "raw", "rix", "rol", "s3m", "sat", "sa2", "sci", "sdb", "sng",
	"sqx", "xad", "xsm", "wlf", NULL
};

int32 TNPlug_TestPlayer (struct TNPlugIFace *Self, uint8 *testme, uint8 *testbuffer,
	uint32 totalsize, uint32 testsize)
{
	const char *ext;
	ext = strrchr((char *)testme, '.');
	if (ext++) {
		const char * const *cmp = extensions;
		while (*cmp) {
			if (!stricmp(ext, *cmp++)) {
				return PLM_File;
			}
		}
	}
	return 0;
}

BOOL TNPlug_OpenPlayer (struct TNPlugIFace *Self, uint8 *openme, struct TuneNet *inTuneNet) {
	BOOL res = FALSE;
	BPTR seglist;

	if (inTuneNet->playmode == PLM_File) {
		struct TNDecHandle *handle;

		dbug(("creating handle\n"));
		handle = IExec->AllocSysObjectTags(ASOT_MESSAGE,
			ASOMSG_Name,		Self->Data.LibBase->lib_Node.ln_Name,
			ASOMSG_Size,		sizeof(*handle),
			TAG_END);
		inTuneNet->handle = handle;
		if (!handle) goto out;
		memset(&handle->msg + 1, 0, sizeof(*handle) - sizeof(handle->msg));
		handle->player_task = NULL;

		dbug(("creating port\n"));
		handle->player_port = IExec->AllocSysObjectTags(ASOT_PORT,
			ASOPORT_Signal,		SIGB_CHILD,
			TAG_END);
		if (!handle->player_port) goto out;
		handle->msg.mn_ReplyPort = handle->player_port;

		handle->filename = IUtility->ASPrintf("%s", openme);
		if (!handle->filename) goto out;

		handle->pcm = (int16 *)IExec->AllocVec(AUDIO_BUFFER_SIZE, MEMF_SHARED);
		if (!handle->pcm) goto out;
		handle->pcm_size = AUDIO_BUFFER_SIZE >> 2;
		inTuneNet->pcm[0] = handle->pcm;
		inTuneNet->pcm[1] = inTuneNet->pcm[0] + 1;

		seglist = IDOS->LoadSeg("PROGDIR:Plugins/data/TN_AdPlug.bin");
		if (!seglist) goto out;

		dbug(("creating subprocess\n"));
		handle->player_task = (struct Task *)IDOS->CreateNewProcTags(
			NP_Name,					"TN_AdPlug.bin",
			NP_Input,					0,
			NP_Output,					0,
			NP_Error,					0,
			NP_Seglist,					seglist,
			NP_FreeSeglist,				TRUE,
			NP_Priority,				1,
			NP_NotifyOnDeathMessage,	handle,
			TAG_END);
		if (!handle->player_task) {
			IDOS->UnLoadSeg(seglist);
			goto out;
		}

		dbug(("sending message\n"));
		IExec->PutMsg(&((struct Process *)handle->player_task)->pr_MsgPort, &handle->msg);
		dbug(("waiting for reply\n"));
		IExec->WaitPort(handle->player_port);
		handle = (struct TNDecHandle *)IExec->GetMsg(handle->player_port);
		IExec->SetSignal(0, SIGF_CHILD);
		dbug(("reply received\n"));

		if (handle->retcode) {
			handle->player_task = NULL;
			goto out;
		}

		res = TRUE;
out:
		if (res) {
			inTuneNet->DirectRender = ARender_InternalBuffer;
			inTuneNet->max_subsongs =
			inTuneNet->current_subsong = 0;

			inTuneNet->dec_frequency = 44100;
			inTuneNet->dec_channels = 2;
			inTuneNet->bitrate = 0;
			inTuneNet->mix_lr = TN_STEREO_50PER;

			inTuneNet->Tune[0] = 0;

			inTuneNet->ms_duration = handle->length_ms;

			inTuneNet->ST_Name[0] = 0;
			inTuneNet->ST_Url[0] = 0;
			inTuneNet->ST_Note[0] = 0;

			inTuneNet->songEOF = TRUE;
		} else {
			TNPlug_ClosePlayer(Self, inTuneNet);
		}
	}

	return res;
}

void TNPlug_ClosePlayer (struct TNPlugIFace *Self, struct TuneNet *inTuneNet) {
	struct TNDecHandle *handle = (struct TNDecHandle *)inTuneNet->handle;
	if (handle) {
		if (handle->player_task) {
			handle->player_port->mp_SigTask = IExec->FindTask(NULL);
			IExec->Signal(handle->player_task, handle->endsig);
			IExec->WaitPort(handle->player_port);
			IExec->GetMsg(handle->player_port);
			IExec->SetSignal(0, SIGF_CHILD);
			handle->player_task = NULL;
		}
		IExec->FreeVec(handle->pcm);
		IExec->FreeVec(handle->filename);
		IExec->FreeSysObject(ASOT_PORT, handle->player_port);
		IExec->FreeSysObject(ASOT_MESSAGE, handle);
		inTuneNet->handle = NULL;
	}
	inTuneNet->pcm[0] =
	inTuneNet->pcm[1] = NULL;
}
