#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)
{
    this->setObjectName("kylin-burner_WindowTitle");
    ui->setupUi(this);

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

    ui->close->setAccessibleName("kylin-burner_QPushButton_WindowTitle_close");
    ui->logo->setAccessibleName("kylin-burner_QLabel_WindowTitle_logo");
    ui->max->setAccessibleName("kylin-burner_QPushButton_WindowTitle_max");
    ui->menu->setAccessibleName("kylin-burner_QToolButton_WindowTitle_menu");
    ui->min->setAccessibleName("kylin-burner_QPushButton_WindowTitle_min");
    ui->title->setAccessibleName("kylin-burner_QLabel_WindowTitle_title");

    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->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(LogicMain::instance(), &LogicMain::exit, [=](){m_bAppRunning = false;});
    connect(LogicMain::instance(), &LogicMain::functionMenuChanged, this, [=](void *d){
        QMenu *menu = static_cast<QMenu *>(d);
        if (menu)
        {
            this->ui->menu->setMenu(menu);
        }

    });
    setFocusPolicy(Qt::NoFocus);

    slotChangeUIFontSize(LogicMain::instance()->GetSystemFontSize());
    connect(LogicMain::instance(), &LogicMain::systemFontSizeChanged, this, &WindowTitle::slotChangeUIFontSize);
}

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::setIconAndTitle(QIcon icon, QString title)
{
    ui->logo->setPixmap(icon.pixmap(ui->logo->size()));
    ui->title->setText(title);
}

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

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

void WindowTitle::slotChangeUIFontSize(int fontSize)
{
    int scaleRatio = qAbs(fontSize - 10.0);
    QFont font = ui->title->font();
    font.setPixelSize(14 + scaleRatio);
    ui->title->setFont(font);
}

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

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

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

void WindowTitle::slotsSetCloseEnabel(bool e)
{
    qDebug() << "(WindowTitle::slotsSetCloseEnabel) 设置Close状态：" << e;
    ui->close->setEnabled(e);
}

bool WindowTitle::eventFilter(QObject *watched, QEvent *event)
{
    QMouseEvent *mouseEvent = nullptr;
    switch (event->type())
    {
    case QEvent::MouseButtonDblClick:
        mouseEvent = static_cast<QMouseEvent *>(event);
        if ( (watched == ui->logo || watched == ui->title || watched == this)
             && (mouseEvent->button() == Qt::LeftButton) ) emit max();
        break;
    default:
        break;
    }

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

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