#ifndef DATEI_H
#define DATEI_H

#include <stdint.h>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include "tslib.h"
#include <QString>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonParseError>

    // create csv file with:
#define FILESEPERATOR       ','

    // pasre csv with:
#define FILESEP1       ','
#define FILESEP2       ';'

#define NEWLINEINFILE       '\n'
#define MAXNUMBEROFSEQUENCES      200
    // only for csv files


// all seting-files located in sudirectory "static machine data - smd"
// all generated files located in sudirectory "dynamic machine data - dmd"

#define FILENAME_COMPORT            "../comport.csv"
#define FILENAME_CONFIG             "/own/work2023/PSA1256ptu5/smd/DC2C_conf.json"
#define FILENAME_DEVICE             "/own/work2023/PSA1256ptu5/smd/DC2C_device.json"
#define FILENAME_CASH              "/own/work2023/PSA1256ptu5/smd/DC2C_cash.json"
#define FILENAME_PRINT             "/own/work2023/PSA1256ptu5/smd/DC2C_print32.json"
#define FILENAME_APSERVCONF        "../APserviceConfig.csv"



// -------------------------------------------------------------------------------------------------
// ------------------------------------------------------ create csv file -------------------------------
// -------------------------------------------------------------------------------------------------


// create array with strings and values (to be written to file)
void csv_startCreatingFile(void);
void csv_addTextToFile(QString myText);
void csv_addIntToFile(int myValue);
void csv_addUintToFile(uint myValue);
void csv_addLongvalToFile(qlonglong myValue);
void csv_addUlongvalToFile(qulonglong myValue);
//void csv_addCurrentTimeToFile(void);
//void csv_addCurrentDateToFile(void);
void csv_addNewlineToFile(void);
QByteArray csv_readbackArray(void);


// -------------------------------------------------------------------------------------------------
// ------------------------------------------------------ parse csv file -------------------------------
// -------------------------------------------------------------------------------------------------


// return number of entries in the just read file (entries are seperated by
// comma or line-feed)
uint32_t csv_nrOfEntriesInFile(QByteArray readFromFile);

// before: QByteArray sourceFile=datei_readFromFile(filename);

QByteArray csv_getOneFileSequence(QByteArray sourceFile, uint32_t sequNr);
// not needed, just for test       // sequNr: 0....(size-1)

// get single entries of of the just read fie:
int csv_getEntryAsInt(QByteArray sourceFile, uint32_t sequNr);
    // sequNr: 0....(size-1)

int32_t csv_getEntryAsLong(QByteArray sourceFile, uint32_t sequNr);
    // sequNr: 0....(size-1)

uint8_t csv_getEntryAsUshort(QByteArray sourceFile, uint32_t sequNr);
    // sequNr: 0....(size-1)

uint16_t csv_getEntryAsUint(QByteArray sourceFile, uint32_t sequNr);
    // sequNr: 0....(size-1)

uint32_t csv_getEntryAsUlong(QByteArray sourceFile, uint32_t sequNr);
    // sequNr: 0....(size-1)

uint64_t csv_getEntryAs2Ulong(QByteArray sourceFile, uint32_t sequNr);
    // sequNr: 0....(size-1)

QString csv_getEntryAsString(QByteArray sourceFile, uint32_t sequNr);
    // sequNr: 0....(size-1)


// -------------------------------------------------------------------------------------------------
// ------------------------------------------------------ create Json Record -------------------------------
// -------------------------------------------------------------------------------------------------

void json_startRecord(void);
    // clear buffer and write opening curly bracket {

void json_enterIntToRecord(QString attribute, ulong i_value);
    // example: "parameter":1234567890

void json_enterTextToRecord(QString attribute, QString txt_value);
    // example: "parameter":"slow"

//void json_addCurrentTimeToRecord(QString attribute);
    // example: if attribute=myTime:  "myTime":"hh_mm_ss"

//void json_addCurrentDateToRecord(QString attribute);
    // example: if attribute=myDate:  "myDate":"dd.mm.yyyy"
    // also / possible as seperator
    // further possible forms:
    // format=  0: dd.mm.yyyy   (deutsch)
    //          1: mm.dd.yyyy   (amerika)
    //          2: yyyy.mm.dd   (Iran, Dubai)
    //          3: dd.yyyy.mm
    //          4: mm.yyyy.dd
    //          5: yyyy.dd.mm

void json_enterArrayToRecord(QString attribute, uint8_t *buf, ulong nrofVals);
    // add array of numbers with "nrofVals" elements

void json_enterStructToRecord(QString attribute);
    // every call must be concluded with an extra "json_finishFile()"
    // example: "sname":{

void json_finishStruct(void);

void json_finishRecord(void);
    // close curly bracket


QString json_readbackRecordStr(void);

QByteArray json_readbackRecordBa(void);


// -------------------------------------------------------------------------------------------------
// ------------------------------------------------------ parse Json file -------------------------------
// -------------------------------------------------------------------------------------------------

// first: QByteArray datei_readFromFile(QString filename);

//void datei_json_readTestFile(QString filename);

int json_nrOfPairsInFile(QByteArray filename);

bool json_exists(QByteArray filename, QString searchForKey);
    // look for "searchForKey" =name of the pair (left of : )
    // retval true if exists

bool json_remove(QByteArray filename, QString searchFor);
    // look for "searchFor" =name of the pair (left of : ) and remove the record from file
    // retval true if removed

QString json_searchForStringInFile(QByteArray filename, QString searchFor);
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String

int json_searchForIntInFile(QByteArray filename, QString searchFor);
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String

//.......................

QJsonObject json_searchForObjectInFile(QByteArray filename, QString searchFor);
    // return an object from the json file

int json_nrOfPairsInObject(QJsonObject objname);

QString json_searchForStringInObject(QJsonObject objname, QString searchFor);
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String

int json_searchForIntInObject(QJsonObject objname, QString searchFor);
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String

//.......................

QJsonArray json_searchForArrayInFile(QByteArray filename, QString searchFor);
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String

int json_nrOfValuesInArray(QJsonArray arrayname);

bool json_getValuesOfArray(QJsonArray arrayname, int *buf, int MaxBufferSize);
    // assuming that the array consists of integers



// -------------------------------------------------------------------------------------------------
// ------------------------------------------------------ read, write, copy files -------------------
// -------------------------------------------------------------------------------------------------

void datei_closeFile(QString filename);

// read content of an exiting file:
QByteArray datei_readFromFile(QString filename);

bool datei_ifFileExists(QString filename);

char datei_writeToFile(QString filename, QByteArray content);
    // retval=0 if successful     1: no write access allowed
    //      2:cannot open to append     3:cannot create new file

bool datei_copyFile(QString currentFileName, QString newFileName);
    // retval=true if successful

bool datei_clearFile(QString filename);
    // retval=true if successful

#endif // DATEI_H