#ifndef __OTX_FLASH_MCDCMDTRANS_HPP__
#define __OTX_FLASH_MCDCMDTRANS_HPP__

#include <iostream>
#include "otx/OTXBasicTypes.hpp"
#include "asam/mcd/MCDEventHandler.hpp"
#include "binary.h"
#include "ECU.hpp"
#include <mutex>
#define MAX_FLASH_FRAME_SIZE 0xFFFFFF00
namespace otx {
	namespace flash {
		class MCDCmdTrans
		{
		public:
			MCDCmdTrans(ECU* ecu);
			MCDCmdTrans(std::string ecuVariantName, std::string logicalLinkName);
			~MCDCmdTrans();

		private:
			string logicalLinkName;
			uint32_t nFrameSize = 0x8000;
			uint64_t nDataLength = 0;
			asam::mcd::MCDSystem* mcdSystem;
			asam::mcd::MCDProject* mcdProject;
			asam::d::MCDInterface* mInterface;
			asam::mcd::MCDLogicalLink* logicalLink = nullptr;;
			asam::d::MCDDiagComPrimitive* hexCop = nullptr;;
			ECU* ecu = NULL;
			int isValid;

		public:
			void delay(int ms);
			unsigned int Calc_Key(uint8_t* a1);
			int radix_hex2Array(const char* hex, int hexLen, uint8_t* outBuffer, int outBufferSize);
			CBinary SeedToKey(CBinary binSeed, CBinary binMask);

		public:
			int32_t getFrameSize();
			CBinary sendCommand(CBinary dSend);
			CBinary sendCommand(uint8_t* inCmd, uint32_t len);
			bool Get_SecurityMask(uint16_t uDOIPAddr, CBinary& binMask);
			CBinary JIDUZY_SendReceiveFrame_7F78(int nMax7F78);
			CBinary JIDUZY_SendReceiveFrame(CBinary& binFrame, int nMax7F78 = 1);
			CBinary SendFrame(CBinary& binSend, int nTryCount = 2);
			CBinary SendFrame_Cmd36(uint8_t* pSendData, int iFrameLen, int nTryCount = 1, int nMax7F78 = 2);

		public:
			void EnterProgramMode();
			bool EnterSystem();
			bool getEcuSignatureMode(bool& isProd);
			bool SecurityAccess(string pinCode, int iLevel = 2);
			bool SecurityAccess_ECOS(string pinCode, int iLevel = 2);
			bool EraseMemory(uint32_t uMem = 0, uint32_t uSize = 0);
			bool RequestDownload(uint32_t formatIdentifer, uint32_t nMemoryAddr, uint64_t nDataLength);
			bool RequestTransferExit();
			bool ActivateSbl(std::string sblAddr);
			bool VerifyAuthenticity(string strKeyInfo, bool isDomain);
			bool FinishProgram();
		private:
		};

	}
}

#endif // __OTX_FLASH_MCDCMDTRANS_HPP__