#include "kasettingdialog.h"

#include <knavigationbar.h>

#include <QResizeEvent>
#include <QVBoxLayout>
#include <QStackedWidget>
#include <kmessagebox.h>
#include <QPushButton>

#include "kaconventionpage.h"
#include "kamodelsettingpage.h"
#include "kainstructionpage.h"
#include "kafloatingbarpage.h"
#include "chatoperator.h"
#include "global/kaglobal.h"

class KASettingDialog::Private
{
public:
    KNavigationBar *navigationBar = nullptr;
    QStackedWidget *stackedWidget = nullptr;
    KAConventionPage *conventionPage = nullptr;
    KAModelSettingPage *modelSettingPage = nullptr;
    KAInstructionPage *instructionPage = nullptr;
    KAFloatingBarPage *floatingBarPage = nullptr;
};

KASettingDialog::KASettingDialog(QWidget *parent):
    KWidget(parent),
    d_ptr(std::make_shared<Private>())
{
    initLayout();
    initConnect();
    setAttribute(Qt::WA_TranslucentBackground);
    // setWindowModality(Qt::WindowModal);
    setWindowModality(Qt::ApplicationModal);
}

KASettingDialog::~KASettingDialog()
{
}

void KASettingDialog::initLayout()
{
    setFixedSize(744, 460);

    setWindowFlag(Qt::Dialog);
    setLayoutType(HorizontalType);
    setIcon(QIcon(":/icon/icon/AI-Writing.svg"));
    setWidgetName(tr("set up"));

    initSideBar();
    initBaseBar();

    windowButtonBar()->maximumButton()->hide();
    windowButtonBar()->minimumButton()->hide();
    windowButtonBar()->setBackgroundRole(QPalette::Window);
    baseBar()->setBackgroundRole(QPalette::Window);
}

void KASettingDialog::initSideBar()
{
    QVBoxLayout *layout_left = new QVBoxLayout(sideBar());
    d_ptr->navigationBar = new KNavigationBar(nullptr);

    QStandardItem *itemConvention = new QStandardItem(QIcon::fromTheme("document-properties-symbolic"), tr("Convention"));
    QStandardItem *itemModelSetting = new QStandardItem(QIcon(":/icon/icon/model.svg"),tr("Model Setting"));
    QStandardItem *itemInstruction = new QStandardItem(QIcon(":/icon/icon/instruction.svg"),tr("Instruction Template"));
    QStandardItem *itemFloatingBar = new QStandardItem(QIcon(":/icon/icon/floatingbar.svg"),tr("Word Selection Floating Bar"));

    d_ptr->navigationBar->addItem(itemConvention);
    d_ptr->navigationBar->addItem(itemModelSetting);
    d_ptr->navigationBar->addItem(itemInstruction);
    d_ptr->navigationBar->addItem(itemFloatingBar);
    d_ptr->navigationBar->listview()->setCurrentIndex(d_ptr->navigationBar->listview()->model()->index(0, 0));
    layout_left->addWidget(d_ptr->navigationBar);

    sideBar()->show();
}

void KASettingDialog::initBaseBar()
{
    QVBoxLayout *vlay = new QVBoxLayout(baseBar());

    d_ptr->stackedWidget = new QStackedWidget;
    vlay->addWidget(d_ptr->stackedWidget);
    {
        d_ptr->conventionPage = new KAConventionPage(this);
        d_ptr->stackedWidget->addWidget(d_ptr->conventionPage);

        // 初始化模型设置界面
        d_ptr->modelSettingPage = new KAModelSettingPage(this);
        connect(d_ptr->modelSettingPage, &KAModelSettingPage::sigDelete, this, &KASettingDialog::showMessageBox);
        d_ptr->stackedWidget->addWidget(d_ptr->modelSettingPage);

        d_ptr->instructionPage = new KAInstructionPage(this);
        d_ptr->stackedWidget->addWidget(d_ptr->instructionPage);

        d_ptr->floatingBarPage = new KAFloatingBarPage(this);
        d_ptr->stackedWidget->addWidget(d_ptr->floatingBarPage);
    }

    d_ptr->stackedWidget->setCurrentIndex(0);
}

void KASettingDialog::initConnect()
{
    connect(d_ptr->navigationBar->listview(), &QListView::clicked, this, &KASettingDialog::slotNaviClicked);
    connect(d_ptr->floatingBarPage, &KAFloatingBarPage::sigShowScribeWords, this, &KASettingDialog::onShowScribeWords);
    connect(d_ptr->floatingBarPage, &KAFloatingBarPage::sigCloseScribeWords, this, &KASettingDialog::onCloseScribeWords);
}

void KASettingDialog::slotNaviClicked(const QModelIndex &index)
{
   QString data = index.data().toString();

   if (data == tr("Convention")) {
        d_ptr->stackedWidget->setCurrentIndex(0);
   } else if(data == tr("Model Setting")) {
        d_ptr->stackedWidget->setCurrentIndex(1);
   } else if(data == tr("Instruction Template")) {
        d_ptr->stackedWidget->setCurrentIndex(2);
   } else if(data == tr("Word Selection Floating Bar")){
        d_ptr->stackedWidget->setCurrentIndex(3);
   }

   return;
}

extern bool is_reply_end;
void KASettingDialog::showMessageBox(RemoteModelId type)
{
    if (!is_reply_end)
    {
        kdk::KMessageBox msgBox(this);
        msgBox.setIcon(kdk::KMessageBox::Critical);
        msgBox.setText(tr("Task in progress, please try again later."));

        QPushButton* buttonRetry = msgBox.addButton(tr("Retry"), kdk::KMessageBox::ResetRole);
        connect(buttonRetry, &QPushButton::clicked, this, [=]{
                    this->showMessageBox(type);
                });
        msgBox.addButton(tr("Cancel"), kdk::KMessageBox::RejectRole);

        msgBox.exec();
    }else{

        kdk::KMessageBox msgBox(this);
        msgBox.setIcon(kdk::KMessageBox::Question);
        msgBox.setText(tr("After deleting this model, the ai tool can not be used properly. Will it continue to delete?"));
        QPushButton* buttonDelete = msgBox.addButton(tr("Delete"), kdk::KMessageBox::AcceptRole);
        buttonDelete->setProperty("isImportant", true);
        msgBox.addButton(tr("Cancel"), kdk::KMessageBox::RejectRole);

        msgBox.exec();

        if(msgBox.clickedButton() == buttonDelete){
            d_ptr->modelSettingPage->deleteModel(type);
        }
    }
}

void KASettingDialog::closeEvent(QCloseEvent *event)
{
    event->ignore();
    hide();
    isSettingOpen = false;
}


void KASettingDialog::scribeWordsDisabled()
{
    if(d_ptr->floatingBarPage)
    {
        d_ptr->floatingBarPage->mEnableBtnDisabled();
    }
}

void KASettingDialog::onShowScribeWords()
{
    emit sigShowScribeWords();
}

void KASettingDialog::onCloseScribeWords()
{
    emit sigCloseScribeWords();
}
