/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * main.c - Sample to demonstrate how to use savedata utility
 *
 * Copyright (c) 2005 weltall (weltall@consoleworld.org)
 *			 (c) 2007 InsertWittyName (tias_dp@hotmail.com)
 * Based on work by Shine
 *
 * $Id: main.c 2364 2008-02-16 22:07:09Z iwn $
 */
#include <pspkernel.h>
#include <pspdisplay.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <pspmoduleinfo.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <pspgum.h>
#include <psputility.h>
#include <pspctrl.h>
#include <oslib/oslib.h>

#define DATABUFFLEN   0x20

char* svdataret;

static int running = 1;

char string[128];

PspUtilitySavedataListSaveNewData newData;

char *titleShow = "New Save";

char nameMultiple[][20] =	// End list with ""
{
 "0000",
 "0001",
 "0002",
 "0003",
 "0004",
 ""
};

PspGeContext __attribute__((aligned(16))) geContext4;

char key[] = "LuaPlayerHMRULES";	// Key to encrypt or decrypt savedata
	      
void initSavedata(SceUtilitySavedataParam * savedata, int mode, char *TITLE1, char *TITLE2, char *DETAILS , char* SVDATA, char* Gamename)
{
	memset(savedata, 0, sizeof(SceUtilitySavedataParam));
	savedata->base.size = sizeof(SceUtilitySavedataParam);

	savedata->base.language = PSP_SYSTEMPARAM_LANGUAGE_ENGLISH;
	savedata->base.buttonSwap = PSP_UTILITY_ACCEPT_CROSS;
	savedata->base.graphicsThread = 0x11;
	savedata->base.accessThread = 0x13;
	savedata->base.fontThread = 0x12;
	savedata->base.soundThread = 0x10;
	if (mode == PSP_UTILITY_SAVEDATA_LISTSAVE)
	{
	savedata->mode = PSP_UTILITY_SAVEDATA_LISTSAVE; //mode;
	}
	if (mode == PSP_UTILITY_SAVEDATA_LISTLOAD)
	{
	savedata->mode = PSP_UTILITY_SAVEDATA_LISTLOAD; //mode;
	}
	savedata->overwrite = 1;
	savedata->focus = PSP_UTILITY_SAVEDATA_FOCUS_LATEST; // Set initial focus to the newest file (for loading)

#if _PSP_FW_VERSION >= 200
	strncpy(savedata->key, key, 16);
#endif

	strcpy(savedata->gameName, Gamename);	// First part of the save name, game identifier name
	strcpy(savedata->saveName, "0000");	// Second part of the save name, save identifier name

	// List of multiple names
	savedata->saveNameList = nameMultiple;

	strcpy(savedata->fileName, "DATA.BIN");	// name of the data file

	// Allocate buffers used to store various parts of the save data
	savedata->dataBuf = malloc(DATABUFFLEN);
	savedata->dataBufSize = DATABUFFLEN;
	savedata->dataSize = DATABUFFLEN;

	// Set some data
	if (mode == PSP_UTILITY_SAVEDATA_LISTSAVE)
	{
	 memset(savedata->dataBuf, 0, DATABUFFLEN);
	 strcpy((char*)savedata->dataBuf, SVDATA);

	strcpy(savedata->sfoParam.title,TITLE1);
	strcpy(savedata->sfoParam.savedataTitle,TITLE2);
	strcpy(savedata->sfoParam.detail,DETAILS);
	savedata->sfoParam.parentalLevel = 1;
	
	// No icon1
	savedata->icon1FileData.buf = NULL;
	savedata->icon1FileData.bufSize = 0;
	savedata->icon1FileData.size = 0;

	savedata->pic1FileData.buf = NULL;//pic1;
	savedata->pic1FileData.bufSize = 0;//size_pic1;
	savedata->pic1FileData.size = 0;//size_pic1;

	savedata->icon0FileData.buf = NULL;//icon0;
	savedata->icon0FileData.bufSize = 0;//size_icon0;
	savedata->icon0FileData.size = 0;//size_icon0;
	
	// No snd0
	savedata->snd0FileData.buf = NULL;
	savedata->snd0FileData.bufSize = 0;
	savedata->snd0FileData.size = 0;

	// Set title
	newData.title = titleShow;

	// Set new data
	savedata->newData = &newData;
	
	savedata->focus = PSP_UTILITY_SAVEDATA_FOCUS_FIRSTEMPTY; // If saving, set inital focus to the first empty slot
	}
}

static void ShowSaveDialog (int mode, char *TITLE1, char *TITLE2, char *DETAILS, char* SVDATA,char* Gamename, OSL_IMAGE *img)
{
	sceGeSaveContext(&geContext4);

	SceUtilitySavedataParam dialog;

    initSavedata(&dialog, mode, TITLE1, TITLE2, DETAILS, SVDATA, Gamename);
	
    sceUtilitySavedataInitStart(&dialog);

    while(running) {
		oslStartDrawing();
		oslClearScreen(0);
		oslDrawImageXY(img, 0, 0);
		switch(sceUtilitySavedataGetStatus()) {
			case PSP_UTILITY_DIALOG_VISIBLE :
				sceUtilitySavedataUpdate(1);
				break;
			case PSP_UTILITY_DIALOG_QUIT :
				sceUtilitySavedataShutdownStart();
				break;
			case PSP_UTILITY_DIALOG_FINISHED :
				sceUtilitySavedataShutdownStart();
				break;	
			case PSP_UTILITY_DIALOG_NONE :
				return;
		}
		oslEndDrawing();
		oslEndFrame();
		oslSyncFrame();
    }
	sceDisplayWaitVblankStart();
	int err;
	err = sceUtilitySavedataGetStatus();
	while(err != 4){
		err = sceUtilitySavedataGetStatus();
		sceUtilitySavedataShutdownStart();
	}
}
char* ShowSaveDialogload (int mode, char *string, char* Gamename, OSL_IMAGE *img)
{
	string = "DATA";
	sceGeSaveContext(&geContext4);

	SceUtilitySavedataParam dialog;

    initSavedata(&dialog, mode, "NULL", "NULL", "NULL", "NULL", Gamename);
	
    sceUtilitySavedataInitStart(&dialog);

    while(running) {
		oslStartDrawing();
		oslClearScreen(0);
		oslDrawImageXY(img, 0, 0);
		switch(sceUtilitySavedataGetStatus()) {
			case PSP_UTILITY_DIALOG_VISIBLE :

				sceUtilitySavedataUpdate(1);
				break;

			case PSP_UTILITY_DIALOG_QUIT :

				sceUtilitySavedataShutdownStart();
				break;
				
			case PSP_UTILITY_DIALOG_FINISHED :
				sprintf(string, "%s", (char*)dialog.dataBuf);
				break;
					
			case PSP_UTILITY_DIALOG_NONE :
				return string;
		}
		oslEndDrawing();
		oslEndFrame();
		oslSyncFrame();
    }
	sceDisplayWaitVblankStart();
	int err;
	err = sceUtilitySavedataGetStatus();
	while(err != 4){
		err = sceUtilitySavedataGetStatus();
		sceUtilitySavedataShutdownStart();
	}
	return string;
}
