
#include "common/stdafx.h"
#include "common/fs.h"
#include "base/plugins.h"

#include "tinsel/plugin.h"
#include "tinsel/tinsel.h"

using namespace Tinsel;

static const PlainGameDescriptor tinselSettings[] = {
	{"dw1", "Discworld 1" },
	{"dw2", "Discworld 2" },
	{NULL, NULL }
};

// for game detection
static const Tinsel::TinselGameSettings dw_games[] = {
	{ "dw1",    "Discworld 1", "dw.scn" },
	{ "dw1gra", "Discworld 1", "dw.gra"},
	{ "dw2",    "Discworld 2", "dw2.scn" },
	{ NULL,     NULL,          NULL }
};

// for language detection
static const Tinsel::TinselLanguage dw_languages[] = {
	{ Common::EN_USA, "english.txt" },
	{ Common::FR_FRA, "french.txt" },
	{ Common::DE_DEU, "german.txt" },
	{ Common::IT_ITA, "italian.txt" },
	{ Common::ES_ESP, "spanish.txt" },
	{ Common::UNK_LANG, NULL }
};

#if 0
GameList Engine_TINSEL_gameList() {
	GameList games;
	const TinselGameSettings *g = dw_games;

	while(g->name) {
		games.push_back(g->toGameSettings());
		g++;
	}

	return games;
}
#endif

DetectedGameList Engine_TINSEL_detectGames(const FSList &fslist) {
	DetectedGameList detectedGames;
	const TinselGameSettings *g;

	for(g = dw_games; g->name; g++) {
		Common::Language lang = Common::UNK_LANG;

		// version detection
		for(FSList::const_iterator file = fslist.begin();
			file != fslist.end();
			file ++)
		{
			const char *displayname = file->displayName().c_str();

			if(0 == scumm_stricmp(displayname, g->detectfile)) {
				// detect language
				for(const Tinsel::TinselLanguage *l = dw_languages;
					l->language != Common::UNK_LANG;
					l ++)
				{
					for(FSList::const_iterator lfile = fslist.begin();
						lfile != fslist.end();
						lfile ++)
					{
						const char *dname_l = lfile->displayName().c_str();

						if(0 == scumm_stricmp(dname_l, l->detectfile)) {
							lang = l->language;
							break;
						}
					}
				}

				if(lang != Common::UNK_LANG)
				{
					// language known
					detectedGames.push_back(
						DetectedGame(g->toGameSettings(), lang));
				}
				else
				{
					// unknown language
					detectedGames.push_back(g->toGameSettings());
				}
			}
		}
	}

	return detectedGames;
}

GameList Engine_TINSEL_gameIDList() {
	const PlainGameDescriptor *g;
	GameList games;

	for(g = tinselSettings; g->gameid != NULL; g ++) {
		games.push_back(*g);
	}
	return games;
}

GameDescriptor Engine_TINSEL_findGameID(const char *gameid) {
	const PlainGameDescriptor *g;

	for(g = tinselSettings; g->gameid != NULL; g ++) {
		if(0 == scumm_stricmp(gameid, g->gameid)) {
			return *g;
		}
	}
	return GameDescriptor();
}

PluginError Engine_TINSEL_create(OSystem *syst, Engine **engine) {
	assert(engine);

	*engine = new Tinsel::TinselEngine(syst);
	return kNoError;
}

REGISTER_PLUGIN(TINSEL, "Tinsel Engine", "(C) GT Interactive");
