/*
 * Copyright 2025 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 "modelwidget.h"
#include <QLabel>
#include <QMimeData>
#include <QMouseEvent>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QVBoxLayout>
#include <aiconfig/modelconfig.h>
#include <ukui-log4qt.h>
#include <QFontMetrics>

#include "util.h"

#define THEME_QT_SCHEMA  "org.ukui.style"

ModelWidget::ModelWidget(const QString &modelName, const QString &modelIntro, DeployType deployType, QWidget *parent)
    : QWidget(parent), m_deployType(deployType)
{
    this->setAttribute(Qt::WA_DeleteOnClose);

    m_modelLayout = new QVBoxLayout;
    m_modelLayout->setSpacing(0);
    m_modelLayout->setContentsMargins(0, 0, 0, 0);

    m_sortButton = new QPushButton(this);
    m_sortButton->setAccessibleName("kylin-ai-subsystem-modelconfig_QPushButton_m_sortButton");
    m_sortButton->setFixedSize(QSize(36, 36));
    m_sortButton->setToolTip(tr("Increase Priority"));
    m_sortButton->setFocusPolicy(Qt::NoFocus);
    m_sortButton->setIcon(QIcon::fromTheme("go-up-symbolic"));
    m_sortButton->setProperty("useIconHighlightEffect", 0x2);
    m_sortButton->setFlat(true);

    modelNameLabel = new QLabel(modelName, this);
    modelNameLabel->setAccessibleName("kylin-ai-subsystem-modelconfig_QLabel_modelNameLabel");
    modelNameLabel->setAlignment(Qt::AlignLeft);

    modelIntroLabel = new kdk::KLabel(this);
    modelIntroLabel->setAccessibleName("kylin-ai-subsystem-modelconfig_kdk::KLabel_modelIntroLabel");
    modelIntroLabel->setText(modelIntro);
    modelIntroLabel->setAlignment(Qt::AlignLeft);
    setKLabelFontColor(modelIntroLabel, "#808080");

    m_modelLayout->addStretch();
    m_modelLayout->addWidget(modelNameLabel);
    m_modelLayout->addWidget(modelIntroLabel);
    m_modelLayout->addStretch();

    configButton = new kdk::KBorderlessButton; // bug #288117 加大小设置会导致切换字体/字体大小显示"..."
    configButton->setAccessibleName("kylin-ai-subsystem-modelconfig_kdk::KBorderlessButton_configButton");
    configButton->setText(tr("Configure"));

    modelSwitchBtn = new KSwitchButton(this);
    modelSwitchBtn->setAccessibleName("kylin-ai-subsystem-modelconfig_kdk::KSwitchButton_modelSwitchBtn");
    modelSwitchBtn->setCheckable(true);
    modelSwitchBtn->setFixedSize(50, 24);

    ModelConfig mc;
    // 获取当前模型的部署是否使能
    modelSwitchBtn->setChecked(mc.getModelDeployEnabled(deployType));

    m_layout = new QHBoxLayout(this);
    m_layout->setContentsMargins(8, 0, 16, 0);
    m_layout->setSpacing(0);
    m_layout->addWidget(m_sortButton);
    m_layout->addSpacing(8);
    m_layout->addLayout(m_modelLayout);
    m_layout->addStretch();
    m_layout->addSpacing(24);
    m_layout->addWidget(configButton);
    m_layout->addSpacing(8);
    m_layout->addWidget(modelSwitchBtn);
    setLayout(m_layout);

    QFontMetrics modelNameLabelFontMetrics(modelNameLabel->font());
    QFontMetrics modelIntroLabelFontMetrics(modelIntroLabel->font());
    this->setFixedHeight(modelIntroLabelFontMetrics.height() + modelNameLabelFontMetrics.height() + 32);

    initConnect();
}

ModelWidget::~ModelWidget()
{

}

QSize ModelWidget::sizeHint() const
{
    return  this->size();
}

void ModelWidget::initConnect()
{
    connect(m_sortButton, &QPushButton::clicked, this, &ModelWidget::onSortButtonClicked);
    connect(modelSwitchBtn, &KSwitchButton::stateChanged, this, &ModelWidget::onTransModelDeployStatusChanged);
    connect(configButton, &QPushButton::clicked, this, [this](){
        emit configClicked(modelWidgetName);
    });

    const QByteArray idd(THEME_QT_SCHEMA);
    if (!QGSettings::isSchemaInstalled(idd)) {
        qWarning() << idd << " is not installed.";
        return;
    }
    QGSettings *qtSettings  = new QGSettings(idd, QByteArray(), this);
    connect(qtSettings, &QGSettings::changed, this, [this](const QString &key) {
        if (key == "systemFontSize") {
            QFontMetrics modelNameLabelFontMetrics(modelNameLabel->font());
            QFontMetrics modelIntroLabelFontMetrics(modelIntroLabel->font());
            this->setFixedHeight(modelIntroLabelFontMetrics.height() + modelNameLabelFontMetrics.height() + 32);
        }
    });
}

void ModelWidget::onTransModelDeployStatusChanged(bool enable)
{
    ModelConfig mc;
    // 设置部署策略并检查是否成功
    bool ret = mc.setModelDeployEnabled(m_deployType, enable);

    //错误处理
    if (!ret) {
        KyInfo() << "Error: Failed to modify PublicCloud model deployment strategy.";
    }

    return;
}

void ModelWidget::onSortButtonClicked() {
    emit sortClicked(); // 发送排序点击信号
}
