/*
 * 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/>.
 */
#include "generalpage.h"
#include "global/kaglobal.h"
#include "libukcc/widgets/SettingWidget/settinggroup.h"

#include <QLabel>
#include <QRadioButton>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSharedPointer>
#include <QScrollArea>
#include <QScrollBar>

class GeneralPage::Private
{
public:
    QLabel *m_titleLabel = nullptr;
    SettingGroup *m_pageGroup = nullptr;

    // 窗口选项
    UkccFrame *m_windowFrame = nullptr;
    QLabel *m_windowLabel = nullptr;
    QRadioButton *m_minimizeToTrayRadio = nullptr;
    QRadioButton *m_directExitRadio = nullptr;
    QButtonGroup *m_windowActionGroup = nullptr;

    QWidget *m_scrollWidget = nullptr;
    QScrollArea *m_scrollArea = nullptr;

    bool isMinimizeToTray = true; // 默认最小化到托盘
};

GeneralPage::GeneralPage(QWidget *parent)
    : QWidget{parent},
    d_ptr(make_shared<Private>())
{
    initLayout();
    initConnection();
    loadConfig();
}

GeneralPage::~GeneralPage()
{

}

void GeneralPage::updateCloseMode(bool isHideToTray)
{
    if(isHideToTray){
        d_ptr->m_minimizeToTrayRadio->setChecked(true);
    }
    else {
        d_ptr->m_directExitRadio->setChecked(true);
    }
}

void GeneralPage::initLayout()
{
    // 设置页面标题
    d_ptr->m_titleLabel = new QLabel;
    d_ptr->m_titleLabel->setAccessibleName("kylin-aiassistant_QLabel_d_ptr->m_titleLabel");
    d_ptr->m_titleLabel->setText(tr("Window"));
    d_ptr->m_titleLabel->setContentsMargins(16, 0, 0, 0);

    d_ptr->m_pageGroup = new SettingGroup(this);
    d_ptr->m_pageGroup->setAccessibleName("kylin-aiassistant_SettingGroup_d_ptr->m_pageGroup");

    // 窗口操作区域
    d_ptr->m_windowFrame = new UkccFrame;
    d_ptr->m_windowFrame->setAccessibleName("kylin-aiassistant_UkccFrame_d_ptr->m_windowFrame");
    d_ptr->m_windowFrame->setMinimumSize(QSize(496, 60));
    d_ptr->m_windowFrame->setMaximumSize(QSize(16777215, 60));
    d_ptr->m_windowFrame->setFrameShape(QFrame::Box);
    d_ptr->m_windowFrame->setObjectName(QString("windowFrame"));

    QHBoxLayout *windowHLayout = new QHBoxLayout;
    windowHLayout->setContentsMargins(16, 10, 16, 10);
    d_ptr->m_windowFrame->setLayout(windowHLayout);

    d_ptr->m_windowLabel = new QLabel(tr("Close Window"));
    d_ptr->m_windowLabel->setAccessibleName("kylin-aiassistant_QLabel_d_ptr->m_windowLabel");

    // 单选按钮
    d_ptr->m_minimizeToTrayRadio = new QRadioButton(tr("Hide window"));
    d_ptr->m_minimizeToTrayRadio->setAccessibleName("kylin-aiassistant_QRadioButton_d_ptr->m_minimizeToTrayRadio");
    connect(d_ptr->m_minimizeToTrayRadio,&QRadioButton::clicked,this,[=](){
        g_configManager->updateConfig("is-hide-tray",true);
    });
    d_ptr->m_directExitRadio = new QRadioButton(tr("Exit Directly"));
    d_ptr->m_directExitRadio->setAccessibleName("kylin-aiassistant_QRadioButton_d_ptr->m_directExitRadio");
    connect(d_ptr->m_directExitRadio,&QRadioButton::clicked,this,[=](){
        g_configManager->updateConfig("is-hide-tray",false);
    });
    d_ptr->m_windowActionGroup = new QButtonGroup(this);
    d_ptr->m_windowActionGroup->addButton(d_ptr->m_minimizeToTrayRadio, 0);
    d_ptr->m_windowActionGroup->addButton(d_ptr->m_directExitRadio, 1);

    // 设置布局
    windowHLayout->addWidget(d_ptr->m_windowLabel);
    windowHLayout->addStretch();
    windowHLayout->addWidget(d_ptr->m_minimizeToTrayRadio);
    windowHLayout->addSpacing(20);
    windowHLayout->addWidget(d_ptr->m_directExitRadio);

    // 将窗口框架添加到设置组
    d_ptr->m_pageGroup->addWidget(d_ptr->m_windowFrame);

    // 创建滚动区域
    d_ptr->m_scrollWidget = new QWidget(this);
    d_ptr->m_scrollWidget->setAccessibleName("kylin-aiassistant_QWidget_d_ptr->m_scrollWidget");
    QVBoxLayout *scrollVLayout = new QVBoxLayout;
    scrollVLayout->setContentsMargins(16, 0, 16, 0);
    scrollVLayout->setSpacing(8);
    scrollVLayout->addWidget(d_ptr->m_titleLabel);
    scrollVLayout->addWidget(d_ptr->m_pageGroup);
    scrollVLayout->addStretch();
    d_ptr->m_scrollWidget->setLayout(scrollVLayout);

    d_ptr->m_scrollArea = new QScrollArea(this);
    d_ptr->m_scrollArea->setAccessibleName("kylin-aiassistant_QScrollArea_d_ptr->m_scrollArea");
    d_ptr->m_scrollArea->setFixedSize(528, 404);
    d_ptr->m_scrollArea->setWidgetResizable(true);
    d_ptr->m_scrollArea->setFocusPolicy(Qt::NoFocus);
    d_ptr->m_scrollArea->setFrameShape(QFrame::NoFrame);
    d_ptr->m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    d_ptr->m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    d_ptr->m_scrollArea->verticalScrollBar()->setProperty("drawScrollBarGroove", false);
    d_ptr->m_scrollArea->setWidget(d_ptr->m_scrollWidget);

    // 创建主布局
    QVBoxLayout *baseVLayout = new QVBoxLayout;
    baseVLayout->setContentsMargins(0, 0, 0, 0);
    baseVLayout->setSpacing(0);
    baseVLayout->addWidget(d_ptr->m_scrollArea);
    baseVLayout->addStretch();

    d_ptr->m_scrollWidget->setObjectName("generaPageWidget");


    setLayout(baseVLayout);
}

void GeneralPage::initConnection()
{
    connect(d_ptr->m_windowActionGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
            this, &GeneralPage::onWindowActionChanged);
}

void GeneralPage::loadConfig()
{
    // 从配置管理器获取配置
    d_ptr->isMinimizeToTray = g_configManager->getConfigValue("is-hide-tray").toBool();

    // 设置初始选中状态
    if (d_ptr->isMinimizeToTray) {
        d_ptr->m_minimizeToTrayRadio->setChecked(true);
    } else {
        d_ptr->m_directExitRadio->setChecked(true);
    }
}

void GeneralPage::onWindowActionChanged(int id)
{
    bool isMinimize = (id == 0);
    if (d_ptr->isMinimizeToTray != isMinimize) {
        d_ptr->isMinimizeToTray = isMinimize;
        // 保存配置
        g_configManager->updateConfig("is-hide-tray", d_ptr->isMinimizeToTray);

        // 发送信号通知主程序
        Q_EMIT sigMinimizeToTrayChanged(d_ptr->isMinimizeToTray);
    }
}
