/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "webprofile.h"
#include "urlinterceptor.h"
#include <QFileInfo>
#include <QSettings>
#include <QWebEngineCookieStore>
#include <QWebEngineSettings>
#include <spdlog/spdlog.h>

static WebProfile *s_profile = nullptr;

void WebProfile::setDefaultProfile(WebProfile *profile)
{
    s_profile = profile;
}
WebProfile *WebProfile::defaultProfile()
{
    return s_profile;
}

QSettings *WebProfile::load(const QString &path, const QString &search, const QUrl &homepage, const QUrl &newtab)
{
    auto *settings = new QSettings(path, QSettings::IniFormat);

    if(!settings->contains("search")) {
        settings->setValue("search", search);
    }
    if(!settings->contains("homepage")) {
        settings->setValue("homepage", homepage);
    }
    if(!settings->contains("newtab")) {
        settings->setValue("newtab", newtab);
    }

    return settings;
}

WebProfile *WebProfile::load(const QString &id, QSettings *settings, bool isOffTheRecord)
{
    WebProfile *profile = nullptr;

    if(settings->value("otr", isOffTheRecord).toBool()) {
        profile = new WebProfile(id, nullptr);
    } else {
        profile = new WebProfile(id, id, nullptr);
    }

    profile->m_name = settings->value("name", id).toString();
    connect(profile, &WebProfile::nameChanged, profile, [settings](const QString &name) { settings->setValue("name", name); });
    profile->m_search = settings->value("search", "").toString();
    connect(profile, &WebProfile::searchChanged, settings, [settings](const QString &url) { settings->setValue("search", url); });
    profile->m_homepage = settings->value("homepage", "").toUrl();
    connect(profile, &WebProfile::homepageChanged, settings, [settings](const QUrl &url) { settings->setValue("homepage", url); });
    profile->m_newtab = settings->value("newtab", "").toUrl();
    connect(profile, &WebProfile::newtabChanged, settings, [settings](const QUrl &url) { settings->setValue("newtab", url); });

    {
        settings->beginGroup("properties");
        const auto keys = settings->childKeys();
        for(const QString &key : keys) {
            profile->setProperty(qUtf8Printable(key), settings->value(key));
        }
        settings->endGroup(); // properties
        connect(profile, &WebProfile::propertyChanged, [settings](const QString &property, const QVariant &value) {
            settings->setValue("properties/" + property, value);
        });
    }
    {
        settings->beginGroup("attributes");
        const auto keys = settings->childKeys();
        auto *s = profile->settings();
        for(const QString &key : keys) {
            auto attribute = static_cast<QWebEngineSettings::WebAttribute>(key.toInt());
            s->setAttribute(attribute, settings->value(key).toBool());
        }
        settings->endGroup();
        connect(profile, &WebProfile::attributeChanged, [settings](const QWebEngineSettings::WebAttribute attr, const bool value) {
            settings->setValue("attributes/" + QString::number(attr), value);
        });
    }
    {
        // headers
        settings->beginGroup("headers");
        const auto keys = settings->childKeys();
        for(const QString &key : keys) {
            profile->setHttpHeader(key.toLatin1(), settings->value(key).toString().toLatin1());
        }
        settings->endGroup();
        connect(profile, &WebProfile::headerChanged, [settings](const QString &name, const QString &value) {
            settings->setValue("headers/" + name, value);
        });
        connect(profile, &WebProfile::headerRemoved, [settings](const QString &name) {
            settings->remove("headers/" + name);
        });
    }
    return profile;
}

// off-the-record constructor
WebProfile::WebProfile(const QString &id, QObject *parent)
    : QWebEngineProfile(parent)
    , m_id(id)
{
    QWebEngineProfile::setUrlRequestInterceptor(new UrlRequestInterceptor(this));
    connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) {
        spdlog::debug("[{}]: +cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
        m_cookies.append(cookie);
    });
    connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) {
        spdlog::debug("[{}]: -cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
        m_cookies.removeOne(cookie);
    });
    cookieStore()->loadAllCookies();
}

// default constructor
WebProfile::WebProfile(const QString &id, const QString &storageName, QObject *parent)
    : QWebEngineProfile(storageName, parent)
    , m_id(id)
{
    QWebEngineProfile::setUrlRequestInterceptor(new UrlRequestInterceptor(this));
    connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) {
        spdlog::debug("[{}]: +cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
        m_cookies.append(cookie);
    });
    connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) {
        spdlog::debug("[{}]: -cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
        m_cookies.removeOne(cookie);
    });
    cookieStore()->loadAllCookies();
}