/***************************************************************************
 *   This file is part of awesome-widgets                                  *
 *                                                                         *
 *   awesome-widgets 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.                       *
 *                                                                         *
 *   awesome-widgets 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 awesome-widgets. If not, see http://www.gnu.org/licenses/  *
 ***************************************************************************/

#include "awscriptformatter.h"
#include "ui_awscriptformatter.h"

#include <QJSEngine>
#include <QSettings>

#include "awdebug.h"


AWScriptFormatter::AWScriptFormatter(QObject *_parent, const QString &_filePath)
    : AWAbstractFormatter(_parent, _filePath)
{
    qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;

    if (!_filePath.isEmpty())
        AWScriptFormatter::readConfiguration();
}


QString AWScriptFormatter::convert(const QVariant &_value) const
{
    qCDebug(LOG_LIB) << "Convert value" << _value;

    // init engine
    QJSEngine engine;
    auto fn = engine.evaluate(m_program);
    auto result = fn.call({_value.toString()});

    if (result.isError()) {
        qCWarning(LOG_LIB) << "Uncaught exception at line" << result.property("lineNumber").toInt() << ":"
                           << result.toString();
        return "";
    } else {
        return result.toString();
    }
}


AWScriptFormatter *AWScriptFormatter::copy(const QString &_fileName, const int _number)
{
    qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number;

    auto item = new AWScriptFormatter(parent(), _fileName);
    AWAbstractFormatter::copyDefaults(item);

    item->setAppendCode(appendCode());
    item->setCode(code());
    item->setHasReturn(hasReturn());
    item->setNumber(_number);

    return item;
}


bool AWScriptFormatter::appendCode() const
{
    return m_appendCode;
}


QString AWScriptFormatter::code() const
{
    return m_code;
}


bool AWScriptFormatter::hasReturn() const
{
    return m_hasReturn;
}


QString AWScriptFormatter::program() const
{
    return m_program;
}


void AWScriptFormatter::setAppendCode(const bool _appendCode)
{
    qCDebug(LOG_LIB) << "Set append code" << _appendCode;

    m_appendCode = _appendCode;
    initProgram();
}


void AWScriptFormatter::setCode(const QString &_code)
{
    qCDebug(LOG_LIB) << "Set code" << _code;

    m_code = _code;
    initProgram();
}


void AWScriptFormatter::setHasReturn(const bool _hasReturn)
{
    qCDebug(LOG_LIB) << "Set has return" << _hasReturn;

    m_hasReturn = _hasReturn;
    initProgram();
}


void AWScriptFormatter::readConfiguration()
{
    AWAbstractFormatter::readConfiguration();

    QSettings settings(filePath(), QSettings::IniFormat);

    settings.beginGroup("Desktop Entry");
    setAppendCode(settings.value("X-AW-AppendCode", appendCode()).toBool());
    setCode(settings.value("X-AW-Code", code()).toString());
    setHasReturn(settings.value("X-AW-HasReturn", hasReturn()).toBool());
    settings.endGroup();

    bumpApi(AW_FORMATTER_API);
}


int AWScriptFormatter::showConfiguration(QWidget *_parent, const QVariant &_args)
{
    Q_UNUSED(_args)

    auto dialog = new QDialog(_parent);
    auto ui = new Ui::AWScriptFormatter();
    ui->setupUi(dialog);

    ui->lineEdit_name->setText(name());
    ui->lineEdit_comment->setText(comment());
    ui->label_typeValue->setText("Script");
    ui->checkBox_appendCode->setCheckState(appendCode() ? Qt::Checked : Qt::Unchecked);
    ui->checkBox_hasReturn->setCheckState(hasReturn() ? Qt::Checked : Qt::Unchecked);
    ui->textEdit_code->setPlainText(code());

    auto ret = dialog->exec();
    if (ret == 1) {
        setName(ui->lineEdit_name->text());
        setComment(ui->lineEdit_comment->text());
        setApiVersion(AW_FORMATTER_API);
        setStrType(ui->label_typeValue->text());
        setAppendCode(ui->checkBox_appendCode->checkState() == Qt::Checked);
        setHasReturn(ui->checkBox_hasReturn->checkState() == Qt::Checked);
        setCode(ui->textEdit_code->toPlainText());
        initProgram();

        writeConfiguration();
    }

    dialog->deleteLater();
    delete ui;

    return ret;
}


void AWScriptFormatter::writeConfiguration() const
{
    AWAbstractFormatter::writeConfiguration();

    QSettings settings(writableConfig(), QSettings::IniFormat);
    qCInfo(LOG_LIB) << "Configuration file" << settings.fileName();

    settings.beginGroup("Desktop Entry");
    settings.setValue("X-AW-AppendCode", appendCode());
    settings.setValue("X-AW-Code", code());
    settings.setValue("X-AW-HasReturn", hasReturn());
    settings.endGroup();

    settings.sync();
}


void AWScriptFormatter::initProgram()
{
    // init JS code
    if (appendCode())
        m_program = QString("(function(value) { %1%2 })").arg(code(), hasReturn() ? "" : "; return output;");
    else
        m_program = code();

    qCInfo(LOG_LIB) << "Create JS engine with code" << m_program;
}
