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

QScopedPointer<FileConfigDecode> FileConfigDecode::m_instance;

FileConfigDecode::FileConfigDecode()
{
    // if (m_instance.isNull()) {
    //     m_instance.reset(new FileConfigDecode);

    // }
    initFile();
}

FileConfigDecode *FileConfigDecode::getInstance()
{
    if (m_instance.isNull()) {
        m_instance.reset(new FileConfigDecode);

    }

    return m_instance.data();
}

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;
}

QString FileConfigDecode::getUserName()
{
    return userName;
}

QString FileConfigDecode::getPassword()
{
    return password;
}

RenameType FileConfigDecode::getRenameType()
{
    return renameType;
}

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();
//    updateFile();
}

void FileConfigDecode::setRename(bool value)
{
    this->rename = value;
    if (value == true) {
        renameType = (renameType == RENAME_TYPE_TIME) ? RENAME_TYPE_TIME : RENAME_TYPE_NORMAL;
    }
    else {
        renameType = (renameType == RENAME_TYPE_TIME) ? RENAME_TYPE_TIME : RENAME_TYPE_NORMAL;
    }
    qDebug() << renameType;
    updateFile();
//    updateFile();
}

void FileConfigDecode::setTimeRename(bool value, int flag)
{
    this->timeRename = value;
    if (value) {
        renameType = RENAME_TYPE_TIME;
    }

    if (!value && flag == 1) {
        renameType = RENAME_TYPE_NORMAL;
    }

    if (!value && flag == 0) {
        renameType = RENAME_TYPE_NONE;
    }
    qDebug() << renameType;
    updateFile();
//    updateFile();
}

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

void FileConfigDecode::setUserName(QString username)
{
    this->userName = username;
    updateFile();
}

void FileConfigDecode::setPassword(QString password)
{
    this->password = password;
    updateFile();
}

void FileConfigDecode::fileWrite(QString path, QString address, bool autosetup,
                                 bool rename, bool timerename, bool autocopy, QString username, QString password)
{
#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->setValue("username", username);
    config->setValue("password", password);
    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();
        this->userName = config->value(section+"username").toString();
        this->password = config->value(section+"password").toString();

        checkConfig();

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

        this->address = "127.0.0.1:8080";
        this->autoSetup = false;
        this->rename = false;
        this->timeRename = false;
        this->autoCopy = false;
        this->userName = "admin";
        this->password = "admin";

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

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

    if (rename)
        renameType = RENAME_TYPE_NORMAL;

    if (timeRename)
        renameType = RENAME_TYPE_TIME;
    qDebug() << renameType;
}

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

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

