#ifndef EOBJECT_H
#define EOBJECT_H
#include "loappconfig.h"
#include "loapptools.h"
#include "globaldefine.h"
#include <math.h>
#include <QGraphicsObject>
#include <QGraphicsScene>

#include <QDebug>
#include <QPainter>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QWidget>
#include <QSpinBox>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QVBoxLayout>

#define MACRO_DEFAULT_PLAYDURATION 10
#define INVALID_RECT QRect(-1, -5, -6, -8)

class eObjectAttr;
class eObject : public QGraphicsObject {
    Q_OBJECT
public:
    typedef enum {
        NONE = 0,
        LT,
        T,
        RT,
        L,
        R,
        LB,
        B,
        RB
    } HANDLE_DIR;

    enum InteractiveType {
        Dynamic = 0,
        Static,
        Custom
    };

    enum ElementType {
        Text = QGraphicsItem::UserType + 1,
        Photo,
        Movie,
        Gif,
        DClock,
        AClock,
        Audio,
        Office,
        Temp,
        Weather,
        Rss,
        Timer,
        ColorText,
        Window
    };

    explicit eObject(InteractiveType type = Dynamic, QGraphicsItem *parent = nullptr);
    explicit eObject(QRectF rect, InteractiveType type = Dynamic, QGraphicsItem *parent = nullptr);
    explicit eObject(const QJsonObject &json, InteractiveType type = Dynamic, QGraphicsItem *parent = nullptr);
    int m_iType=-1;

    virtual QRectF boundingRect() const override;
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) override{};
    virtual void setInteractiveType(InteractiveType type = Dynamic);
    virtual QJsonObject save(const QString &pRoot) = 0;
    virtual QWidget* wAttrElement() = 0;

    virtual QWidget* wAttr();
    virtual QStringList filesList() const { return QStringList(); }
    virtual QJsonObject elementJson() const;

    void setRLimit(const QRectF &r);
    QRectF rLimit() const { return m_rLimit; }
    InteractiveType interactiveType() const { return m_interactiveType; }
    QRectF rect()                     const { return QRectF(0, 0, m_w, m_h); }
    QRectF geometry()                 const { return QRectF(x(), y(), m_w, m_h); }


    QString getFileMd5(QString filePath);

signals: // Qt::BlockingQueuedConnection
    void requestUpdate(const QRectF &);
    void geometryChanged(const QRectF &);
    void rectChanged(const QRectF &);
    void sigCiTie(bool,int);

    void sPlayBQ();
    void sStopBQ();

public slots:
    void setGeometry (qreal x, qreal y, qreal w, qreal h) { setGeometry(QRectF(x, y, w, h)); }
    void setGeometry (const QRectF &r);
    void updateGeometry();
    void updateGeometry(const QRectF &gLast) { updateGeometry(geometry(), gLast); }
    void updateGeometry(const QRectF &gCur, const QRectF &gLast);
    void setBrightBianLeft(bool);
    void setBrightBianTop(bool);
    void setBrightBianRight(bool);
    void setBrightBianbottom(bool);

    virtual void playElectment() {}
    virtual void stopElectment() {}
    void onAttrChanged(const QRectF &);


protected:
    int handleLen() const { return m_handleLen; }
    QPointF pointToParent(const QPointF &p);
    HANDLE_DIR handleDir(const QPointF &p);
    void adjustHandle();

    InteractiveType m_interactiveType;
    bool       m_movable;
    int        m_handleLen;
    QPen       m_handlePen;
    QPen       m_borderPen;

    qreal      m_w;
    qreal      m_h;
    QRectF     m_rLT;
    QRectF     m_rT;

    QRectF     m_rRT;
    QRectF     m_rL;
    QRectF     m_rR;
    QRectF     m_rLB;
    QRectF     m_rB;
    QRectF     m_rRB;
    QRectF     m_rOrg;
    QPointF    m_pOrg;
    HANDLE_DIR m_hDir;
    QRectF     m_rLimit;
    bool  bMousePress=false;
    bool bLeftCitie=false;
    bool bRightCitie=false;
    bool bTopCitie=false;
    bool bBottomCitie=false;
    int m_keyPressId=0;

    eObjectAttr *m_wAttr;
    void init();
};

#endif // EOBJECT_H
