#include "windowtitle.h"
#include "ui_windowtitle.h"
#include "src/model/logicmain.h"
#include "src/po/translation.h"
#include "src/frame/logger.h"

#include <QEvent>
#include <QMouseEvent>
#include <QDebug>

#define ICON_SIZE QSize(16, 16)

WindowTitle::WindowTitle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WindowTitle),
    m_bShowTitle(true),
    m_bAppRunning(true)/*,
    //取消右键菜单栏
    m_oMenu(new QMenu(this)),
    m_oActionClose(new QAction(QIcon::fromTheme("window-close-symbolic"), _("Close"), m_oMenu)),
    m_oActionMin(new QAction(QIcon::fromTheme("window-minimize-symbolic"), _("Minimize"), m_oMenu)),
    m_oActionMax(new QAction(QIcon::fromTheme("window-maximize-symbolic"), _("Maximize"), m_oMenu)),
    m_oActionSelf(new QAction(m_oMenu))*/
{
    QFont f;
    ui->setupUi(this);

    // 日志记录打印
    connect(this, SIGNAL(info(QString)), LOGGER, SLOT(info(QString)));
    connect(this, SIGNAL(warn(QString)), LOGGER, SLOT(warn(QString)));
    connect(this, SIGNAL(error(QString)), LOGGER, SLOT(error(QString)));
    connect(this, SIGNAL(debug(QString)), LOGGER, SLOT(debug(QString)));

    /*
    //m_oMenu->addAction(m_oActionSelf);
    m_oMenu->addAction(m_oActionMin);
    connect(m_oActionMin, SIGNAL(triggered(bool)), this, SIGNAL(min()));
    m_oMenu->addAction(m_oActionMax);
    connect(m_oActionMax, SIGNAL(triggered(bool)), this, SIGNAL(max()));
    m_oMenu->addAction(m_oActionClose);
    connect(m_oActionClose, SIGNAL(triggered(bool)), this, SIGNAL(close()));
    */

    ui->logo->installEventFilter(this);
    ui->title->installEventFilter(this);

    f = ui->title->font();
    f.setPixelSize(14);
    ui->title->setFont(f);

    ui->close->setProperty("isWindowButton", 0x2);
    ui->close->setToolTip(_("Close"));
    ui->min->setProperty("isWindowButton", 0x1);
    ui->min->setToolTip(_("Min"));
    ui->max->setProperty("isWindowButton", 0x1);
    ui->max->setToolTip(_("Max"));
    ui->menu->setProperty("isWindowButton", 0x1);
    ui->menu->setToolTip(_("Options"));

    //ui->close->setProperty("useIconHighlightEffect", 0x8);
    //ui->min->setProperty("useIconHighlightEffect", 0x8);
    //ui->max->setProperty("useIconHighlightEffect", 0x8);
    //ui->menu->setProperty("useIconHighlightEffect", 0x8);
    ui->menu->setPopupMode(QToolButton::InstantPopup);

    ui->close->setFocusPolicy(Qt::NoFocus); // 当为子窗口时，避免与回车关闭窗口冲突

    // 统一图标文件大小
    ui->close->setIconSize(ICON_SIZE);
    ui->max->setIconSize(ICON_SIZE);
    ui->min->setIconSize(ICON_SIZE);
    ui->menu->setIconSize(ICON_SIZE);

    // 采用系统图标
    ui->close->setIcon(QIcon::fromTheme("window-close-symbolic"));
    ui->min->setIcon(QIcon::fromTheme("window-minimize-symbolic"));
    ui->max->setIcon(QIcon::fromTheme("window-maximize-symbolic"));
    ui->menu->setIcon(QIcon::fromTheme("open-menu-symbolic")); // 特定此主题menu图标，否则会有下拉箭头

    connect(ui->close, SIGNAL(clicked(bool)), this, SIGNAL(close()));
    connect(ui->max, SIGNAL(clicked(bool)), this, SIGNAL(max()));
    connect(ui->min, SIGNAL(clicked(bool)), this, SIGNAL(min()));
    connect(MAIN, &LogicMain::exit, [=](){m_bAppRunning = false;});
    connect(MAIN, &LogicMain::menu, this, [=](void *d){
        QMenu *menu = static_cast<QMenu *>(d);
        if(menu==NULL)
        {
            error(_("Geting Menu is NULL , click menu is null"));
        }
        this->ui->menu->setMenu(menu);
        // if (menu && m_bAppRunning)
    });
    setFocusPolicy(Qt::NoFocus);
}

void WindowTitle::onlyShowButton()
{
    ui->logo->hide(); ui->title->setText("");
    m_bShowTitle = false;
}

void WindowTitle::onlyShowLogo()
{
    ui->close->hide();ui->min->hide();ui->max->hide();ui->menu->hide();
}

void WindowTitle::onlyShowInDialog()
{
    ui->min->hide();ui->max->hide();ui->menu->hide();
}

void WindowTitle::onlyVisibleClose(bool b)
{
	ui->close->setVisible(b);
}

void WindowTitle::onWindowIconChange(QIcon icon)
{
    //m_oActionSelf->setIcon(icon);
    ui->logo->setPixmap(icon.pixmap(ui->logo->size()));
}

void WindowTitle::onWindowTitleChange(QString title)
{
    //m_oActionSelf->setText(title);
    if (m_bShowTitle) ui->title->setText(title);
}

void WindowTitle::onWindowShowMaxed(bool b)
{
    if (b)
    {
        //m_oActionMax->setIcon(QIcon::fromTheme("window-restore-symbolic"));
        //m_oActionMax->setText(_("Restore"));
        ui->max->setIcon(QIcon::fromTheme("window-restore-symbolic"));
        ui->max->setToolTip(_("Restore"));

    }
    else
    {
        //m_oActionMax->setIcon(QIcon::fromTheme("window-maximize-symbolic"));
        //m_oActionMax->setText(_("Maximize"));
        ui->max->setIcon(QIcon::fromTheme("window-maximize-symbolic"));
        ui->max->setToolTip(_("Max"));
    }
}

void WindowTitle::setIsTabletMode(bool tablet)
{
    ui->max->setVisible(!tablet);
}

bool WindowTitle::eventFilter(QObject *watched, QEvent *event)
{
    //QMouseEvent *mouseEvent = nullptr;
    switch (event->type())
    {
    case QEvent::MouseButtonDblClick:
        if (watched == ui->logo || watched == ui->title || watched == this) emit max();
        break;
    /*  取消右键菜单栏（右键点击事件）
    case QEvent::MouseButtonPress:
        mouseEvent = static_cast<QMouseEvent *>(event);
        if ((watched == ui->logo) ||
                ((watched == ui->title || watched == this)
                 && mouseEvent->button() == Qt::RightButton))
        {
            m_oMenu->move(mapToGlobal(mouseEvent->pos()));
            m_oMenu->show();
        }
        break;
    */
    default:
        break;
    }

    return QWidget::eventFilter(watched, event);
}

WindowTitle::~WindowTitle()
{
    delete ui;
}
