/*
 * 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: git://neueland.iserlohn-fortress.net/smolbote.git
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "profileview.h"
#include "ui_profileview.h"

#include <QLineEdit>
#include <QPlainTextEdit>
#include <QComboBox>
#include <QPushButton>
#include "webengine/webengineprofile.h"
#include "forms/cookiesform.h"

#include <QDialogButtonBox>

#include <QFormLayout>

ProfileView::ProfileView(WebEngineProfile *profile, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProfileView)
{
    ui->setupUi(this);
    setProfile(profile);

    // actions
    connect(ui->clearCache_pushButton, &QPushButton::clicked, [this]() {
        this->m_profile->clearHttpCache();
    });
    connect(ui->clearHistory_pushButton, &QPushButton::clicked, [this]() {
        this->m_profile->clearAllVisitedLinks();
    });

    connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ProfileView::updateProfile);
    connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ProfileView::close);
}

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

void ProfileView::setProfile(WebEngineProfile *profile)
{
    Q_CHECK_PTR(profile);

    m_profile = profile;
    setWindowTitle(m_profile->name());

    m_cookiesForm = new CookiesForm(m_profile->cookieStore(), this);
    //ui->tabWidget->addTab(m_cookiesForm, m_cookiesForm->windowTitle());

    // general
    ui->homepage_lineEdit->setText(m_profile->homepage().toString());
    ui->newtab_lineEdit->setText(m_profile->newtab().toString());

    // http
    ui->userAgent->setPlainText(m_profile->httpUserAgent());
    ui->acceptLanguage->setPlainText(m_profile->httpAcceptLanguage());
    ui->cacheType->setCurrentIndex(m_profile->httpCacheType());
    ui->cacheSize->setText(QString::number(m_profile->httpCacheMaximumSize()));

    // path
    ui->storagePath_lineEdit->setText(m_profile->persistentStoragePath());
    ui->cachePath_lineEdit->setText(m_profile->cachePath());

    // policy
    ui->cookiePolicy->setCurrentIndex(m_profile->persistentCookiesPolicy());

    //ui->formLayout_3->addWidget(m_cookiesForm);
    ui->verticalLayout_3->addWidget(m_cookiesForm);
}

void ProfileView::showProfile()
{
    ui->tabWidget->setCurrentIndex(0);
    show();
}

void ProfileView::showCookies()
{
    ui->tabWidget->setCurrentIndex(2);
    show();
}

void ProfileView::updateProfile()
{
    qDebug("Updating profile [%s]...", qUtf8Printable(m_profile->name()));

    // general
    m_profile->setHomepage(QUrl::fromUserInput(ui->homepage_lineEdit->text()));
    m_profile->setNewtab(QUrl::fromUserInput(ui->newtab_lineEdit->text()));

    // http
    m_profile->setHttpUserAgent(ui->userAgent->toPlainText());
    m_profile->setHttpAcceptLanguage(ui->acceptLanguage->toPlainText());
    switch (ui->cacheType->currentIndex()) {
    case 0:
        m_profile->setHttpCacheType(QWebEngineProfile::MemoryHttpCache);
        break;
    case 1:
        m_profile->setHttpCacheType(QWebEngineProfile::DiskHttpCache);
        break;
    case 2:
        m_profile->setHttpCacheType(QWebEngineProfile::NoCache);
        break;
    default:
        break;
    }
    m_profile->setHttpCacheMaximumSize(ui->cacheSize->text().toInt());

    // policy
    switch (ui->cookiePolicy->currentIndex()) {
    case 0:
        m_profile->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
        break;
    case 1:
        m_profile->setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies);
        break;
    case 2:
        m_profile->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
        break;
    default:
        break;
    }

    m_profile->saveProfile();
}