#ifndef CMAINWND_H
#define CMAINWND_H

#include <QObject>
#include <QWidget>
#include <QTextEdit>
#include <QLabel>
#include <QJsonParseError>
#include <QJsonObject>
#include <QDesktopWidget>
#include <QApplication>
#include <QMenuBar>
#include <QToolButton>
#include <QAction>
#include <QDebug>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QStackedWidget>
#include <QFileSystemWatcher>
#include <QGuiApplication>
#include <QScreen>
#include <QList>
#include <QClipboard>
#include <QQuickWidget>
#include <QQuickItem>
#include <QQmlContext>
#include <QSpacerItem>
#include <QDir>
#include <QShowEvent>
#include <QTimer>
#include <QSessionManager>
#include <QProcess>
#include <QStandardPaths>
#include <QFileDialog>
#include <KGlobalAccel>
#include <KWindowSystem>
#include <unistd.h>
#include <QShortcut>
#include <QKeySequence>
#include <QDesktopServices>
#include <QUrl>
#include <QMetaObject>

#include <kysdk/applications/kwidget.h>
#include <ukui-log4qt.h>
#include <kysdk/diagnosetest/libkydatacollect.h>
#include <ukuistylehelper/ukuistylehelper.h>
#include <windowmanager/windowmanager.h>

#include "msgpane.h"
#include "cpushbuttonex.h"
#include "setting/settingdialog.h"
#include "dbus/kadbusoperator.h"
#include "textselect/clipboard.h"
#include "textselect/selection.h"
#include "setting/scribewordpage.h"
#include "sqlconversationmodel.h"
#include "global/kaglobal.h"
#include "tipwidget.h"
#include "textselect/selection.h"
#include "commandtemplatelist.h"
#include <custommessagebox.h>
#include "global/kaglobal.h"
#include "introductionview.h"
#include "kaboutdialog.h"
#include "closemodewidget.h"
#include "windowhelper.h"
#include "customwindowbutton.h"

#define UKUI_FONT_SIZE "systemFontSize"
#define WINDOW_MARGIN 8
#define SIDEBAR_WIDTH 402
#define WINDOW_WIDTH 902
#define WINDOW_HEIGHT 568
#define WINDOW_MIN_WIDTH 768
#define WINDOW_MIN_HEIGHT 568

using namespace kdk;


class CPushButtonEx;

enum MainWndViewType
{
    TABTITLE_MESSAGE = 0,       // 消息会话
    TABTITLE_VOICE = 1,      // 通讯录
};

class CMainWnd : public KWidget
{
    Q_OBJECT
public:

    explicit CMainWnd(QWidget *parent = nullptr);
    ~CMainWnd();

    QGSettings *themeData = nullptr; //监测主题

    void checkTheme();
    void keepWindowAbove(bool needTokeepAbove);
    void setMovable(bool isMovable);

    void initTitleBar();
    void initComponent();
    void initWindowMode();

    void showView();
    void hideView();

public slots:
    void onFocusChanged(QWidget *oldWidget, QWidget *newWidget);
    void slotToTop();
    void slotToTopChangeWinMode();

    void onConfActionTriggered();

    void onShotCutActionTriggled();
    void onAboutActionTriggled();
    void onExitActionTriggled();
    void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason);
    void onAppRestarted(const QString &message);
    void onSideBarBtnClicked();
    void onBackBtnClicked();
    void onPageChanged(const int pageNumber);
    void handleClicpText();
    void onStartSendSelectMsg();
    void onCreateDialog();
    void onShowScribeWord();
    void onShowCustomMessageBox(const QVariantList &strings);
    void showOrHideWindow();
    void onShowHelp();
    void onShowAbout();
    void onActivateMainWindow();
    void onSaveAs(const QString &base64Image);  //响应qml发来的下载图片文件选择框
    void onOpenFile(int typeNum);  //响应qml发来的添加图片/文档文件选择框
    void onShowAddFileMenu();
    void onAddFileMenuAboutToHide();
    void aboutToQuit();
    ///
    /// \brief handleSendFile 处理插件右键发送的文件
    ///
    void handleSendFile();

signals:
    void sigThemeChanged();

private:
    void createAllChildWnd();
    void initCtrl();
    void initSolts();
    void initLayout();
    void initTrayIcon();
    void initTrayIconMenu();
    void initScribeWords();
    void initPosition();
    void initScribeWordsSetting();
    void showSettingPage();
    void initShortcut();
    QString getAiAssistantVersion();
    void initTopping();
    void closeOrHidden();
    void initLoadingIcon();

    void switchToSideMode(); // 切换到侧边栏模式
    void setSideModePos(); // 保持在当前屏幕
    void switchToWindowMode(); // 切换到窗口模式
    void setWindowModePos(); // 设置在当前屏幕居中
    void adjustForScreenChanged(); // 响应屏幕变化
    QScreen* getCurrentScreen(); // 获取当前所在屏幕


protected:
   void closeEvent(QCloseEvent *e) override;


protected:
    void moveEvent(QMoveEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    bool eventFilter(QObject *obj, QEvent *event) override;

private:
    bool m_isTop = false;
    bool isMsgBoxOpen = false;
    bool isCloseBoxOpen = false;

    QWidget *m_leftBtnWidget = nullptr;
    CPushButtonEx *m_btnSideBar = nullptr;
    QPushButton *m_btnBack = nullptr;
    QPushButton *m_btnStick = nullptr;
    QAction *confAction = nullptr;
    SettingDialog *m_settingPage = nullptr;
    KADBusOperator *m_dbusOperator = nullptr;
    QAction *m_shotcutAction = nullptr;
    QAction *m_aboutAction = nullptr;
    QAction *m_exitAction = nullptr;
    QMenu *m_trayIconMenu = nullptr;
    QSystemTrayIcon *m_trayIcon = nullptr;
    CMsgPane *m_msgPane = nullptr;
    Clipboard *m_selectclip = nullptr;
    SelectWidget *m_selectwid = nullptr;
    QFileSystemWatcher *m_fileWatcher = nullptr;
    QQuickWidget *m_mainQml = nullptr;                        // QML主页面
    QStackedWidget *m_stackWdiget = nullptr;
    IntroductionView *m_introWidget = nullptr;
    QAction *m_actionShowScribeWord = nullptr;                //划词快捷键
    QAction *m_actionShowView = nullptr;                      //显示助手快捷键
    QMenu *m_addFileMenu = nullptr;
    ActiveModule m_previousModule = ActiveModule::MODULE_CHAT;
    QString appVersion = nullptr;
    QString appShowingName = tr("AI Assistant");
    kdk::KAboutDialog *aboutWindow;
    QTimer *selectTimer = nullptr;
    bool m_isMouseDragMove = true; // 是否是鼠标拖动
    QLabel *m_loadingIcon = nullptr;

#ifdef UKUI_WINDOWHELPER_SUPPORT
    UkuiWindowHelper *m_ukuiWindowHelper = nullptr;
#else
    quint64 m_window_id = 0;
#endif

    CloseModeWidget *m_closeModeWidget = nullptr;
    QMetaObject::Connection m_availableGeometryChangedConnection;
    QScreen *m_currentScreen = nullptr;
    bool m_isFirstShow = true;
    QString m_peonyPluginFilePath = "";
};

#endif // CMAINWND_H
