#pragma once

#include "PFrogConsole.h"
#include <PocketFrog/PocketFrog.h>
#include <stdio.h>


#define MAXLINEBUFFER 1500

using namespace Frog;


class GameSettings  
{

private:
	// file .ini structs
	struct keys {
		char *key, *value;
		struct keys *next;
	};
	struct sections {
		struct keys *key;
		char *section;
		struct sections *next;
	};
	struct sections *headsection;	// the head of sections struct

	// we're in buffered load/save mode
	bool bufferedMode;

	// the file name
	TCHAR filename[255];


	// load file in structures (returns the sections head pointer)
	void loadsettings();
	void savesettings();
	void freestructs();
	void *findsection(char *section);
	void *findkey(char *section, char *key);
	void addvalue(char *section, char *key, char *value);

	// final functions for override functions
	void _SaveSetting(char *section, char *key, char *value);
	char *_LoadSetting(char *section, char *key, char *defvalue, size_t maxsize);

	// crypt key
	unsigned char cryptKey;
	char *StringCrypt(char *str);
	char *StringDecrypt(char *str);

public:
	// constructor / destructor
	GameSettings(LPTSTR Filename, bool saveincurrentdir);
	virtual ~GameSettings();

	// boolean load/save
	void SaveBool(char *section, char *key, bool value);
	bool LoadBool(char *section, char *key, bool defvalue);

	// long load/save
	void SaveLong(char *section, char *key, long value);
	long LoadLong(char *section, char *key, long defvalue);

	// string load/save
	void SaveShortString(char *section, char *key, char *string);
	char *LoadShortString(char *section, char *key, char *defvalue);

	// start/end buffered operation (file will we re-saved at the end of operation and won't be reloaded)
	void StartBufferedOperation();
	void EndBufferedOperation();

	// set crypt key
	void SetCryptKey(unsigned char newkey);
};

