/*
   SPDX-FileCopyrightText: 2013-2022 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "notenetworkconfig.h"

#include "notesharedglobalconfig.h"

#include <KLocalizedString>
#include <QLineEdit>
#include <QSpinBox>

#include <QCheckBox>
#include <QGroupBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QWhatsThis>

using namespace NoteShared;

NoteNetworkConfigWidget::NoteNetworkConfigWidget(QWidget *parent)
    : QWidget(parent)
{
    auto layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);

    auto incoming = new QGroupBox(i18n("Incoming Notes"));
    auto tmpLayout = new QHBoxLayout;

    mTmpChkB = new QCheckBox(i18n("Accept incoming notes"));
    mTmpChkB->setObjectName(QStringLiteral("kcfg_ReceiveNotes"));
    tmpLayout->addWidget(mTmpChkB);
    incoming->setLayout(tmpLayout);
    layout->addWidget(incoming);

    auto outgoing = new QGroupBox(i18n("Outgoing Notes"));
    tmpLayout = new QHBoxLayout;

    auto label_SenderID = new QLabel(i18n("&Sender ID:"));
    m_kcfg_SenderID = new QLineEdit;
    m_kcfg_SenderID->setClearButtonEnabled(true);
    m_kcfg_SenderID->setObjectName(QStringLiteral("kcfg_SenderID"));
    label_SenderID->setBuddy(m_kcfg_SenderID);
    tmpLayout->addWidget(label_SenderID);
    tmpLayout->addWidget(m_kcfg_SenderID);
    outgoing->setLayout(tmpLayout);
    layout->addWidget(outgoing);

    tmpLayout = new QHBoxLayout;

    auto label_Port = new QLabel(i18n("&Port:"));

    tmpLayout->addWidget(label_Port);

    kcfg_Port = new QSpinBox;
    kcfg_Port->setObjectName(QStringLiteral("kcfg_Port"));
    kcfg_Port->setRange(0, 65535);
    label_Port->setBuddy(kcfg_Port);
    tmpLayout->addWidget(kcfg_Port);
    layout->addLayout(tmpLayout);
    layout->addStretch();
    load();
}

NoteNetworkConfigWidget::~NoteNetworkConfigWidget() = default;

void NoteNetworkConfigWidget::save()
{
    NoteShared::NoteSharedGlobalConfig::self()->setReceiveNotes(mTmpChkB->isChecked());
    NoteShared::NoteSharedGlobalConfig::self()->setSenderID(m_kcfg_SenderID->text());
    NoteShared::NoteSharedGlobalConfig::self()->setPort(kcfg_Port->value());
    NoteShared::NoteSharedGlobalConfig::self()->save();
}

void NoteNetworkConfigWidget::load()
{
    mTmpChkB->setChecked(NoteShared::NoteSharedGlobalConfig::self()->receiveNotes());
    m_kcfg_SenderID->setText(NoteShared::NoteSharedGlobalConfig::self()->senderID());
    kcfg_Port->setValue(NoteShared::NoteSharedGlobalConfig::self()->port());
}

NoteNetworkConfig::NoteNetworkConfig(QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
{
    auto lay = new QVBoxLayout(this);
    lay->setContentsMargins(0, 0, 0, 0);
    auto noteNetworkConfigWidget = new NoteNetworkConfigWidget(this);
    lay->addWidget(noteNetworkConfigWidget);
    addConfig(NoteShared::NoteSharedGlobalConfig::self(), noteNetworkConfigWidget);
    load();
}

void NoteNetworkConfig::save()
{
    KCModule::save();
}

void NoteNetworkConfig::load()
{
    KCModule::load();
}
