#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 <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 "xeventmonitor.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"
#include <startuptools.h>

#define UKUI_FONT_SIZE "systemFontSize"
#define WINDOW_MARGIN 8
#define SIDEBAR_WIDTH 402


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();
    IntroductionView * m_introductionView = nullptr;    //新手引导页面
    void keepWindowAbove(bool needTokeepAbove);
    void setMovable(bool isMovable);

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 onWindowModeButtonClicked();
    void onShowScribeWord();
    void onShowCustomMessageBox(const QVariantList &strings);
    void onX11GlobalMousePress(int x, int y);
    void showOrHideWindow();
    void onShowHelp();
    void onShowAbout();
    void onResizeEnd();
    void onMoveEnd();
    void onActivateMainWindow();
    void onSaveAs(const QString &base64Image);  //响应qml发来的下载图片文件选择框
    void onOpenFile(int typeNum);  //响应qml发来的添加图片/文档文件选择框
    void onShowAddFileMenu();
    void onAddFileMenuAboutToHide();
    ///
    /// \brief checkIntroduction 查询用户是否同意协议，展示新手引导
    ///
    void checkIntroduction();
    void aboutToQuit();

signals:
    void sigThemeChanged();

private:
    void createAllChildWnd();
    void initCtrl();
    void initSolts();
    void initLayout();
    void switchToWindowMode();
    void switchToSideMode();
    void initTrayIcon();
    void initTrayIconMenu();
    void showView();
    void hideView();
    void initScribeWords();
    void initPosition();
    void initXeventMonitor();
    void changeToOccupiedMode();
    void initScribeWordsSetting();
    void changeToNormalMode();
    void showSettingPage();
    void initShortcut();
    void changeScreenResolution(); // 切换屏幕分辨率修改窗口大小和位置
    QString getAiAssistantVersion();
    void initTimer();
    void initTopping();
    void initGlobalSetting();   //初始化全局设置

protected:
   void closeEvent(QCloseEvent *e) override;


protected:
    void resizeEvent(QResizeEvent *event) override;
    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;
    QPushButton *m_btnOccupiedMode = 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;
    xEventMonitor* m_xeventMonitor = nullptr;
    QQuickWidget *m_mainQml = nullptr;                        // QML主页面
    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 *resizeTimer = nullptr;
    QTimer *moveTimer = nullptr;
    QTimer *selectTimer = nullptr;
    bool m_isMouseDragMove = true; // 是否是鼠标拖动

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

    CloseModeWidget *m_closeModeWidget = nullptr;
};

#endif // CMAINWND_H
