/* in_flac - Winamp2 FLAC input plugin
 * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
 *
 * 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 <windows.h>
#include <stdio.h>

#include "plugin_common/all.h"
#include "FLAC/all.h"


/*
 * Helper functions
 * ANSI -> Unicode
 * Unicode -> ANSI
 */

static int ConvertANSIToUTF8(const char* src,wchar_t* dest){
	return MultiByteToWideChar(CP_ACP,0,src,-1,dest,1024);
}

static int ConvertUTF8ToANSI(const wchar_t* src,char* dest){
	return WideCharToMultiByte(CP_ACP,0,src,-1,dest,1024,NULL,NULL);
}


/*
 * Convert Winamp library style field name to Vorbis comments field
 */

static wchar_t* getVorbisField(const char* WinampField){
	static wchar_t tmpvorbisfield[1024];

	if(!stricmp(WinampField, "track") || !stricmp(WinampField, "tracknumber"))
		ConvertANSIToUTF8("TRACKNUMBER",tmpvorbisfield);
	else
	if(!stricmp(WinampField, "artist"))
		ConvertANSIToUTF8("ARTIST",tmpvorbisfield);
	else
	if(!stricmp(WinampField, "title"))
		ConvertANSIToUTF8("TITLE",tmpvorbisfield);
	else
	if(!stricmp(WinampField, "album"))
		ConvertANSIToUTF8("ALBUM",tmpvorbisfield);
	else
	if(!stricmp(WinampField, "date") || !stricmp(WinampField, "year"))
		ConvertANSIToUTF8("DATE",tmpvorbisfield);
	else
	if(!stricmp(WinampField, "comment"))
		ConvertANSIToUTF8("COMMENT",tmpvorbisfield);
	else
	if(!stricmp(WinampField, "genre"))
		ConvertANSIToUTF8("GENRE",tmpvorbisfield);
	else
		ConvertANSIToUTF8(WinampField,tmpvorbisfield);

	return tmpvorbisfield;
}


/*
 * Get field function
 */

static FLAC_Plugin__CanonicalTag readtag;
static char readfilename[MAX_PATH];

int __declspec(dllexport) winampGetExtendedFileInfo(const char *fn, const char *data, char *dest, int destlen){
	const wchar_t* fieldValue=NULL;
	int length;

	//preliminary checks
	if (!fn || (fn && !*fn) || !destlen)return 0;
	dest[0]='\0';

	//length is special...
	if(!stricmp(data,"length")){
		FLAC__StreamMetadata streaminfo;
		if(FLAC__metadata_get_streaminfo(fn, &streaminfo)){
			length=(int)(streaminfo.data.stream_info.total_samples*10 / (streaminfo.data.stream_info.sample_rate/100));
			itoa(length,dest,10);
			return 1;
		}else{
			return 0;
		}
	}

	//open file if it's a new one
	if(stricmp(fn,readfilename)){
		strcpy(readfilename,fn);
		FLAC_plugin__canonical_tag_init(&readtag);
		FLAC_plugin__vorbiscomment_get(fn,&readtag,NULL);
	}

	//get the fiels and write it
	fieldValue=FLAC_plugin__canonical_get(&readtag,getVorbisField(data));
	if(fieldValue){
		ConvertUTF8ToANSI(fieldValue,dest);
		return 1;
	}

	//if we get here, we failed...
	return 0;
}


/*
 * Set field functions
 */

static FLAC_Plugin__CanonicalTag writetag;
static char writefilename[MAX_PATH];

int __declspec(dllexport) winampSetExtendedFileInfo(const char *fn, const char *data, char *val){
	wchar_t fieldValue[1024];

	//prelimineray checks
	if (!fn || (fn && !*fn))return 0;

	//open file if it's a new one
	if(stricmp(fn,writefilename)){
		strcpy(writefilename,fn);
		FLAC_plugin__canonical_tag_init(&writetag);
		FLAC_plugin__vorbiscomment_get(fn,&writetag,NULL);
	}

	//put new field in tag
	ConvertANSIToUTF8(val,fieldValue);
	if(!stricmp(val,"")){
		FLAC_plugin__canonical_remove(&writetag,fieldValue);
	}else{
		FLAC_plugin__canonical_set(&writetag,getVorbisField(data),fieldValue);
	}

	return 1;
}


int __declspec(dllexport) winampWriteExtendedFileInfo(){
	//prelimineray checks
	if (!writefilename || (writefilename && !*writefilename))return 0;

	//flush tag to file
	return FLAC_plugin__vorbiscomment_set(writefilename,&writetag);
}

