#ifndef _NDS_HEADER_
#define _NDS_HEADER_
//DSTwo resources
#include <ds2_types.h>
#include "../src/Display/Display.h"

#ifdef __cplusplus
extern "C" {
#endif

#define NZIP_VERSION 2

#define NDS_TITLE_LEN 12
#define NDS_ID_LEN 4
#define NDS_BANNER_SIZE 256
#define NDS_ICON_SIZE 136

#define NZIP_RESERVED 12
#define NZIP_ICONSIZE_V1 1024
#define NZIP_ICONPAD 480


#define NDS_ICON_WIDTH 32
#define NDS_ICON_HEIGHT 32

#define BANNER_DATA 0x68
#define TILE_DATA 32 //512 bytes
#define PAL_DATA 128 //32 bytes big
//all 256 bytes
#define LANG_JAP 576
#define LANG_ENG 832
#define LANG_FRE 1088
#define LANG_GER 1344
#define LANG_ITN 1600
#define LANG_SPN 1856


typedef enum
{
	ROM_HB,//homebrew
	ROM_CM,//commercial
}ROMTYPES;

typedef struct _ndshead
{
	char text[NDS_BANNER_SIZE], // English Title
         game_title[NDS_TITLE_LEN],
		 game_code[NDS_ID_LEN + 2];
	int type;
}NDSHEADER __attribute__ ((aligned (4)));


/*=================================================
Nitro Zip
==================================================*/

#define BUFSIZE 131072

typedef enum
{
	NZIP_VER,
	NZIP_HASICON,
	NZIP_ICONTRANS,
}nzipfooter_reserved;

typedef struct _nzipfooter_v1 //nzip version 1
{
	unsigned short Reserved[NZIP_RESERVED>>1];
	NDSHEADER nds;
	unsigned short Icon[NZIP_ICONSIZE_V1];
}NZIPFOOTER_SIZE;

typedef struct _nzipfooter //nzip version 2
{
	unsigned short Reserved[NZIP_RESERVED>>1];
	NDSHEADER nds;
	int *Icon;
}NZIPFOOTER;

extern int BAG_DSZIP_Deflate(const char *inputFilename, const char *outputFilename, int language, char *mode);
extern int BAG_DSZIP_Inflate(const char *inputFilename, const char *outputFilename);
extern int BAG_DSZIP_GetDataSize(const char * filename);
extern void zerr(int ret, FILE * inerr, FILE * outerr);


/*=================================================
Easy API
==================================================*/
struct _ndsrom
{
	FILE *romFile;
	//contains header information of both nzip and rom file
	NZIPFOOTER Header;
	int banner_offset;
	int footerStart;//starting position for the nzipFooter
	
	//for reading portions of the header
	char	(*readTitle)    (struct _ndsrom *);
	char	(*readID)		(struct _ndsrom *);	
	char	(*readBanner)   (struct _ndsrom *, int);

	char	(*createIcon)	(struct _ndsrom *, GFXObj_t *);
	char	(*createIconEx)	(struct _ndsrom *, GFXObj_t *, unsigned short);
	//reading entire header
	char	(*readHeader)	(struct _ndsrom *, GFXObj_t *, int);
	
	void	(*closeRom)		(struct _ndsrom *);
	
	//accessing header info
	char*	(*getID)		(struct _ndsrom *);
	char*  	(*getTitle)		(struct _ndsrom *);
	char*  	(*getBanner)	(struct _ndsrom *);
	int    	(*getType)		(struct _ndsrom *);

	//gzip header access
	char	(*readReserved)	(struct _ndsrom *);
	unsigned short*   (*zipReserved)	(struct _ndsrom *);
};

extern char NDS_OpenFile(const char *filename, struct _ndsrom *Rom);
extern void NDS_CloseFile(struct _ndsrom *Rom);

extern char NZIP_OpenFile(const char *filename, struct _ndsrom *Rom);
extern void NZIP_CloseFile(struct _ndsrom *Rom);

extern char Rom_OpenFile(const char *filename, struct _ndsrom *Rom);
extern void Rom_CloseFile(struct _ndsrom *Rom);
extern char Rom_ReadHeader(struct _ndsrom *Rom, GFXObj_t *Icon, int language);

#ifdef __cplusplus
}
#endif

#endif

