/*
  SPDX-FileCopyrightText: 2025-2026 Laurent Montel <montel@kde.org>

  SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "textautogeneratetextlineeditwidget.h"

#include "core/models/textautogeneratechatsmodel.h"
#include "core/textautogeneratemanager.h"
#include "core/textautogeneratetextplugin.h"
#include "widgets/common/textautogeneratetextlineedit.h"
#include "widgets/common/textautogeneratetextlineeditattachmentwidget.h"
#include "widgets/toolswidget/textautogeneratetoolswidget.h"
#include <KLocalizedString>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QMimeDatabase>
#include <QToolButton>

using namespace Qt::Literals::StringLiterals;
using namespace TextAutoGenerateText;
TextAutoGenerateTextLineEditWidget::TextAutoGenerateTextLineEditWidget(TextAutoGenerateText::TextAutoGenerateManager *manager, QWidget *parent)
    : QWidget{parent}
    , mTextAutoGenerateTextLineEdit(new TextAutoGenerateTextLineEdit(this))
    , mTextAutoGenerateToolsWidget(new TextAutoGenerateToolsWidget(this))
    , mTextLineEditAttachmentWidget(new TextAutoGenerateTextLineEditAttachmentWidget(this))
    , mSendMessage(new QToolButton(this))
    , mAttachFile(new QToolButton(this))
    , mConfigureTools(new QToolButton(this))
    , mManager(manager)
{
    auto topLayout = new QVBoxLayout(this);
    topLayout->setObjectName(u"topLayout"_s);
    topLayout->setContentsMargins(QMargins{});
    topLayout->setSpacing(0);

    mTextLineEditAttachmentWidget->setObjectName(u"mTextLineEditAttachmentWidget"_s);
    topLayout->addWidget(mTextLineEditAttachmentWidget);

    mTextAutoGenerateToolsWidget->setObjectName(u"mTextAutoGenerateToolsWidget"_s);
    topLayout->addWidget(mTextAutoGenerateToolsWidget);
    mTextAutoGenerateToolsWidget->setVisible(false);

    auto mainLayout = new QHBoxLayout;
    mainLayout->setObjectName(u"mainLayout"_s);
    mainLayout->setContentsMargins(QMargins{});
    mainLayout->setSpacing(0);
    topLayout->addLayout(mainLayout);

    mAttachFile->setObjectName(u"mAttachFile"_s);
    mAttachFile->setToolTip(i18nc("@info:tooltip", "Attach File"));
    mainLayout->addWidget(mAttachFile, 0, Qt::AlignTop);
    mAttachFile->setIcon(QIcon::fromTheme(u"mail-attachment-symbolic"_s));
    mAttachFile->setAutoRaise(true);

    connect(mAttachFile, &QToolButton::clicked, this, [this]() {
        const bool hasVisionSupport = mManager->textAutoGeneratePlugin()->hasVisionSupport();
        const bool hasOcrSupport = mManager->textAutoGeneratePlugin()->hasOcrSupport();
        const bool hasAudioSupport = mManager->textAutoGeneratePlugin()->hasAudioSupport();
        QStringList filterFiles;
        if (hasVisionSupport || hasOcrSupport) {
            filterFiles << i18n("Image file %1", u"(*.png *.xpm *.jpg *.gif)"_s); // TODO add more
        }
        if (hasAudioSupport) {
            filterFiles << i18n("Audio file %1", u"(*.mp4 *.avi)"_s); // TODO add more
        }
        filterFiles << i18n("Text file %1", u"(*.txt *.md)"_s); // TODO add more
        const QStringList fileNames = QFileDialog::getOpenFileNames(this, i18nc("@title:window", "Select File"), {}, filterFiles.join(u";;"_s));
        if (!fileNames.isEmpty()) {
            for (const auto &f : fileNames) {
                mTextLineEditAttachmentWidget->addAttachement(f);
            }
        }
    });

    mTextAutoGenerateTextLineEdit->setObjectName(u"mTextAutoGenerateTextLineEdit"_s);
    mainLayout->addWidget(mTextAutoGenerateTextLineEdit, 0, Qt::AlignTop);
    mTextAutoGenerateTextLineEdit->setProperty("_breeze_borders_sides", QVariant::fromValue(QFlags{Qt::TopEdge}));

    mSendMessage->setObjectName(u"mSendMessage"_s);
    mainLayout->addWidget(mSendMessage, 0, Qt::AlignTop);
    mSendMessage->setToolTip(i18nc("@info:tooltip", "Send"));
    mSendMessage->setIcon(QIcon::fromTheme(u"document-send"_s));
    mSendMessage->setAutoRaise(true);

    connect(mTextAutoGenerateTextLineEdit, &TextAutoGenerateTextLineEdit::sendMessage, this, [this](const QString &msg) {
        const QString str = msg.trimmed();
        mSendMessage->setEnabled(!str.isEmpty());
        if (!str.isEmpty()) {
            Q_EMIT editingFinished(str,
                                   mUuid,
                                   mTextAutoGenerateToolsWidget->generateListOfActiveTools(),
                                   mTextLineEditAttachmentWidget->attachmentElementInfoList());
            clearLineEdit();
        }
    });
    mSendMessage->setEnabled(false);

    connect(mTextAutoGenerateTextLineEdit, &TextAutoGenerateTextLineEdit::textChanged, this, [this]() {
        mSendMessage->setEnabled(!mTextAutoGenerateTextLineEdit->document()->isEmpty());
    });

    connect(mSendMessage, &QToolButton::clicked, this, [this]() {
        Q_EMIT editingFinished(mTextAutoGenerateTextLineEdit->text(),
                               mUuid,
                               mTextAutoGenerateToolsWidget->generateListOfActiveTools(),
                               mTextLineEditAttachmentWidget->attachmentElementInfoList());
        clearLineEdit();
    });
    connect(mTextAutoGenerateTextLineEdit, &TextAutoGenerateTextLineEdit::keyPressed, this, &TextAutoGenerateTextLineEditWidget::keyPressed);
    if (mManager) {
        connect(mManager, &TextAutoGenerateText::TextAutoGenerateManager::showArchiveChanged, this, &TextAutoGenerateTextLineEditWidget::updateEnableState);
        connect(mManager, &TextAutoGenerateText::TextAutoGenerateManager::chatInProgressChanged, this, &TextAutoGenerateTextLineEditWidget::updateEnableState);
        connect(mManager, &TextAutoGenerateText::TextAutoGenerateManager::currentChatIdChanged, this, &TextAutoGenerateTextLineEditWidget::updateEnableState);
        connect(mManager,
                &TextAutoGenerateText::TextAutoGenerateManager::currentModelChanged,
                this,
                &TextAutoGenerateTextLineEditWidget::slotCurrentModelChanged);

        connect(mManager, &TextAutoGenerateManager::discussionListIsEmpty, this, [this](bool state) {
            setEnabled(!state);
        });
    }

    mConfigureTools->setObjectName(u"mConfigureTools"_s);
    mConfigureTools->setToolTip(i18nc("@info:tooltip", "Allow to select tools"));
    mainLayout->addWidget(mConfigureTools, 0, Qt::AlignTop);
    mConfigureTools->setIcon(QIcon::fromTheme(u"settings-configure"_s));
    mConfigureTools->setAutoRaise(true);
    mConfigureTools->setCheckable(true);
    connect(mConfigureTools, &QToolButton::clicked, this, [this]() {
        mTextAutoGenerateToolsWidget->setVisible(mConfigureTools->isChecked());
    });
}

TextAutoGenerateTextLineEditWidget::~TextAutoGenerateTextLineEditWidget() = default;

void TextAutoGenerateTextLineEditWidget::updateAttachmentButton(bool state)
{
    mAttachFile->setEnabled(state);
}

void TextAutoGenerateTextLineEditWidget::updateEnableState()
{
    setEnabled(!mManager->showArchived() && !mManager->chatInProgress(mManager->currentChatId()) && !mManager->textAutoGenerateChatsModel()->isEmpty());
}

void TextAutoGenerateTextLineEditWidget::clearLineEdit()
{
    mTextAutoGenerateTextLineEdit->clear();
    mUuid.clear();
    mTextLineEditAttachmentWidget->clear();
}

QString TextAutoGenerateTextLineEditWidget::text() const
{
    return mTextAutoGenerateTextLineEdit->text();
}

void TextAutoGenerateTextLineEditWidget::setText(const QString &str)
{
    mTextAutoGenerateTextLineEdit->setText(str);
}

QByteArray TextAutoGenerateTextLineEditWidget::uuid() const
{
    return mUuid;
}

void TextAutoGenerateTextLineEditWidget::setUuid(const QByteArray &newUuid)
{
    mUuid = newUuid;
}

void TextAutoGenerateTextLineEditWidget::setActivatedTools(const QList<QByteArray> &lst)
{
    mTextAutoGenerateToolsWidget->setActivatedTools(lst);
}

QList<QByteArray> TextAutoGenerateTextLineEditWidget::activatedTools() const
{
    return mTextAutoGenerateToolsWidget->generateListOfActiveTools();
}

void TextAutoGenerateTextLineEditWidget::slotCurrentModelChanged()
{
    const bool hasVisionSupport = mManager->textAutoGeneratePlugin()->hasVisionSupport();
    const bool hasToolsSupport = mManager->textAutoGeneratePlugin()->hasToolsSupport();
    mConfigureTools->setEnabled(hasToolsSupport);
    if (!hasToolsSupport) {
        mConfigureTools->setChecked(false);
        mTextAutoGenerateToolsWidget->setHidden(true);
    }
    mAttachFile->setChecked(hasVisionSupport);
}

#include "moc_textautogeneratetextlineeditwidget.cpp"
