#pragma once

#include <QFrame>
#include <QPoint>
#include <QString>

class QHideEvent;
class QVBoxLayout;

// The popup every tooltip in the UI is drawn in (REQ-UI-TOOLTIP-TRIGGER,
// REQ-UI-TOOLTIP-DISMISS). Shown and hidden by a TooltipTrigger attached to the element
// it describes; it carries no trigger logic of its own.
//
// A window of its own rather than Qt's own tooltip, because ours must not time out, must
// stay up while the pointer is on it, and -- for the item production tooltip
// (REQ-UI-ITEM-TOOLTIP) -- must hold drawn content rather than text. A Qt::ToolTip window
// so it floats above the game window instead of being clipped by it.
class Tooltip : public QFrame
{
    Q_OBJECT

public:
    // The tooltip currently on screen, if any: only ever one, since the pointer can only
    // be on one element.
    static Tooltip* getVisibleTooltip();
    static void     hideVisibleTooltip();

    explicit Tooltip(QWidget* parent = nullptr);
    ~Tooltip() override;

    // Plain text content, wrapped at a readable measure. Newlines are kept.
    void setText(const QString& text);
    // Refreshes the content and shows the tooltip beside the given screen position --
    // one pixel to its right, or to its left where there is no room -- kept inside the
    // screen (REQ-UI-TOOLTIP-DISMISS).
    void showAt(const QPoint& globalPos);
    bool getContainsGlobalPos(const QPoint& globalPos) const;

protected:
    // Called before every show, for content that goes stale between them. The base
    // tooltip's text does not, so this does nothing here.
    virtual void refreshContent();
    QVBoxLayout* getContentLayout();
    void         clearContent();

    void hideEvent(QHideEvent* event) override;

private:
    static Tooltip* s_visibleTooltip;

    QVBoxLayout* m_layout;
};
