#include "fileconfigdecode.h"
#include <QDebug>

FileConfigDecode::FileConfigDecode()
{
    initFile();
    qDebug() << "read:" << autoSetup << rename << timeRename << autoCopy;
}

FileConfigDecode::~FileConfigDecode()
{
    updateFile();
//    qDebug() << "write:" << autoSetup << rename << timeRename << autoCopy;
}

QString FileConfigDecode::getAddress()
{
    return this->address;
}

quint16 FileConfigDecode::getPort()
{
    return port;
}

bool FileConfigDecode::getAutoSetup()
{
    return autoSetup;
}

bool FileConfigDecode::getRename()
{
    return rename;
}

bool FileConfigDecode::getTimeRename()
{
    return timeRename;
}

bool FileConfigDecode::getAutoCopy()
{
    return autoCopy;
}

void FileConfigDecode::setAddress(QString value)
{
    this->address = value;
//    updateFile();
}

void FileConfigDecode::setPort(quint16 value)
{
    this->port = value;
//    updateFile();
}

void FileConfigDecode::setAutoSetup(bool value)
{
    this->autoSetup = value;
//    updateFile();
}

void FileConfigDecode::setRename(bool value)
{
    this->rename = value;
//    updateFile();
}

void FileConfigDecode::setTimeRename(bool value)
{
    this->timeRename = value;
//    updateFile();
}

void FileConfigDecode::setAutoCopy(bool value)
{
    this->autoCopy = value;
//    updateFile();
}

void FileConfigDecode::fileWrite(QString path, QString address, quint16 port, bool autosetup, bool rename, bool timerename, bool autocopy)
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QSettings* config = new QSettings(path, QSettings::IniFormat);
    config->setIniCodec(QTextCodec::codecForName("utf-8"));
    QString section = QString("config");
    config->beginGroup(section);
    config->setValue("address", address);
    config->setValue("port", port);
    config->setValue("autosetup", autosetup);
    config->setValue("rename", rename);
    config->setValue("timerename", timerename);
    config->setValue("autocopy", autocopy);
    config->endGroup();
    delete config;
#else
    QSettings* config = new QSettings(path, QSettings::IniFormat);

    QString section = QString("config");
    config->beginGroup(section);
    config->setValue("address", address);
    config->setValue("port", port);
    config->setValue("autosetup", autosetup);
    config->setValue("rename", rename);
    config->setValue("timerename", timerename);
    config->setValue("autocopy", autocopy);
    config->endGroup();
    delete config;
#endif
}

void FileConfigDecode::initFile()
{
    QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QString addFilePath = addDataPath + "config.ini";
    qDebug() << addFilePath;
    QFile file(addFilePath);
    if (file.exists()) {
        QSettings* config = new QSettings(addFilePath, QSettings::IniFormat);
        QString section = "config/";

        this->address = config->value(section + "address").toString();
        this->port = config->value(section + "port").toUInt();
        this->autoSetup = config->value(section + "autosetup").toBool();
        this->rename = config->value(section + "rename").toBool();
        this->timeRename = config->value(section + "timerename").toBool();
        this->autoCopy = config->value(section + "autocopy").toBool();

        checkConfig();

        this->firstCreate = false;
    }
    else {
        this->firstCreate = true;

        this->address = "127.0.0.1";
        this->port = 0;
        this->autoSetup = false;
        this->rename = false;
        this->timeRename = false;
        this->autoCopy = false;

        fileWrite(addFilePath, address, port, autoSetup, rename, timeRename, autoCopy);
    }
}

void FileConfigDecode::checkConfig()
{
    if (address.isEmpty()) {
        address = "127.0.0.1";
    }
}

void FileConfigDecode::updateFile()
{
    QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QString addFilePath = addDataPath + "config.ini";

    qDebug() << "write" << autoSetup << rename << timeRename << autoCopy;
    fileWrite(addFilePath, address, port, autoSetup, rename, timeRename, autoCopy);
}

