#include "window.h"
#include "UI/CBTableView.h"

#include <QGraphicsView>
#include <QHBoxLayout>
#include <QMouseEvent>

#include "ElaContentDialog.h"
#include "ElaDef.h"
#include "ElaDockWidget.h"
#include "ElaEventBus.h"
#include "ElaLog.h"
#include "ElaMenu.h"
#include "ElaMenuBar.h"
#include "ElaProgressBar.h"
#include "ElaStatusBar.h"
#include "ElaText.h"
#include "ElaTheme.h"
#include "ElaToolBar.h"
#include "ElaToolButton.h"

#ifdef Q_OS_WIN
    #include "ElaApplication.h"
    #include <QTimer>
#endif

MainWindow::MainWindow(QWidget *parent) : ElaWindow(parent) { initWindow(); }

MainWindow::~MainWindow() {}

void MainWindow::initWindow() {
    // 调整窗口
    setNavigationBarDisplayMode(ElaNavigationType::NavigationDisplayMode::Minimal);
    resize(480, 640);
    setIsFixedSize(true);

    // 调整托盘
    createActions();
    createTrayIcon();

    // 设置图标
    setIcon();
    trayIcon->show();

    // 设置窗口页面内容
    // TODO: Set the subtitle of user info card
    setUserInfoCardSubTitle("Nothing!");
    _tablePage = new CBTableView(this);

    addPageNode("剪贴板历史", _tablePage, ElaIconType::TableTree);

    // this->setIsDefaultClosed(false);
    // connect(this, &MainWindow::closeButtonClicked, this, [=]() { setVisible(false); });
}

void MainWindow::createActions() {
    minimizeAction = new QAction(tr("Mi&nimize"), this);
    connect(minimizeAction, &QAction::triggered, this, &QWidget::hide);

    maximizeAction = new QAction(tr("Ma&ximize"), this);
    connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized);

    restoreAction = new QAction(tr("&Restore"), this);
    connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);

    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
}

void MainWindow::createTrayIcon() {
    trayMenu = new QMenu(this);
    trayMenu->addAction(minimizeAction);
    trayMenu->addAction(maximizeAction);
    trayMenu->addAction(restoreAction);
    trayMenu->addSeparator();
    trayMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayMenu);
}

// TODO: 添加正式图标
void MainWindow::setIcon() {
    QIcon icon(":/assets/icon.png");
    setWindowIcon(icon);
    trayIcon->setIcon(icon);
}
