/*
    SPDX-FileCopyrightText: 2008, 2013 Friedrich W. H. Kossebau <kossebau@kde.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "copyasdialog.hpp"

// Kasten gui
#include <Kasten/AbstractModelStreamEncoderConfigEditor>
#include <Kasten/AbstractSelectionView>
// KF
#include <KLocalizedString>
// Qt
#include <QSplitter>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QIcon>
#include <QFont>
#include <QDialogButtonBox>

namespace Kasten {

CopyAsDialog::CopyAsDialog(const QString& remoteTypeName,
                           std::unique_ptr<AbstractModelStreamEncoderConfigEditor>&& configEditor,
                           AbstractModelStreamEncoder* encoder,
                           QWidget* parent)
    : QDialog(parent)
    , mConfigEditor(configEditor.release()) // to be life-time handled by QWidget parentship
    , m_encoder(encoder)
{
    setAttribute(Qt::WA_DeleteOnClose, true);
    mConfigEditor->setParent(this);

    setWindowTitle(i18nc("@title:window", "Copy As"));

    auto* const splitter = new QSplitter(this);

    // config editor
    auto* const editorPage = new QWidget(splitter);
    auto* const editorPageLayout = new QVBoxLayout(editorPage);
    auto* const editorLabel = new QLabel(remoteTypeName);
    QFont font = editorLabel->font();
    font.setBold(true);
    editorLabel->setFont(font);

    editorPageLayout->addWidget(editorLabel);
    editorPageLayout->addWidget(mConfigEditor);
    editorPageLayout->addStretch();

    splitter->addWidget(editorPage);
    splitter->setCollapsible(0, false);

    mPreviewView = mConfigEditor->createPreviewView();

    if (mPreviewView) {
        auto* const previewBox = new QGroupBox(i18nc("@title:group", "Preview"), this);
        splitter->addWidget(previewBox);

        auto* const previewBoxLayout = new QHBoxLayout(previewBox);

        previewBoxLayout->addWidget(mPreviewView->widget());
    }

    // dialog buttons
    auto* const dialogButtonBox = new QDialogButtonBox;
    auto* const exportButton = new QPushButton(QIcon::fromTheme(QStringLiteral("edit-copy")),
                                               i18nc("@action:button", "&Copy to clipboard"));
    exportButton->setToolTip(i18nc("@info:tooltip",
                                   "Copy the selected data to the clipboard."));
    exportButton->setWhatsThis(xi18nc("@info:whatsthis",
                                      "If you press the <interface>Copy to clipboard</interface> "
                                      "button, the selected data will be copied to the clipboard "
                                      "with the settings you entered above."));

    dialogButtonBox->addButton(exportButton, QDialogButtonBox::AcceptRole);
    connect(dialogButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    dialogButtonBox->addButton(QDialogButtonBox::Cancel);
    connect(dialogButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    exportButton->setEnabled(mConfigEditor->isValid());
    connect(mConfigEditor, &AbstractModelStreamEncoderConfigEditor::validityChanged,
            exportButton, &QWidget::setEnabled);

    // main layout
    auto* const layout = new QVBoxLayout;
    layout->addWidget(splitter, 1);
    layout->addWidget(dialogButtonBox);

    setLayout(layout);

    connect(this, &QDialog::finished, this, &CopyAsDialog::onFinished);
}

CopyAsDialog::~CopyAsDialog() = default;

void CopyAsDialog::setData(AbstractModel* model, const AbstractModelSelection* selection)
{
    m_selection = selection;

    if (mPreviewView) {
        mPreviewView->setData(model, selection);
    }
}

void CopyAsDialog::onFinished(int result)
{
    if (result != QDialog::Accepted) {
        return;
    }

    Q_EMIT copyAccepted(m_encoder, m_selection);
}

}

#include "moc_copyasdialog.cpp"
