#include "modelcallpriority.h"
#include <QLabel>
#include <QHBoxLayout>
#include <ukui-log4qt.h>

using namespace kyai::config::model;

ModelCallPriority::ModelCallPriority(QWidget *parent) : QWidget(parent)
{
    modelCellPriLabel = new QLabel(tr("Model call priority"), this);
    modelCellPriLabel->setAccessibleName("kylin-ai-subsystem-modelconfig_QLabel_modelCellPriLabel");
    modelCellPriLabel->setAlignment(Qt::AlignLeft |Qt::AlignVCenter);

    m_modelCallPriComboBox = new QComboBox; // 云端，本地
    m_modelCallPriComboBox->setAccessibleName("kylin-ai-subsystem-modelconfig_QComboBox_m_modelCallPriComboBox");
    m_modelCallPriComboBox->setFixedSize(410, 36);

    m_modelCallPriComboBox->addItem(tr("Cloud model first"));
    m_modelCallPriComboBox->addItem(tr("Local model first"));
    m_modelCallPriComboBox->addItem(tr("Private model first"));

    QHBoxLayout *modelCellPriLayout = new QHBoxLayout(this);

    modelCellPriLayout->setContentsMargins(16, 0, 16, 0);

    modelCellPriLayout->addWidget(modelCellPriLabel);
    modelCellPriLayout->addWidget(m_modelCallPriComboBox);

    connect(m_modelCallPriComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(callPriChanged(int)));

    setLayout(modelCellPriLayout);

    //更新调度优先级数据
    updateModelCallPriority();
}

ModelCallPriority::~ModelCallPriority()
{

}

void ModelCallPriority::updateModelCallPriority()
{
    //获取当前优先级的数据，并且更新
    if(m_mapDeployTypeValue == nullptr)
    {
        m_mapDeployTypeValue = new QMap<kyai::config::model::DeployType ,int >;
    }
    ModelConfig mc;
    int  publicCloudValue  = mc.getModelSchedulingPriority(DeployType::PublicCloud);
    m_mapDeployTypeValue->insert(DeployType::PublicCloud,publicCloudValue);
    KyInfo() << "publicCloudValue" << publicCloudValue;
    int ondeviceValue = mc.getModelSchedulingPriority(DeployType::Ondevice);
    m_mapDeployTypeValue->insert(DeployType::Ondevice,ondeviceValue);
    KyInfo() << "ondeviceValue" <<ondeviceValue;
    int customValue = mc.getModelSchedulingPriority(DeployType::Custom);
    m_mapDeployTypeValue->insert(DeployType::Custom,customValue);
    KyInfo() << "customValue" << customValue;

    QList<int > values = m_mapDeployTypeValue->values();
    KyInfo() << "11111111111111111111111:" << values;
    //排序
    std::sort(values.begin(),values.end());
    KyInfo() << "2222222222222222222222:" << values;

    DeployType type = m_mapDeployTypeValue->key(values.first());
    if(type == DeployType::PublicCloud){
        m_modelCallPriComboBox->setCurrentIndex(0);
    }else if(type == DeployType::Ondevice){
        m_modelCallPriComboBox->setCurrentIndex(1);
    }else if(type == DeployType::Custom){
        m_modelCallPriComboBox->setCurrentIndex(2);
    }

}

void ModelCallPriority::callPriChanged(int index)
{
    KyInfo() << "callPriChanged " << index;

    ModelConfig mc;
    bool ret = false;

    if (index == 0) {
        // 云端模型优先
        ret = mc.setModelSchedulingPriority(DeployType::PublicCloud, 1);
        if (ret == false) {
            KyCritical() << "Failed to set the call priority of cloud model.";
            return;
        }

        ret = mc.setModelSchedulingPriority(DeployType::Ondevice, 2);
        if (ret = false) {
            KyCritical() << "Failed to set the call priority of local model.";
        }

        ret = mc.setModelSchedulingPriority(DeployType::Custom, 3);
        if (ret = false) {
            KyCritical() << "Failed to set the call priority of custom model.";
        }

        return;
    }

    if (index == 1) {
        // 本地模型优先
        ret = mc.setModelSchedulingPriority(DeployType::Ondevice, 1 );
        if (ret == false) {
            KyCritical() << "Failed to set the call priority of local model.";
            return;
        }

        ret = mc.setModelSchedulingPriority(DeployType::PublicCloud, 2);
        if (ret == false) {
            KyCritical() << "Failed to set the call priority of cloud model.";
        }

        ret = mc.setModelSchedulingPriority(DeployType::Custom, 3);
        if (ret = false) {
            KyCritical() << "Failed to set the call priority of custom model.";
        }
        return;
    }

    if( index = 2){
         // 自定义模型优先
        ret = mc.setModelSchedulingPriority(DeployType::Custom, 1 );
        if (ret == false) {
            KyCritical() << "Failed to set the call priority of local model.";
            return;
        }

        ret = mc.setModelSchedulingPriority(DeployType::PublicCloud, 2);
        if (ret == false) {
            KyCritical() << "Failed to set the call priority of cloud model.";
        }
        ret = mc.setModelSchedulingPriority(DeployType::Ondevice, 3);
        if (ret = false) {
            KyCritical() << "Failed to set the call priority of custom model.";
        }

        return;
    }

    return;
}
