#ifndef HISTORYDB_H
#define HISTORYDB_H

#include <QString>
#include <QDateTime>
#include <QList>

//***********************************************************************
// historyDB
//***********************************************************************

#define DB_FOLDER_NAME  "history"
#define DB_FILE_NAME    "db_history.ini"

#define db_error_no_error_str       "no error"
#define db_error_format_file_str    "error in file format"
#define db_error_generic_str        "generic error"
#define db_error_erase_str          "error in erase function"
#define db_error_program_str        "error in program function"
#define db_error_tftp_str           "tftp error"
#define db_error_srio_str           "srio error"

//error during program if present
enum db_error_t
{
    db_error_no_error = 0,
    db_error_format_file,
    db_error_generic,
    db_error_erase,
    db_error_program,
    db_error_tftp,
    db_error_srio
};

#define db_status_error_str         "error"
#define db_status_nc_str            "not specified"
#define db_status_started_str       "started"
#define db_status_erased_str        "erased"
#define db_status_programmed_str    "programmed"

//status of ...
enum db_status_t
{
    db_status_error = -1,
    db_status_nc = 0,
    db_status_started,
    db_status_erased,
    db_status_programmed
};

#define spi_port_primary_str        "primary"
#define spi_port_secondary_str      "secondary"


//struct of single histiry item
struct db_item_t
{
    QString path;           //path of the file
    QString name;           //file name
    QString timestamp;      //when i start to program
    db_error_t error;       //error during program if present
    QString where;          //where it is loaded
    db_status_t status;     //status of ...
    QString flash_address;  //flash address to program
    QString srio_conn;      //connection
    unsigned int spi;       //spi port = 0 primary, 1 secondary
    QString errorStr;       //string of error, "" if no error
    QString fileLog;        //file name for log
};

class dbHistory
{
    public:
        static dbHistory *instance();

        bool readHistory();

        bool addNewFile(QString _file,
                        db_error_t _error,
                        QString _where,
                        db_status_t _status=db_status_nc,
                        QString _address = "",
                        QString _srio = "",
                        unsigned int _spi=0);

        void setParam(QString _host);

        void refreshList();

        int getNumEntry();

        db_item_t getEntry(int idx);

        bool changeStatusLast(db_status_t newStatus);

        bool changeErrorLast(db_error_t newError, QString _errorstr="");

        QString getStatusString(db_status_t _status);

        QString getErrorString(db_error_t _error);

        QString getSPIString(unsigned int _spi);

        QString getFileDB();

        void reset();

        void saveLog();

    private:
        static dbHistory* s_instance;
        dbHistory();

        QString hostname;   //hostname
        QString history_folder;     //folder history
        QString history_filedb;     //db file, complete path

        QList<db_item_t> listDBEntry;

        bool addToDB(db_item_t _new);

        bool updateEntryOnFile(int idx);
};

#endif // HISTORYDB_H
