#include "egif.h"
#include "egifattr.h"

eGif::eGif(QRectF rectInit,const QString &file, InteractiveType type, QGraphicsItem *parent) :
    eObject(rectInit,type, parent)
{
    m_iType=eObject::Gif;
    QFileInfo fi(file);
    m_attr.name = fi.fileName();
    m_attr.path = fi.absolutePath();
    setPlayArg(10, 1);
    qreal x=rectInit.left();
    qreal y=rectInit.top();
    qreal w=rectInit.width();
    qreal h=rectInit.height();

    setGeometry(QRectF(x, y, w, h));

    init();
}
eGif::eGif(QRectF rectInit,QSizeF DrawBoardSize,const QString &file, InteractiveType type, QGraphicsItem *parent) :
    eObject(rectInit,type, parent)
{
    m_iType=eObject::Gif;
    qreal x=rectInit.left();
    qreal y=rectInit.top();
    QFileInfo fi(file);
    m_attr.name = fi.fileName();
    m_attr.path = fi.absolutePath();
    setPlayArg(10, 1);
    QImage m_photo(file);
    qreal ihbiw=static_cast<qreal>(m_photo.height())/ static_cast<qreal>(m_photo.width());
    qreal iwbih=static_cast<qreal>(m_photo.width()) / static_cast<qreal>(m_photo.height());
    qreal w = rectInit.width();
    qreal h = floor(w*ihbiw );
    if(x+w>DrawBoardSize.width())
    {
        x=0;
        if(w>DrawBoardSize.width())
            w=DrawBoardSize.width();
        h=w*ihbiw;
    }
    if(y+h>DrawBoardSize.height())
    {
        y=0;
        if(h>DrawBoardSize.height())
            h=DrawBoardSize.height();

        w=h*iwbih;
    }
    setGeometry(QRectF(x, y, w, h));

    init();
}
eGif::eGif(const QJsonObject &json, const QString &pRoot, InteractiveType type, QGraphicsItem *parent) :
    eObject(json["geometry"].toObject(), type, parent)
{
    m_iType=eObject::Gif;
    QJsonDocument jRoot(json);
    QString file0 = pRoot                              + MACRO_FENGEFU + jRoot["widget"]["file"].toString();
    QString file1 = jRoot["widget"]["path"].toString() + MACRO_FENGEFU + jRoot["widget"]["file"].toString();
    QFile f0(file0);
    QFile f1(file1);
    m_attr.name = jRoot["widget"]["file"].toString();
    m_attr.path = jRoot["widget"]["path"].toString();
    QString file;
    if(f0.exists()) {
        file = file0;
    } else if(f1.exists()) {
        file = file1;
    } else {
        setGeometry(RECTF_INVALID);
        return;
    }
    QFileInfo fi(file);
    m_attr.name = fi.fileName();
    m_attr.path = fi.absolutePath();
    setPlayArg(jRoot["play"]["playDuration"].toInt(),
               jRoot["play"]["playTimes"].toInt());
    init();
}

void eGif::init()
{
    m_movie = new QMovie(this);
    m_vWidget = new QGraphicsProxyWidget(this);
    m_vWidget->setFlag(QGraphicsItem::ItemStacksBehindParent);
    auto label = new QLabel();
    label->setMovie(m_movie);

    m_vWidget->setWidget(label);
    m_vWidget->setMinimumSize(0, 0);
    connect(this, SIGNAL(rectChanged(const QRectF &)), this, SLOT(onAdjustMovieRect()));
    onAdjustMovieRect();
    playElectment();
}

void eGif::setElement(const QJsonObject &json, Data &attr)
{
    attr.name         = json["widget"]["file"].toString();
    attr.path         = json["widget"]["path"].toString();
    attr.playDuration = json["play"]["playDuration"].toInt();
    attr.playTimes    = json["play"]["playTimes"].toInt();
}

QWidget* eGif::wAttr()
{
    QWidget *wObj = eObject::wAttr();
    QWidget *w = wAttrElement();
    static_cast<QBoxLayout*>(w->layout())->insertWidget(0, wObj);
    return w;
}

QWidget* eGif::wAttrElement()
{
    eGifAttr *w = new eGifAttr(m_attr);
    connect(w, SIGNAL(sResChanged(const QString &)),     this, SLOT(onResChanged(const QString &)));
    connect(w, SIGNAL(sAttrChanged(const eGif::Data &)), this, SLOT(onAttrChanged(const eGif::Data &)));
    return w;
}

QJsonObject eGif::save(const QString &pRoot)
{
    QString file0 = m_attr.path + PAGEDEL_SUFFIX + MACRO_FENGEFU + m_attr.name;
    QString file1 = m_attr.path + MACRO_FENGEFU + m_attr.name;
    QFile f0(file0);
    QFile f1(file1);

    QString old_file;
    QString new_file = pRoot + MACRO_FENGEFU + m_attr.name;
    if(f0.exists()) {
        old_file = file0;
    } else if(f1.exists()) {
        old_file = file1;
    } else {
        return QJsonObject();
    }

    QFile old_f(old_file);
    QFile new_f(new_file);
    if(!new_f.exists())
        old_f.copy(new_file);
    m_attr.path = pRoot;
    return elementJson();
}

QStringList eGif::filesList() const
{
    QStringList list;
    list.append(m_attr.path + MACRO_FENGEFU + m_attr.name);
    return list;
}

QJsonObject eGif::elementJson() const
{
    QJsonObject oRoot;
    QJsonObject oWidget;
    QJsonObject oPlay;
    // Interior
    oRoot["elementType"]   = "Gif";
    oRoot["elementTypeId"] = type();
    oRoot["geometry"]      = eObject::elementJson();
    // Widget
    oWidget["file"]        = m_attr.name;
    QString strtempPath=m_attr.path;
    strtempPath.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
    oWidget["path"]        = strtempPath;
    oRoot["widget"]        = oWidget;
    // Play
    oPlay["playDuration"]  = m_attr.playDuration;
    oPlay["playTimes"]     = m_attr.playTimes;
    oRoot["play"]          = oPlay;
    return oRoot;
}

void eGif::playElectment()
{
    QString file = m_attr.path + MACRO_FENGEFU + m_attr.name;
    file.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
    QFile f(file);
    if(f.exists())
    {
        m_movie->setFileName(file);
        m_movie->start();

    }
}

void eGif::stopElectment()
{
    QString file = m_attr.path + MACRO_FENGEFU + m_attr.name;
    file.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
    QFile f(file);
    if(f.exists())
    {
        m_movie->stop();
        m_movie->setFileName(QString::Null());

    }
}

void eGif::setPlayArg(int pd, int pt)
{
    m_attr.playDuration = pd;
    m_attr.playTimes    = pt;
}

void eGif::onResChanged(const QString &file)
{
    QFileInfo fi(file);
    m_attr.name = fi.fileName();
    m_attr.path = fi.absolutePath();
    stopElectment();
    playElectment();
}

void eGif::onAttrChanged(const eGif::Data &data)
{
    setPlayArg(data.playDuration, data.playTimes);
}

void eGif::onAdjustMovieRect()
{
    m_movie->setScaledSize(QSize(static_cast<int>(rect().width()), static_cast<int>(rect().height())));
    m_vWidget->setGeometry(rect());
}
void eGif:: deleteContent()
{
    QFile file(m_attr.path+"/"+m_attr.name);
    if(file.exists())
    {
        stopElectment();
        file.remove();
    }
}
