#ifndef EBASE_H
#define EBASE_H

#include <QGraphicsObject>
#include <QVBoxLayout>
#include <QPen>
#include <float.h>
#define m_handleLen 10

class EBase : public QGraphicsObject {
    Q_OBJECT
public:
    enum ElementType {
        Text = QGraphicsItem::UserType + 1,
        Photo, Video, Gif, Audio,
        DClock, AClock, Timer, Environ, Window, Web
    };
    Q_ENUM(ElementType)

    explicit EBase(EBase *multiWin = nullptr);

    void setBaseAttr(const QJsonObject &);
    void addBaseAttr(QJsonObject &) const;

    QRectF boundingRect() const override;
    void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override;

    virtual QJsonObject attrJson() const = 0;
    virtual void loadFiles() {}
    virtual void freeFiles() {}
    virtual bool save(const QString &) {return true;}
    virtual QWidget* attrWgt() = 0;

    void addBaseAttrWgt(QBoxLayout *vBox);
    inline void setSize(qreal width, qreal height) {
        prepareGeometryChange();
        mWidth = width;
        mHeight = height;
        emit sizeChanged();
    }
    void fitProgSize();
    QRectF innerRect() const;
    QRectF rect() const { return innerRect(); }

    int mType{-1};
    EBase *mMultiWin{nullptr};
    qreal mWidth{0.0}, mHeight{0.0};
    int mStartTime{0};
signals:
    void sizeChanged();

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *) override;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *) override;
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *) override;
    QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;

    void timerEvent(QTimerEvent *) override;

    void setFrmSec(const QPointF &);
    void setFrmSecIfNeed(Qt::WindowFrameSection frmSec, Qt::CursorShape cursor);
    void clearSnap();
    QPen mSidePen;

    QRectF m_rL, m_rR, m_rT, m_rB, m_rLT, m_rRT, m_rRB, m_rLB;
    QPointF mPressRel{FLT_MAX, FLT_MAX};
    Qt::WindowFrameSection mFrmSec{Qt::NoSection};
    char mLRSnap{0}, mTBSnap{0};
    QList<EBase *> mOtherEles;

    int bdImgIdx{-1};
    QString bdEff{"rotate"};
    int bdSpeed{2};
    int bdOff{0};
    int bdTimerId{0};
};

#endif // EBASE_H
