#pragma once

#include <stm32f4xx_hal.h>

#include <string>
#include <cstring>
//TODO should these be in CPFcpp.hpp?
#include "FIFO.hpp"
#include "SDLogger.hpp"

static UART_HandleTypeDef hUSART6 = UART_HandleTypeDef();

static uint8_t usart6RXBuffer[256];

class SBE41
{
public:
	static SBE41& getInstance(char const* id)
	{
		static SBE41 instance(id);
		cout << "\t\tpublic SBE41CTD getInstance" << endl;
		return instance;
	}
	
private:
	SBE41(char const* id)
	{
		configUART6();
		defineCommands();
		IDTag = id;
		cout << "\t\tSBE41 constructor, qID: " << IDTag << endl;
	}

public:
	enum SBE41Commands
	{
		getFastPress  = 0,
		setCPMode,
	};
	
	int8_t sendCommand(SBE41Commands command, string &returnTimestamp, string &returnMsgString, bool noWait);

protected:

private:
	SDLogger sdLogger = SDLogger::getInstance();		//TODO Why is this a .instance, singleton?

	struct CommandParams
	{
		uint8_t*  cmdPtr;
		uint16_t cmdSize;
		uint16_t responseSize;
		uint32_t cmdTimeout;
		char cmdLabel[16] = "";
		string correctResponse = "";
		
	};
	
	struct CommandParams CmdParam[2];
	void defineCommands()
	{
		//TODO figure out how to make these const
		CmdParam[getFastPress].cmdPtr = (uint8_t *)"fp\r";
		CmdParam[getFastPress].cmdSize = sizeof("fp\r");
		CmdParam[getFastPress].responseSize = 10;
		CmdParam[getFastPress].cmdTimeout = 1000;
		strcpy(CmdParam[getFastPress].cmdLabel, "Send fp");
		CmdParam[getFastPress].correctResponse = "S";
	}
	
	void configUART6();
	int8_t checkRespAndTimeout(string &actualResponse, string &correctResponse);
	int8_t checkTimeout(bool clearCount);
	int8_t checkResponse();
	CommandParams setCmdParams(SBE41::SBE41Commands cmd);

	char const* IDTag;
	int8_t numTimeouts = 0;
}
;
