#pragma once

#include "HexRecord.h"
#include <QString>
#include <QList>
#include <QFile>

#define HEX_FILE_HEADER_CODE (int)0xBAADBEEF



class CHexFile
{

	enum eHexFileStatus
	{
		STATUS_OK,
		STATUS_OK_LAST_INDEX,
		ERR_INDEX_OUT_OF_BOUND
	};

public:
	CHexFile(void);
	~CHexFile(void);

    int OpenDataFile(QString FilePath, bool CloseIfAlreadyParsed = true);
    int CloseDataFile();
    int CloseOpenedHexFile();
    int ParseData(void);
	unsigned int GetNBRecords(void);
	unsigned long GetFileSize(void);
    unsigned int GetFirmwareSize(void){return mFirmwareDataSize;}
    unsigned int GetDataCRC32(void);
	bool HexFileLoaded(void){return mFileOpened;}
	bool HexDataValid(void){return mFileParsed;}
    unsigned long GetTotalParsedRecords(void){return mTotalParsedRecords;}
    QByteArray GetRawData(bool IncludeHeader = true);
    int FilterRecords(unsigned int StartAddress, unsigned int EndAddress);



	CHexRecord * GetRecord(int RecordIndex, int &Status); 
	unsigned long mDiscardedRecords;
    unsigned long mTotalParsedRecords;

    QString GetHexFileInfoString();

private:
    QFile *mHexfileHandle;
	bool mFileOpened;
	bool mFileParsed;
	bool mRecordsListValid;
    //CArray<CHexRecord*,CHexRecord*> mRecordsList;
    QList<CHexRecord*> mRecordsList;
//	CHexRecord* mRecordsTable[0x3000];
	unsigned int mRecordsTableSize;
	unsigned int mHighAddress;
	unsigned long mHexFileSize;
    unsigned int mFirmwareDataSize;
    unsigned int mFirmwareCRC;


};
