/*
 * 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>

GeneralPage::GeneralPage(QWidget *parent)
    : QWidget{parent}
{
    initLayout();
    initConnection();
    loadConfig();
}

GeneralPage::~GeneralPage()
{

}

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

void GeneralPage::openUkuiControlCenter()
{
    QProcess process;
    QString command = "ukui-control-center";
    QStringList arguments;
    arguments << "-m" << "aisubsystem";
    process.startDetached(command, arguments);
    process.waitForFinished(-1);
    // 检查进程是否正常退出
    if (process.exitStatus() == QProcess::NormalExit) {
        KyInfo() << "ukui-control-center successfully!";
    } else {
        KyInfo() << "Failed  to open ukui-control-center";
    }
}
void GeneralPage::initLayout()
{
    // 常规-模型
    m_modelTitleLabel = new QLabel;
    m_modelTitleLabel->setAccessibleName("kylin-aiassistant_QLabel_m_modelTitleLabel");
    m_modelTitleLabel->setText(tr("Model"));
    m_modelTitleLabel->setContentsMargins(16,0,0,0);

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

    //model setting
    m_modelFrame = new UkccFrame;
    m_modelFrame->setAccessibleName("kylin-aiassistant_UkccFrame_d_ptr->m_modelFrame");
    m_modelFrame->setMinimumSize(QSize(496, 60));
    m_modelFrame->setMaximumSize(QSize(16777215, 60));

    QHBoxLayout *modelHLayout = new QHBoxLayout;
    modelHLayout->setContentsMargins(16, 10, 16, 10);
    m_modelFrame->setLayout(modelHLayout);

    m_modelLabel = new QLabel(tr("ModelSetting"));
    m_modelLabel->setAccessibleDescription("kylin-aiassistant_QLabel_m_modelLabel");

    //跳转按钮
    m_modelSettingButton = new QPushButton(tr("GoToSetting"));
    connect(m_modelSettingButton,&QPushButton::clicked,this,&GeneralPage::openUkuiControlCenter);
    modelHLayout->addWidget(m_modelLabel);
    modelHLayout->addStretch();
    modelHLayout->addSpacing(20);
    modelHLayout->addWidget(m_modelSettingButton);

    m_modelGroup->addWidget(m_modelFrame);

    // 常规-窗口
    m_titleLabel = new QLabel;
    m_titleLabel->setAccessibleName("kylin-aiassistant_QLabel_d_ptr->m_titleLabel");
    m_titleLabel->setText(tr("Window"));
    m_titleLabel->setContentsMargins(16, 0, 0, 0);
    //m_titleLabel->setFrameShape(QFrame::Box);
    m_titleLabel->setObjectName(QString("modelFrame"));

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

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

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

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

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

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

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

    // 创建滚动区域
    m_scrollWidget = new QWidget(this);
    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(m_modelTitleLabel);
    scrollVLayout->addWidget(m_modelGroup);
    scrollVLayout->addSpacing(40);
    scrollVLayout->addWidget(m_titleLabel);
    scrollVLayout->addWidget(m_pageGroup);
    scrollVLayout->addStretch();
    m_scrollWidget->setLayout(scrollVLayout);

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

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



    setLayout(baseVLayout);
}

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

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

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

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

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