/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "configmanager.h"
#include <QFile>
#include <QDir>
#include <QDebug>
#include <ukui-log4qt.h>

ConfigManager::ConfigManager(const QString &configFilePath)
{
    // 创建 QSettings 对象，指定 INI 格式
    m_settings = new QSettings(configFilePath, QSettings::IniFormat);

    // 检查文件是否存在
    if (!QFile::exists(configFilePath)) {
        KyInfo() << "配置文件不存在，创建新的配置文件...";
        createDefaultConfig();
    }


    // 从配置文件读取值
    //loadConfig();
}

ConfigManager::~ConfigManager()
{
    delete m_settings;
}

QVariant ConfigManager::getConfigValue(const QString &key, const QVariant &defaultValue)
{
    return m_settings->value(key, defaultValue);  // 获取指定键的值
}

void ConfigManager::updateConfig(const QString &key, const QVariant &value)
{
    m_settings->setValue(key, value);  // 设置新的配置值
    m_settings->sync();  // 强制写入
    KyInfo() << "配置已更新：" << key << "=" << value;
}

void ConfigManager::updateNonCriticalConfig(const QString &key, const QVariant &value) {
    KyInfo() << "updateNonCriticalConfig";
    m_settings->setValue(key, value);  // 设置新的配置值
    KyInfo() << "配置已更新：" << key << "=" << value;
}

void ConfigManager::createDefaultConfig()
{
    m_settings->setValue("is-stroke-word", true);
    m_settings->setValue("history-id", 0);
    m_settings->setValue("win-mode", 2);
    m_settings->setValue("is-side-mode-abreast", false);
    m_settings->setValue("is-window-mode-topping", false);
    m_settings->setValue("window-mode-length", 902);
    m_settings->setValue("window-mode-wide", 568);
    m_settings->setValue("window-mode-x", -1);
    m_settings->setValue("window-mode-y", -1);
    m_settings->setValue("is-first-install", true);
    m_settings->setValue("side-mode-x", -1);
    m_settings->setValue("side-mode-y", -1);
    m_settings->setValue("is-privacy-detection", false);
    m_settings->setValue("is-hide-tray",true);
    m_settings->setValue("is-need-tips",true);
    m_settings->setValue("cur-nlp-model", "");
    m_settings->sync();  // 强制写入
}
