#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>

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("Sort Upwards"));
    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);
    modelIntroLabel->setFontColor("#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(8);
    m_layout->addWidget(configButton);
    m_layout->addSpacing(8);
    m_layout->addWidget(modelSwitchBtn);
    setLayout(m_layout);

    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);
    });
    return;
}

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(); // 发送排序点击信号
}
