#include "ItemChip.h"

#include <QFont>
#include <QHBoxLayout>
#include <QLabel>
#include <QPalette>
#include <QVBoxLayout>

#include "ItemTooltip.h"
#include "TooltipTrigger.h"

namespace
{

// Point-size rise of the count and drop of the sub-line, relative to the card's text.
const int kCountSizeRisePt   = 2;
const int kSubLineSizeDropPt = 1;

}  // namespace


ItemChip::ItemChip(const SelectionContext& context, const std::string& itemId,
                   const QPixmap& icon, QWidget* parent)
    : QWidget(parent)
{
    // Its own boxed chrome, drawn with palette colors like the rest of the panel's
    // furniture rather than from visuals.toml, which is for world rendering.
    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet(QStringLiteral(
        "ItemChip { border: 1px solid palette(mid); border-radius: 3px; }"));

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setContentsMargins(4, 3, 4, 3);
    layout->setSpacing(6);

    m_iconLabel = new QLabel(this);
    m_iconLabel->setPixmap(icon);
    m_iconLabel->setVisible(!icon.isNull());

    QWidget* text = new QWidget(this);
    QVBoxLayout* textLayout = new QVBoxLayout(text);
    textLayout->setContentsMargins(0, 0, 0, 0);
    textLayout->setSpacing(0);

    m_countLabel = new QLabel(text);
    QFont countFont = m_countLabel->font();
    countFont.setPointSize(countFont.pointSize() + kCountSizeRisePt);
    m_countLabel->setFont(countFont);

    m_subLineLabel = new QLabel(text);
    QFont subLineFont = m_subLineLabel->font();
    subLineFont.setPointSize(qMax(1, subLineFont.pointSize() - kSubLineSizeDropPt));
    m_subLineLabel->setFont(subLineFont);
    QPalette subLinePalette = m_subLineLabel->palette();
    subLinePalette.setColor(QPalette::WindowText,
                            palette().color(QPalette::Disabled, QPalette::WindowText));
    m_subLineLabel->setPalette(subLinePalette);
    m_subLineLabel->hide();

    textLayout->addWidget(m_countLabel);
    textLayout->addWidget(m_subLineLabel);

    layout->addWidget(m_iconLabel);
    layout->addWidget(text);

    // Every child is transparent to the mouse, so the chip is what the pointer is on
    // wherever it rests on the face -- a child under it would take the chip's own enter,
    // leave and press events (REQ-UI-TOOLTIP-TRIGGER).
    m_iconLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    m_countLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    m_subLineLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    text->setAttribute(Qt::WA_TransparentForMouseEvents, true);

    // A chip displays a quantity and does nothing when clicked, so a click brings its
    // tooltip up at once rather than waiting the hover out (REQ-UI-TOOLTIP-TRIGGER).
    ItemTooltip::attachTo(*this, context.getItemTooltipContext(), itemId,
                          TooltipTrigger::Trigger::HoverAndClick);
}

void ItemChip::setCount(const QString& count)
{
    m_countLabel->setText(count);
}

void ItemChip::setSubLine(const QString& subLine)
{
    m_subLineLabel->setText(subLine);
    m_subLineLabel->setVisible(!subLine.isEmpty());
}
