#ifndef ETEXT_H
#define ETEXT_H

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

#define MACRO_MAX_TEXT_PAGE 100
//这条语句害死人，加了这条语句文字就不能做到无反锯齿 MYFONT.setHintingPreference(QFont::PreferNoHinting);

class SuCaiFile{
public:
    QString strEffect;
    QString strFileName;
};
class txtBmpPage{
public:
    int index=0;
    QPixmap bitmap;
};
class txtBmpLine{
public:
    int index=0;
    int iStart=0;
    int iEnd=0;
    QPixmap bitmap;
};
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 turningSt
    {
        QString strEffect="no";
        int iEffectTime;
        int iEffectSpeed;
        int iPageStopTime;
        int playDuration=10;
    };
    struct rolloingSt
    {
        int rollingStyle;
        int rollingSpeed;
        bool headTailConnected;
        int  headTailSpacing=10;
        int  playDuration=10;
    };
    struct staticSt
    {
        int  playDuration=10;
    };
    struct Data {
        // Widget
        QString     text;
        QTextOption opt;
        int         lineSpacing;
        int         wordSpacing;
        QColor  cBackground;
        int iPageCount=1;
//        QList<SuCaiFile> listPng;
        // Play
        int  playStyle;
            //turning
        turningSt stTurning;
            //rolling
        rolloingSt stRolloing;
            //static
        staticSt stStatic;
    };

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

public:
    static void setElement(const QJsonObject &json, Data &attr);

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;
    virtual QJsonObject elementJson() const override;
//    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;

public:
    void setInput();
    void setTextContents(QTextDocument &doc);
    void setTextRichContents(QTextDocument &doc);

//    void setTextVAlign(int align, QTextDocument &doc);

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

public slots: // Widget
    void onTextRickChanged(const QString &);
    void onTextChanged(const QString &text)            { m_attr.text = text;             updateGeometry(); }
    void onBackgroundColorChanged(const QColor& color) { m_attr.cBackground = color;     updateGeometry();  RefreshBigPixmap();}
    void onTextLineSpacingChanged(int s)               { m_attr.lineSpacing = s;         updateGeometry(); RefreshBigPixmap();}
    void onTextLetterSpacingChanged(int s)             { m_attr.wordSpacing=s; updateGeometry();RefreshBigPixmap(); }
    void onTextHAlignChanged(int align);
    void onTextVAlignChanged(int align);
    void RefreshBigPixmap();
    void OnRectChanged();
    void OnPageIndexPreview(int iPageIndex);

public slots: // Play
    void onPlayStyleChanged(int n)       { m_attr.playStyle         = n; updateGeometry(); RefreshBigPixmap();}
    void onHeadTailConnected(bool f)     { m_attr.stRolloing.headTailConnected = f; }
    void onHeadTailSpacingChanged(int n) { m_attr.stRolloing.headTailSpacing   = n; }
    void onRollingSpeedChanged(int n)    { m_attr.stRolloing.rollingSpeed      = n; }
    void onTurningEffectSpeedChanged(int n)    { m_attr.stTurning.iEffectSpeed      = n;
                                                 int ibeishu=1;
                                                 if(m_list.count()>0)
                                                     ibeishu=m_list.count();
                                                 m_attr.stTurning.playDuration=n*ibeishu;
                                               }

    void onRollingStyleChanged(int n)    { m_attr.stRolloing.rollingStyle      = n; updateGeometry(); RefreshBigPixmap();}
    void onTurningEffectChanged(QString strEffect);


   // void onPlayDurationChanged(int n)    { m_attr.playDuration      = n; }
    void onTurningPlayDurationChanged(QTime n)    {        QTime aa(0,0,0);  m_attr.stTurning.playDuration      = aa.secsTo(n); }
    void onEveryPageDurationChanged(QTime n)    {        QTime aa(0,0,0);  m_attr.stTurning.iEffectTime      = aa.secsTo(n); }


    void onRollingPlayDurationChanged(QTime n)    {        QTime aa(0,0,0);  m_attr.stRolloing.playDuration      = aa.secsTo(n); }
    void onStaticPlayDurationChanged(QTime n)    {        QTime aa(0,0,0);  m_attr.stStatic.playDuration      = aa.secsTo(n); }

private:
    int ClearPixmapTopAndBottomBlack();
    void MakeMuliPngToDir(int iPageIndex,QString strPngPathAndName) ;
    void MakeBigMoveUpOrDownOnePngToDir() ;
    void MakeBigMoveLeftOrRightOnePngToDir() ;
    void MakeBigMoveUpOrDownOnePngToDir(QString strPngPathAndName) ;
    void MakeBigMoveLeftOrRightOnePngToDir(QString strPngPathAndName) ;
    void CreateBigOnePixBmp();
    int GetPagesPosList();
    txtBmpPage GetFirstPagePixmap();
    int GetLinePng(QPixmap bigOnePixmap) ;
    void ProcessLastStartEndLianJie(QPixmap bmp,QString strPngFilePathName,int iLastRightNoWordWidth);
    int GetWordSpaceRealInBmp(QPixmap bmp);

private: // Interior
    QPointF    m_pText;
    QRectF     m_rClip;
    eTextAttr  *m_wAttr;
    eTextInput *m_wTextInput;
    Data       m_attr;
    int        m_lineH;
    QPainter *m_pPainter=nullptr;
    QPixmap *m_pRenderPixmap=nullptr;
    QPixmap m_bigOnePixBmp;

    QList<txtBmpPage> m_list;
    qreal m_oldWidth=0;
    qreal m_oldHeight=0;
    QColor m_ImageBkColor;
    QString m_pRootPath;
    QList<txtBmpLine> m_linelist;

};

#endif // ETEXT_H
