#include "kamodelitem.h"

#include <QMenu>
#include <QLabel>
#include <QVariant>
#include <QToolButton>
#include <QHBoxLayout>

class KAModelItem::Private
{
public:
    QLabel *labelName = nullptr;
    QToolButton *buttonMore = nullptr;

    QMenu *menu = nullptr;
    RemoteModelId modelType;
};

KAModelItem::KAModelItem(RemoteModelId type, QWidget *parent)
    : QWidget{parent},
      d_ptr(make_shared<Private>())
{
    d_ptr->modelType = type;
    initLayout();
}

KAModelItem::~KAModelItem()
{

}

void KAModelItem::initLayout()
{
    d_ptr->labelName = new QLabel;
    d_ptr->labelName->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    QString model_name = "";
    switch (d_ptr->modelType)
    {
    case NLP:
        model_name = tr("NLP Model");
        break;
    case VISION:
        model_name = tr("VISION Model");
        break;
    default:
        break;
    }
    d_ptr->labelName->setText(model_name);

    d_ptr->buttonMore = new QToolButton;
    d_ptr->buttonMore->setFixedSize(38, 38);
    d_ptr->buttonMore->setProperty("useButtonPalette", true);
    d_ptr->buttonMore->setAutoRaise(true);
    d_ptr->buttonMore->setPopupMode(QToolButton::InstantPopup);
    d_ptr->buttonMore->setIcon(QIcon::fromTheme("view-more-horizontal-symbolic"));
    d_ptr->buttonMore->setFocusPolicy(Qt::NoFocus);

    QHBoxLayout *layout_h = new QHBoxLayout(this);
    layout_h->setContentsMargins(20, 10, 20, 10);
    layout_h->addWidget(d_ptr->labelName);
    layout_h->addWidget(d_ptr->buttonMore);

    initMenu();
}

void KAModelItem::initMenu()
{
    d_ptr->menu = new QMenu();
    d_ptr->buttonMore->setMenu(d_ptr->menu);

    QAction *actionEdit = new QAction(QIcon::fromTheme("document-edit-symbolic"), tr("Edit"));
    connect(actionEdit, &QAction::triggered, [this]{emit sigEdit(d_ptr->modelType);});

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

    d_ptr->menu->addAction(actionEdit);
    d_ptr->menu->addAction(actionDelete);
}
