/*
    SPDX-FileCopyrightText: 2019 Mark Nauwelaerts <mark.nauwelaerts@gmail.com>
    SPDX-FileCopyrightText: 2023 Akseli Lahtinen <akselmo@akselmo.dev>

    SPDX-License-Identifier: MIT
*/

#include "debugconfigpage.h"
#include "plugin_kategdb.h"
#include "ui_debugconfig.h"

#include <KLocalizedString>

#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/Repository>
#include <KSyntaxHighlighting/SyntaxHighlighter>
#include <KSyntaxHighlighting/Theme>

#include <KTextEditor/Editor>

#include <QJsonDocument>
#include <QJsonParseError>
#include <QMenu>
#include <QPalette>

DebugConfigPage::DebugConfigPage(QWidget *parent, KatePluginGDB *plugin)
    : KTextEditor::ConfigPage(parent)
    , m_plugin(plugin)
{
    ui = new Ui::DebugConfigWidget();
    ui->setupUi(this);

    // Fix-up our two text edits to be proper JSON file editors
    updateHighlighters();

    // Ensure we update the highlighters if the repository is updated or theme is changed
    connect(KTextEditor::Editor::instance(), &KTextEditor::Editor::repositoryReloaded, this, &DebugConfigPage::updateHighlighters);
    connect(KTextEditor::Editor::instance(), &KTextEditor::Editor::configChanged, this, &DebugConfigPage::updateHighlighters);

    // Setup default JSON settings
    QFile defaultConfigFile(QStringLiteral(":/debugger/dap.json"));
    defaultConfigFile.open(QIODevice::ReadOnly);
    Q_ASSERT(defaultConfigFile.isOpen());
    ui->defaultConfig->setPlainText(QString::fromUtf8(defaultConfigFile.readAll()));

    // Setup default config path as placeholder to show user where it is
    ui->edtConfigPath->setPlaceholderText(m_plugin->m_defaultConfigPath.toLocalFile());

    reset();

    connect(ui->edtConfigPath, &KUrlRequester::textChanged, this, &DebugConfigPage::configUrlChanged);
    connect(ui->edtConfigPath, &KUrlRequester::urlSelected, this, &DebugConfigPage::configUrlChanged);

    auto cfgh = [this](int position, int added, int removed) {
        Q_UNUSED(position);
        // Discard format change
        // (e.g. due to syntax highlighting)
        if (added || removed) {
            configTextChanged();
        }
    };
    connect(ui->userConfig->document(), &QTextDocument::contentsChange, this, cfgh);
}

DebugConfigPage::~DebugConfigPage()
{
    delete ui;
}

QString DebugConfigPage::name() const
{
    return QString(i18n("Debugger"));
}

QString DebugConfigPage::fullName() const
{
    return QString(i18n("Debugger"));
}

QIcon DebugConfigPage::icon() const
{
    return QIcon::fromTheme(QLatin1String("debug-run"));
}

void DebugConfigPage::apply()
{
    m_plugin->m_configPath = ui->edtConfigPath->url();
    // Own scope to ensure file is flushed before we signal below in writeConfig!
    {
        QFile configFile(m_plugin->configPath().toLocalFile());
        configFile.open(QIODevice::WriteOnly);
        if (configFile.isOpen()) {
            configFile.write(ui->userConfig->toPlainText().toUtf8());
        }
    }

    m_plugin->writeConfig();
}

void DebugConfigPage::reset()
{
    ui->edtConfigPath->setUrl(m_plugin->m_configPath);
    readUserConfig(m_plugin->configPath().toLocalFile());
}

void DebugConfigPage::defaults()
{
    reset();
}

void DebugConfigPage::readUserConfig(const QString &fileName)
{
    QFile configFile(fileName);
    configFile.open(QIODevice::ReadOnly);
    if (configFile.isOpen()) {
        ui->userConfig->setPlainText(QString::fromUtf8(configFile.readAll()));
    } else {
        ui->userConfig->clear();
    }

    updateConfigTextErrorState();
}

void DebugConfigPage::updateConfigTextErrorState()
{
    const auto data = ui->userConfig->toPlainText().toUtf8();
    if (data.isEmpty()) {
        ui->userConfigError->setText(i18n("No JSON data to validate."));
        return;
    }

    QJsonParseError error{};
    auto json = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError) {
        if (json.isObject()) {
            ui->userConfigError->setText(i18n("JSON data is valid."));
        } else {
            ui->userConfigError->setText(i18n("JSON data is invalid: no JSON object"));
        }
    } else {
        ui->userConfigError->setText(i18n("JSON data is invalid: %1", error.errorString()));
    }
}

void DebugConfigPage::configTextChanged()
{
    updateConfigTextErrorState();
    changed();
}

void DebugConfigPage::configUrlChanged()
{
    readUserConfig(ui->edtConfigPath->url().isEmpty() ? m_plugin->m_defaultConfigPath.toLocalFile() : ui->edtConfigPath->url().toLocalFile());
    changed();
}

void DebugConfigPage::updateHighlighters()
{
    for (auto textEdit : {ui->userConfig, ui->defaultConfig}) {
        auto highlighter = new KSyntaxHighlighting::SyntaxHighlighter(textEdit->document());
        highlighter->setDefinition(KTextEditor::Editor::instance()->repository().definitionForFileName(QStringLiteral("dap.json")));

        // Use monospace font
        textEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));

        // Use right color scheme
        const auto theme = KTextEditor::Editor::instance()->theme();
        auto pal = qApp->palette();
        pal.setColor(QPalette::Base, QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::BackgroundColor)));
        pal.setColor(QPalette::Highlight, QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::TextSelection)));
        textEdit->setPalette(pal);
        highlighter->setTheme(theme);
        highlighter->rehighlight();
    }
}

#include "moc_debugconfigpage.cpp"
