#include "validation-bubble.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QEvent>
#include <QApplication>
#include <QStyle>
#include <QScreen>

ValidationBubble::ValidationBubble(QWidget *parent)
    : QWidget(parent)
{
    setAttribute(Qt::WA_TransparentForMouseEvents, false);
    setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_ShowWithoutActivating);
    setAttribute(Qt::WA_StyledBackground, true);
    setObjectName("ValidationBubble");

    m_label = new QLabel(this);
    m_label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    QFont f = m_label->font(); f.setPixelSize(12); m_label->setFont(f);
    m_label->setText(QString());

    auto *layout = new QVBoxLayout(this);
    layout->setContentsMargins(8,4,8,4);
    layout->addWidget(m_label);
}

void ValidationBubble::showMessage(QWidget *target, const QString &text) {
    if (!target) return;
    // If switching targets, remove invalid highlight from previous target
    if (m_target && m_target != target) {
        m_target->setProperty("invalid", false);
        m_target->style()->unpolish(m_target);
        m_target->style()->polish(m_target);
    }
    m_target = target;
    m_label->setText(text);
    m_target->installEventFilter(this); // install (Qt tolerates duplicate silently)
    // Mark target invalid for stylesheet highlight
    m_target->setProperty("invalid", true);
    m_target->style()->unpolish(m_target);
    m_target->style()->polish(m_target);
    reposition();
    show();
}

void ValidationBubble::clear() {
    hide();
    m_label->clear();
    if (m_target) m_target->removeEventFilter(this);
    if (m_target) {
        m_target->setProperty("invalid", false);
        m_target->style()->unpolish(m_target);
        m_target->style()->polish(m_target);
    }
    m_target.clear();
}

bool ValidationBubble::eventFilter(QObject *obj, QEvent *event) {
    if (obj == m_target) {
        switch (event->type()) {
        case QEvent::Move:
        case QEvent::Resize:
        case QEvent::Show:
        case QEvent::Hide:
            reposition();
            break;
        case QEvent::Destroy:
            clear();
            break;
        default: break;
        }
    }
    return QWidget::eventFilter(obj, event);
}

void ValidationBubble::reposition() {
    if (!m_target) return;
    // Prefer positioning to the right side centered vertically to avoid covering subsequent fields.
    adjustSize();
    QRect targetRect = QRect(m_target->mapToGlobal(QPoint(0,0)), m_target->size());
    int bubbleW = width();
    int bubbleH = height();
    int gap = 8;
    int x = targetRect.right() + gap;
    int y = targetRect.top() + (targetRect.height() - bubbleH)/2;
    // If off screen to the right, try left side
    QRect screenRect = QApplication::primaryScreen()->geometry();
    if (x + bubbleW > screenRect.right() - 20) {
        x = targetRect.left() - bubbleW - gap;
    }
    // If still off screen left, fallback below
    if (x < screenRect.left()) {
        x = targetRect.left();
        y = targetRect.bottom() + 4;
    }
    move(x, y);
}

// Painting removed; QSS now handles background, border, radius.
