/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#ifndef CMAINWND_H
#define CMAINWND_H

#include <QObject>
#include <QWidget>
#include <QTextEdit>
#include <QLabel>
#include <QJsonParseError>
#include <QJsonObject>
#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 <kysdk/applications/kwidget.h>
#include <ukui-log4qt.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 <kysdk/kysdk-system/libkydatacollect.h>
#include <ukuistylehelper/ukuistylehelper.h>
#include <ukuiwindowhelper/ukuiwindowhelper.h>
#include <unistd.h>
#include <windowmanager/windowmanager.h>
#include <custommessagebox.h>
#include "global/kaglobal.h"
#include "introductionview.h"
#include "kaboutdialog.h"
#include <QShortcut>
#include <QKeySequence>
#include <QDesktopServices>
#include <QUrl>

using namespace kdk;

#define SIDEBAR_WIDTH                    402

class CPushButtonEx;

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

class CMainWnd : public KWidget
{
    Q_OBJECT
public:

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

    void checkTheme();
    IntroductionView * m_introductionView = nullptr;    //新手引导页面
    void keepWindowAbove(bool needTokeepAbove);

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 onResizeEnd();
    void onMoveEnd();
    void onActivateMainWindow();
    void onSaveAs(const QString &base64Image);  //响应qml发来的下载图片文件选择框
    void onOpenFile(int typeNum);  //响应qml发来的添加图片/文档文件选择框

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 initScribeWordsSetting();
    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;
    quint64 m_window_id = 0;
    bool isMsgBoxOpen = 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主页面
    QAction *m_actionShowScribeWord = nullptr;                //划词快捷键
    QAction *m_actionShowView = 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
};

#endif // CMAINWND_H
