#pragma once
#include <stdint.h>
#include "app_fatfs.h"

static FIL EngFile;

class Logger
{
public:
	Logger()
	{
	}
	
	FRESULT Init()
	{
		FRESULT res;
		FATFS SDFatFs;

		uint8_t workBuffer[_MAX_SS];
		bool makeFS = false;
		
		int32_t linkDriverReturn = -1;
		linkDriverReturn = FATFS_LinkDriver(&SD_Driver, SDPath);
		if (linkDriverReturn != 0)
		{
			//cout << "**ERROR: Couldn't link FATFS Driver return: " << linkDriverReturn << endl;
			return FR_DISK_ERR;
			//TODO process error
		}	
		
		if (makeFS)
		{
			res = f_mkfs(SDPath, FM_ANY, 0, workBuffer, sizeof(workBuffer));
		}

		/* Register the file system object to the FatFs module */
		res = f_mount(&SDFatFs, "", 0);
		return res;
	}
	
	FRESULT openNewFile()
	{
		FRESULT fResult;
		TCHAR fileName[] = "file8.txt";

		/* Create and Open a new text file object with write access */
		fResult = f_open(&EngFile, fileName, FA_CREATE_ALWAYS | FA_WRITE);
		return fResult;
	}
	
	FRESULT closeFile()
	{
		FRESULT fResult;
		fResult = f_close(&SDFile);	

		return fResult;
	}
	
	FRESULT writeToFile()
	{
		FRESULT fResult;
		uint8_t wtext[] = "line8"; /* File write buffer */
		uint32_t byteswritten;

		/* Write data to the text file */
		fResult = f_write(&SDFile, wtext, sizeof(wtext), (UINT*)&byteswritten);
		fResult = f_sync(&SDFile);
	
		return (fResult);
	}
};
