#include "sui_title.h"
sui_title::sui_title(QWidget *parent)
    : QWidget(parent),
      m_menu(nullptr),
      s_menu(nullptr),
      f_menu(nullptr),
      l_menu(nullptr),
      t_menu(nullptr),
      m_type(QD_TYPE::QD_EXIT)
{
    m_parent = parent;
    setObjectName(tr("sui_title"));
    for (int i = 0; i <= m_btn_obj_str.count() - 1; i++)
    {
        QPushButton *btn = new QPushButton(this);
        btn->setAutoFillBackground(true);
        btn->setFlat(true);
        btn->setObjectName(m_btn_obj_str[i]);
        btn->setToolTip(m_btn_tips_str[i]);
        m_btn_vectors.push_back(btn);
    }
    // 标题栏布局
    m_title_label = new QLabel(this);
    m_title_label->setAlignment(Qt::AlignCenter);
    m_title_label->setObjectName(tr("HEAD_STR"));
    QFont font = m_title_label->font();
    font.setPointSize(28);
    m_title_label->setFont(font);
    m_layout = new QHBoxLayout(this);
    m_layout->setAlignment(Qt::AlignCenter);
    m_layout->addWidget(m_btn_vectors[0]);
    m_layout->addItem(sparcer_item);
    m_layout->addWidget(m_title_label);
    m_layout->addItem(sparcer_item);
    m_layout->addWidget(m_btn_vectors[1]);
    m_layout->addWidget(m_btn_vectors[2]);
    m_layout->addWidget(m_btn_vectors[3]);
    m_layout->setSpacing(5);
    m_layout->setContentsMargins(5, 0, 5, 0);
    setMouseTracking(true);
    setFixedHeight(40);
    setLayout(m_layout);
    connect_slots();
}
sui_title::~sui_title()
{
    if (m_thread)
    {
        m_thread->quit();
        m_thread->exit();
    }
}

void sui_title::connect_slots()
{
    for (auto btn : m_btn_vectors)
    {
        connect(btn, SIGNAL(clicked(bool)), this, SLOT(slots_on_clicked()));
    }
}
void sui_title::btn_ico_menu()
{
    m_menu = new QMenu();
    // 外观
    if (!s_menu)
    {
        QActionGroup *s_QGroup = new QActionGroup(this);
        s_menu = new QMenu(tr("外观设置"));
        for (int i = 0; i <= sui_style_table.count() - 1; i++)
        {
            QString sName = QFileInfo(sui_style_table[i]).baseName();
            QAction *s_Action = new QAction(sName, s_QGroup);
            s_Action->setObjectName(QString::number(i));
            s_Action->setCheckable(true);
            if (sui_config.style_system)
            {
                s_Action->setEnabled(false);
            }
            else
            {
                connect(s_Action, SIGNAL(triggered(bool)), this, SLOT(slots_change_style()));
            }
            if (i == sui_config.style)
            {
                s_Action->setChecked(true);
            }
            s_menu->addAction(s_Action);
        }
        QAction *separator = new QAction(nullptr);
        separator->setSeparator(true);
        s_menu->addAction(separator);
        QActionGroup *t_group = new QActionGroup(this);
        QAction *t_action = new QAction("跟随系统", t_group);
        t_action->setObjectName("track_system");
        t_action->setCheckable(true);
        if (sui_config.style_system)
        {
            t_action->setChecked(true);
        }
        connect(t_action, SIGNAL(triggered(bool)), this, SLOT(slots_track_style_off_on_sys()));
        s_menu->addAction(t_action);
    }
    // 字体
    if (!f_menu)
    {
        QActionGroup *f_QGroup = new QActionGroup(this);
        f_menu = new QMenu(tr("字体设置"));
        for (int i = 0; i <= sui_font_table.count() - 1; i++)
        {
            QAction *f_Action = new QAction(sui_font_table[i], f_QGroup);
            f_Action->setObjectName(QString::number(i));
            f_Action->setCheckable(true);
            if (i == sui_config.font)
            {
                f_Action->setChecked(true);
            }
            connect(f_Action, SIGNAL(triggered(bool)), this, SLOT(slots_change_font()));
            f_menu->addAction(f_Action);
        }
        QAction *separator = new QAction(nullptr);
        separator->setSeparator(true);
        f_menu->addAction(separator);
        QActionGroup *b_QGroup = new QActionGroup(this);
        QAction *b_Action = new QAction("字体加粗", b_QGroup);
        b_Action->setObjectName("font_bold");
        b_Action->setCheckable(true);
        if (sui_config.font_bold)
        {
            b_Action->setChecked(true);
        }
        connect(b_Action, SIGNAL(triggered(bool)), this, SLOT(slots_change_font_bold(bool)));
        f_menu->addAction(b_Action);
    }
    // 语言
    if (!l_menu)
    {
        QActionGroup *t_QGroup = new QActionGroup(this);
        l_menu = new QMenu(tr("语言选择"));
        for (int i = 0; i <= sui_lang_table.count() - 1; i++)
        {
            QAction *t_Action = new QAction(sui_lang_table[i], t_QGroup);
            t_Action->setObjectName(QString::number(i));
            t_Action->setCheckable(true);
            if (i == sui_config.lang)
            {
                t_Action->setChecked(true);
            }
            //connect(t_Action, SIGNAL(triggered(bool)), this, SLOT(slots_change_font()));
            l_menu->addAction(t_Action);
        }
    }
    if (!t_menu)
    {
        t_menu = new QMenu(tr("背景透明度")); 
        QWidgetAction *slider_action = new QWidgetAction(t_menu);
        QWidget *s_widget = new QWidget;
        QVBoxLayout *s_layout = new QVBoxLayout();
        QSlider *slider = new QSlider(Qt::Horizontal);
        slider->setRange(0, 100);
        slider->setSingleStep(1);
        s_layout->addWidget(slider);
        s_widget->setLayout(s_layout);
        slider_action->setDefaultWidget(s_widget);
        t_menu->addAction(slider_action);
    } 
    m_menu->addMenu(s_menu);
    m_menu->addMenu(f_menu);
    m_menu->addMenu(l_menu);
    m_menu->addMenu(t_menu);
    m_menu->exec(QCursor::pos());
}
void sui_title::slots_track_style_off_on_sys()
{
    if (sui_config.style_system == 1)
    {
        sui_config.style_system = 0;
        m_style->state = 0;
    }
    else
    {
        sui_config.style_system = 1;
        m_style->state = 1;
    }
}
void sui_title::slots_change_font_bold(bool state)
{
    if (sui_config.font_bold)
    {
        sui_config.font_bold = 0;
    }
    else
    {
        sui_config.font_bold = 1;
    }
    init_font_style();
}
void sui_title::slots_change_font()
{
    if (sender() != nullptr)
    {
        sui_config.font = sender()->objectName().toInt();
    }
    init_font_style();
}
void sui_title::slots_change_style()
{
    if (sender() != nullptr)
    {
        sui_config.style = sender()->objectName().toInt();
    }
    init_font_style();
}
void sui_title::slots_change_style_sys(bool state)
{
    sui_config.style = 0;
    if (state)
    {
        sui_config.style = 1;
    }
    init_font_style();
}
void sui_title::init_font_style()
{
    QString style;
    QString qss_font = "QWidget { ";
    qss_font.append("font-family: '" + sui_font_table[sui_config.font] + "';");
    if (sui_config.font_bold)
    {
        qss_font.append("font-weight: bold;");
    }
    qss_font.append("}");

    QString style_file = tr(":/res/qss/") + sui_style_table[sui_config.style];
    QFile qss(style_file);
    if (qss.open(QFile::ReadOnly))
    {
        style = QLatin1String(qss.readAll());
        style.append(qss_font);
    }
    qss.close();
    QApplication *main_app = static_cast<QApplication *>(QCoreApplication::instance());
    main_app->setStyleSheet(style);
}

// 双击标题栏进行界面的最大化/还原
void sui_title::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event); // 没有实质性的作用，只是用来允许event可以不使用，用来避免编译器警告
    emit m_btn_vectors[2]->clicked();
}
// 进行最小化、最大化/还原、关闭操作
void sui_title::slots_on_clicked()
{
    if (sender()->objectName() == "btn_opt")
    {
        btn_ico_menu();
    }
    else if (sender()->objectName() == "btn_min")
    {
        window()->showMinimized();
    }
    else if (sender()->objectName() == "btn_max")
    {
        window()->isFullScreen() ? window()->showNormal() : window()->showFullScreen();
        up_maximize();
    }
    else if (sender()->objectName() == "btn_close")
    {
        if (m_type == QD_TYPE::QD_CLOSE)
        {
            emit signals_close();
        }
        else if (m_type == QD_TYPE::QD_EXIT)
        {
            // 保存窗体设置
            sui_save_json();
            QApplication::quit();
        }
    }
}
// 最大化/还原
void sui_title::up_maximize()
{
    QWidget *pWindow = this->window(); // 获得标题栏所在的窗口
    if (pWindow->isWindow())
    {
        bool bMaximize = pWindow->isFullScreen(); // 判断窗口是不是最大化状态，是则返回true，否则返回false
        if (bMaximize)
        {
            // 目前窗口是最大化状态，则最大化/还原的toolTip设置为"Restore"
            m_btn_vectors[2]->setToolTip(tr("Restore"));
            // 设置按钮的属性名为"maximizeProperty"
            m_btn_vectors[2]->setProperty("maximizeProperty", tr("restore"));
        }
        else
        {
            // 目前窗口是还原状态，则最大化/还原的toolTip设置为"Maximize"
            m_btn_vectors[2]->setToolTip(tr("Maximize"));
            // 设置按钮的属性名为"maximizeProperty"
            m_btn_vectors[2]->setProperty("maximizeProperty", tr("maximize"));
        }
        m_btn_vectors[2]->style()->unpolish(m_btn_vectors[2]);
        m_btn_vectors[2]->style()->polish(m_btn_vectors[2]);
    }
}
void sui_title::set_type(QD_TYPE _type)
{
    m_type = _type;
    if (m_type == QD_TYPE::QD_EXIT)
    {
        // 启动系统主题监听线程
        m_style = new sui_sys_style();
        m_thread = new QThread();
        m_style->moveToThread(m_thread);
        m_style->state = sui_config.style_system;
        connect(m_style, SIGNAL(signals_sys_style_change(bool)), this, SLOT(slots_change_style_sys(bool)));
        connect(m_thread, SIGNAL(started()), m_style, SLOT(slots_sys_style_thread()));
        m_thread->start();
    }
    else
    {
        disconnect(m_btn_vectors[0], SIGNAL(clicked(bool)), this, SLOT(slots_on_clicked()));
        m_btn_vectors[1]->setVisible(false);
        m_btn_vectors[2]->setVisible(false);
    }
    init_font_style();
}
void sui_title::set_title(QString str)
{
    m_title_label->setText(str);
}