#include "cloudmodelitem.h"

ModelItem::ModelItem(kyai::config::model::AiCapability capability,
                     QString modelName, bool isTrialAccount, QWidget *parent)
    : QWidget(parent),
      m_capability(capability),
      m_modelName(modelName),
      m_isTrialAccount(isTrialAccount) {
    initLayout();
    if (!m_isTrialAccount) {
        initMenu();
    }
}

ModelItem::~ModelItem() {}

void ModelItem::initLayout() {
    initNameLabel();
    if (!m_isTrialAccount) {
        initMoreButton();
    }
    initSelectedIconLabel();

    QFrame *frame = new QFrame(this);
    frame->setAccessibleName("kylin-ai-subsystem-modelconfig_QFrame_frame");
    frame->setFixedSize(16,16);
    QHBoxLayout *frameLayout = new QHBoxLayout(frame);
    frameLayout->setMargin(0);
    frame->setLayout(frameLayout);
    frameLayout->addWidget(m_selectedIconLabel);
    m_selectedIconLabel->setVisible(false);

    QHBoxLayout *hLayout = new QHBoxLayout(this);
    hLayout->setContentsMargins(16, 10, 16, 10);
    hLayout->addWidget(m_nameLabel);
    hLayout->addStretch();
    if (!m_isTrialAccount) {
        hLayout->addWidget(m_moreButton);
    }
    hLayout->addWidget(frame);
    this->setLayout(hLayout);
}

void ModelItem::initNameLabel() {
    m_nameLabel = new QLabel(this);
    m_nameLabel->setAccessibleName("kylin-ai-subsystem-modelconfig_QLabel_m_nameLabel");
    m_nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    m_nameLabel->setText(m_modelName);
    if (m_isTrialAccount) {
        m_nameLabel->setContentsMargins(16, 0, 0, 0);
    }
}

void ModelItem::initMoreButton() {
    m_moreButton = new QToolButton(this);
    m_moreButton->setAccessibleName("kylin-ai-subsystem-modelconfig_QToolButton_m_moreButton");
    m_moreButton->setFixedSize(38, 38);
    m_moreButton->setProperty("useButtonPalette", true);
    m_moreButton->setAutoRaise(true);
    m_moreButton->setPopupMode(QToolButton::InstantPopup);
    m_moreButton->setIcon(QIcon::fromTheme("view-more-horizontal-symbolic"));
    m_moreButton->setFocusPolicy(Qt::NoFocus);
}

void ModelItem::initSelectedIconLabel() {
    m_selectedIconLabel = new QLabel();
    m_selectedIconLabel->setAccessibleName("kylin-ai-subsystem-modelconfig_QLabel_m_selectedIconLabel");
    QIcon selectIcon = QIcon::fromTheme("ukui-selected");
    m_selectedIconLabel->setPixmap(selectIcon.pixmap(selectIcon.actualSize(QSize(16, 16))));
    const QByteArray id(THEME_QT_SCHEMA);
    QGSettings *mQtSettings = new QGSettings(id, QByteArray(), this);
    connect(mQtSettings, &QGSettings::changed, this, [=](QString key) {
        if (key == "iconThemeName")
            m_selectedIconLabel->setPixmap(selectIcon.pixmap(selectIcon.actualSize(QSize(16, 16))));
    });
}

void ModelItem::initMenu() {
    m_menu = new QMenu();
    m_menu->setAccessibleName("kylin-ai-subsystem-modelconfig_QMenu_m_menu");
    m_moreButton->setMenu(m_menu);

    QAction *actionEdit = new QAction(QIcon::fromTheme("document-edit-symbolic"), tr("Edit"));
    //actionEdit->setDisabled(true);
    connect(actionEdit, &QAction::triggered, [this]{Q_EMIT sigEdit(m_capability, m_modelName);});

    QAction *actionDelete = new QAction(QIcon::fromTheme("edit-delete-symbolic"), tr("Delete"));
    connect(actionDelete, &QAction::triggered, [this]{Q_EMIT sigDelete(m_capability, m_modelName);});

    m_menu->addAction(actionEdit);
    m_menu->addAction(actionDelete);
}

void ModelItem::showSelectedIcon(bool flag) {
    m_selectedIconLabel->setVisible(flag);
}

bool ModelItem::isSelected() {
    return m_selectedIconLabel->isVisible();
}

AiCapability ModelItem::getAICapability() {
    return m_capability;
}

QString ModelItem::getModelName() {
    return m_modelName;
}

bool ModelItem::isTrialModel() {
    return m_isTrialAccount;
}
