#include <QFile>
#include <QDebug>
#include <QDateTime>

#include "filemanager.h"

#if 0
namespace FileManager {

Global::ExperimentInfo _expeInfo;
QFile _expeFile;

void writeExperimentFile(const CommonData &cd)
{
    if(!_expeFile.isOpen()){
        qDebug()<<"FileManager,file not open.";

        createExperimentFile();
    }

    QTextStream out(&_expeFile);
    out.setRealNumberPrecision(3); // 设置精度为三位小数
    out.setRealNumberNotation(QTextStream::FixedNotation);

    out << cd.sample_temp << " "
        << cd.dsc << endl;
}

void createExperimentFile()
{
    QString fileName = _expeInfo.sampleName;
    if(fileName.isEmpty()){
        fileName = "sampleName";
    }

    fileName.append("_");
    if(_expeInfo.date.isEmpty()){
        fileName.append(QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss"));
    }else{
        fileName.append(_expeInfo.date);
    }
    fileName.append(".txt");

    qDebug()<<"fileName:"<<fileName;

    _expeFile.setFileName(fileName);

    // 尝试以写入文本模式打开文件，如果文件不存在则创建它
    if (!_expeFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
        qDebug() << "FileManager file open failed.";
    } else {
        qDebug() << "FileManager file open succ.";
    }
}

void test()
{
    createExperimentFile();
    CommonData cd;
    cd.dsc = 1.0;
    writeExperimentFile(cd);
}

void close()
{
    if(_expeFile.isOpen()){
        _expeFile.close();
    }
}

void readExperimentFile(const QString fileName, QVector<Global::ExperimentData> &dataVtr)
{
    if(fileName.isEmpty()){
        return;
    }

    if(!_expeFile.isOpen()){
        _expeFile.close();
    }
    _expeFile.setFileName(fileName);
    // 尝试以写入文本模式打开文件，如果文件不存在则创建它
    if (!_expeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "FileManager file open failed.";
    } else {
        qDebug() << "FileManager file open succ.";
    }
    
    QTextStream in(&_expeFile);
    while (!in.atEnd()) {
        QString line = in.readLine();
        QStringList values = line.split(" ");
        if (values.size() == 2) {
            Global::ExperimentData data;
            data.sampleTemp = values[0].toFloat();
            data.dsc = values[1].toFloat();
            // dataVtr.append(data);
            dataVtr.push_back(data);
        }
    }

    _expeFile.close();
}

void writeFile()
{
    createExperimentFile();
}

}

#endif
