#ifndef ETEXT_H
#define ETEXT_H

#include <QPainter>
#include <QFontMetrics>
#include <QTextDocument>
#include <QGraphicsSceneMouseEvent>
#include "eobject.h"

class eTextAttr;
class eTextInput;
class eText : public eObject
{
    Q_OBJECT
public:
    enum PlayStyle {
        Turning = 0,
        Rolling,
        Static
    };

    enum RollingStyle {
        Left2Right = 0,
        Right2Left,
        Top2Bottom,
        Bottom2Top
    };

    struct Data {
        // Widget
        QString     text;
        QFont       font;
        QTextOption opt;
        QColor      cText;
        QColor      cTextShadow;
        QColor      cBackground;
        int         lineSpacing;
        // Play
        int  playStyle;
        bool headTailConnected;
        int  headTailSpacing;
        int  rollingSpeed;
        int  rollingStyle;
        int  playDuration;
    };

public:
    explicit eText(InteractiveType type = Dynamic, QGraphicsItem *parent = nullptr);
    explicit eText(const QJsonObject &json, InteractiveType type = Dynamic, QGraphicsItem *parent = nullptr);

public:
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
    virtual int type() const override { return eObject::Text; }
    virtual QWidget* wAttr() override;
    virtual QWidget* wAttrElement() override;
    virtual QJsonObject save(const QString &pRoot) override { Q_UNUSED(pRoot); return elementJson(); }
    virtual QJsonObject elementJson() const override;

public:
    void setInput();
    void setTextContents(QTextDocument &doc);
    void setTextVAlign(int align, QTextDocument &doc);

signals:

public slots:
    void onWAttrClosed() { m_wAttr = nullptr; }
    void onInputClosed() { m_wTextInput = nullptr; }

public slots: // Widget
    void onTextChanged(const QString &text)            { m_attr.text = text;             updateGeometry(); }
    void onFontFamilyChanged(const QString&ff)         { m_attr.font.setFamily(ff);      updateGeometry(); }
    void onFontSizeChanged(int size)                   { m_attr.font.setPointSize(size); updateGeometry(); }
    void onFontBoldChanged(bool en)                    { m_attr.font.setBold(en);        updateGeometry(); }
    void onFontItalicsChanged(bool en)                 { m_attr.font.setItalic(en);      updateGeometry(); }
    void onFontUnderlineChanged(bool en)               { m_attr.font.setUnderline(en);   updateGeometry(); }
    void onTextHAlignChanged(int align);
    void onTextVAlignChanged(int align);
    void onTextColorChanged(const QColor& color)       { m_attr.cText       = color;     updateGeometry(); }
    void onTextShadowColorChanged(const QColor& color) { m_attr.cTextShadow = color;     updateGeometry(); }
    void onBackgroundColorChanged(const QColor& color) { m_attr.cBackground = color;     updateGeometry(); }
    void onTextLetterSpacingChanged(int s)             { m_attr.font.setLetterSpacing(QFont::AbsoluteSpacing, s);
                                                         updateGeometry(); }
    void onTextLineSpacingChanged(int s)               { m_attr.lineSpacing = s;         updateGeometry(); }

public slots: // Play
    void onPlayStyleChanged(int n)       { m_attr.playStyle         = n; }
    void onHeadTailConnected(bool f)     { m_attr.headTailConnected = f; }
    void onHeadTailSpacingChanged(int n) { m_attr.headTailSpacing   = n; }
    void onRollingSpeedChanged(int n)    { m_attr.rollingSpeed      = n; }
    void onRollingStyleChanged(int n)    { m_attr.rollingStyle      = n; }
    void onPlayDurationChanged(int n)    { m_attr.playDuration      = n; }

private: // Interior
    QPointF    m_pText;
    QRectF     m_rText;
    QRectF     m_rClip;
    eTextAttr  *m_wAttr;
    eTextInput *m_wTextInput;
    Data       m_attr;
    int        m_lineH;
};

#endif // ETEXT_H
