#include "privatemodelitemwidget.h"

PrivateModelItemWidget::PrivateModelItemWidget(kyai::config::model::AiCapability capability, QString modelName, QWidget *parent)
    : QWidget(parent)
{
    m_capability = capability;
    m_modelName = modelName;
    initLayout();
    initMenu();
}

PrivateModelItemWidget::~PrivateModelItemWidget()
{

}

void PrivateModelItemWidget::initLayout()
{
    m_nameLabel = new QLabel;
    m_nameLabel->setAccessibleName("kylin-ai-subsystem-modelconfig_QLabel_m_nameLabel");
    m_nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    m_nameLabel->setText(m_modelName);
    m_moreButton = new QToolButton;
    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);

    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))));
    });
    QFrame *frame = new QFrame(this);
    frame->setFixedSize(16,16);
    QHBoxLayout *frameLayout = new QHBoxLayout;
    frameLayout->setMargin(0);
    frame->setLayout(frameLayout);
    frameLayout->addWidget(m_selectedIconLabel);
    m_selectedIconLabel->setVisible(false);

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

void PrivateModelItemWidget::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 PrivateModelItemWidget::showSelectedIcon(bool flag)
{
    m_selectedIconLabel->setVisible(flag);
}

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

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

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


