#include "audioaiassistantsetting.h"
#include "private/eaiexecutor.h"
#include "audiowizard/gui/iatwidget.h"

#include <QSettings>
#include <QApplication>
#include <QThread>
#include <QStandardPaths>
#include <QFileInfo>
#include <QDir>

UOSAI_USE_NAMESPACE

AudioAiassistantSetting::AudioAiassistantSetting(QObject *parent) : QObject(parent)
{
    Q_ASSERT(QThread::currentThread() == qApp->thread());

    auto configPath = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).first();
    configPath = configPath
                 + "/deepin"
                 + "/uos-ai-assistant"
                 + "/assistantsubstitute.conf";

    QFileInfo configFile(configPath);
    if (!configFile.exists())
        configFile.absoluteDir().mkpath(".");

    m_setting = new QSettings(configPath, QSettings::IniFormat);
    m_timer.setSingleShot(true);
    m_timer.setInterval(2000);
    connect(&m_timer, &QTimer::timeout, this, &AudioAiassistantSetting::sync);
}

AudioAiassistantSetting *AudioAiassistantSetting::instance()
{
    static AudioAiassistantSetting set;
    return &set;
}

void AudioAiassistantSetting::setTTSEnable(bool enable)
{
    m_setting->setValue("tts/enable",enable);
    m_timer.start();
}

bool AudioAiassistantSetting::getTTSEnable()
{
    QString key = "tts/enable";
    bool defaultv = true;
    if(!m_setting->contains(key))
    {
        setTTSEnable(defaultv);
    }

    if(!EAiExec()->isAudioOutputAvailable()) {
        qWarning() << "microphone is not found";
        return false;
    }
    return m_setting->value(key, true).toBool();
}

void AudioAiassistantSetting::setEnableWindow(bool enable)
{
    m_setting->setValue("tts/enableWindow", enable);
    m_timer.start();
}

bool AudioAiassistantSetting::getEnableWindow()
{
    QString key = "tts/enableWindow";
    bool defaultv = true;
    if(!m_setting->contains(key))
    {
        setEnableWindow(defaultv);
    }
    return m_setting->value(key).toBool();
}

void AudioAiassistantSetting::setIatEnable(bool on)
{
    m_setting->setValue("iat/enable", on);
    m_timer.start();
}

bool AudioAiassistantSetting::getIatEnable()
{
    if (IatWidget::hasRecorder()) {
        return m_setting->value("iat/enable", true).toBool();
    }

    return false;
}

void AudioAiassistantSetting::setIatLanguage(QString language)
{
    m_setting->setValue("iat/language",language);
}
QString AudioAiassistantSetting::getIatLanguage()
{
    QString key = "iat/language";
    QString defaultv = "zh_cn";
    if(!m_setting->contains(key))
    {
        setIatLanguage(defaultv);
    }
    return m_setting->value(key).toString();
}

bool AudioAiassistantSetting::setEos(int eos)
{
    if(eos < 0 || eos > 10000)
    {
        return false;
    }
    m_setting->setValue("iat/eos",eos);
    return true;
}
int  AudioAiassistantSetting::getEos()
{
    QString key = "iat/eos";
    int defaultv = 10000;
    if(!m_setting->contains(key))
    {
        setEos(defaultv);
    }
    return m_setting->value(key).toInt();
}
bool AudioAiassistantSetting::setBos(int bos)
{
    if(bos < 0 || bos > 10000)
    {
        return false;
    }
    m_setting->setValue("iat/bos",bos);
    return true;
}
int  AudioAiassistantSetting::getBos()
{
    QString key = "iat/bos";
    int defaultv = 10000;
    if(!m_setting->contains(key))
    {
        setBos(defaultv);
    }
    return m_setting->value(key).toInt();
}

bool  AudioAiassistantSetting::setBosWarning(int warningTime)
{
    if(warningTime < 0 || warningTime > 10000)
    {
        return false;
    }
    m_setting->setValue("iat/BosWarning",warningTime);
    return true;
}
int  AudioAiassistantSetting::getBosWarning()
{
    QString key = "iat/BosWarning";
    int defaultv = 5000;
    if(!m_setting->contains(key))
    {
        setBosWarning(defaultv);
    }
    return m_setting->value(key).toInt();
}

QVariantMap AudioAiassistantSetting::getIatParam()
{
    //params_str = "sub = iat, domain = iat, dwa=wpgs, language = zh_cn, accent = mandarin, sample_rate = 16000, eos=3000, result_type = json, result_encoding = utf8";
    QVariantMap param;
    param["sub"] = QString("iat");
    param["dwa"] = QString("wpgs");
    param["language"] = getIatLanguage();
    param["accent"] = QString("mandarin");
    param["sample_rate"] = QString("16000");
    param["vad_eos"] = QString::number(getEos());
    param["vad_bos"] = QString::number(getBos());
    param["result_type"] = QString("json");
    param["result_encoding"] = QString("utf8");
    return param;
}

void AudioAiassistantSetting::sync()
{
    m_setting->sync();
}
