/*
 * 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 "modelcallpriority.h"

#include <ukui-log4qt.h>

#include <QHBoxLayout>
#include <QLabel>

using namespace kyai::config::model;

namespace {
const QString& cloudModelText() {
    static const QString text = QObject::tr("Cloud Model First (Kylin Certification)");
    return text;
}

const QString& localModelText() {
    static const QString text = QObject::tr("Local Model First (Kylin Certification)");
    return text;
}

const QString& privateModelText() {
    static const QString text = QObject::tr("Custom Model First");
    return text;
}

const QMap<QString, DeployType>& modelTypeMap() {
    static const QMap<QString, DeployType> map = {{cloudModelText(), DeployType::PublicCloud},
                                                  {localModelText(), DeployType::Ondevice},
                                                  {privateModelText(), DeployType::Custom}};
    return map;
}
}  // namespace

ModelCallPriority::ModelCallPriority(bool hasLocalModel, QWidget* parent)
    : m_hasLocalModel(hasLocalModel), QWidget(parent) {
    m_modelCallPriorityLabel = new kdk::KLabel(parent);
    m_modelCallPriorityLabel->setText(tr("Model Call Priority"));
    m_modelCallPriorityLabel->setAccessibleName("kylin-ai-subsystem-modelconfig_QLabel_modelCallPriorityLabel");

    m_modelPriorityComboBox = new QComboBox(parent);
    m_modelPriorityComboBox->setAccessibleName("kylin-ai-subsystem-modelconfig_QComboBox_m_modelCallPriorityLabel");
    m_modelPriorityComboBox->setMinimumSize(320, 36);

    m_modelPriorityComboBox->addItem(cloudModelText());
    if (m_hasLocalModel) {
        m_modelPriorityComboBox->addItem(localModelText());
    }
    m_modelPriorityComboBox->addItem(privateModelText());

    QHBoxLayout* modelCellPriLayout = new QHBoxLayout();
    modelCellPriLayout->setContentsMargins(16, 0, 16, 0);
    modelCellPriLayout->setSpacing(2);

    modelCellPriLayout->addWidget(m_modelCallPriorityLabel);
    modelCellPriLayout->addWidget(m_modelPriorityComboBox);

    parent->setLayout(modelCellPriLayout);

    initConnections();
}

void ModelCallPriority::setModelCallPriority(kyai::config::model::DeployType deployType) {
    if (deployType == DeployType::PublicCloud) {
        m_modelPriorityComboBox->setCurrentText(cloudModelText());
    } else if (deployType == DeployType::Ondevice) {
        m_modelPriorityComboBox->setCurrentText(localModelText());
    } else if (deployType == DeployType::Custom) {
        m_modelPriorityComboBox->setCurrentText(privateModelText());
    }
}

void ModelCallPriority::initConnections() {
    connect(m_modelPriorityComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(modelPriorityChanged(int)));
}

void ModelCallPriority::modelPriorityChanged(int index) {
    Q_UNUSED(index);
    QString comboBoxCurText = m_modelPriorityComboBox->currentText();
    DeployType type = modelTypeMap().value(comboBoxCurText, DeployType::PublicCloud);
    emit topModelPriorityChanged(type);
}
