#include "eaudio.h"
#include "eaudioattr.h"
#include "eobject.h"
#include "etext.h"
#include "ephoto.h"
#include "emovie.h"
#include "egif.h"
#include "edclock.h"
#include "eaclock.h"
#include "etimer.h"
#include "etemp.h"
#include "eweather.h"
#include "eaudioattr.h"

eAudio::eAudio(QRectF rectInit,InteractiveType type, QGraphicsItem *parent) :
    eObject(rectInit, type, parent)
{
    m_attr.strCur  = "";
    m_attr.index = -1;
    m_attr.self  = this;
    m_attr.eList.clear();
}

eAudio::eAudio(const QJsonObject &json, const QString pRoot, InteractiveType type, QGraphicsItem *parent) :
    eObject(json["geometry"].toObject(), type, parent)
{
    QJsonArray oElements = json["elements"].toArray();
    m_attr.strCur  = "";
    m_attr.index = json["index"].toInt();
    m_attr.self  = this;
    int n = oElements.count();
    for(int i=0; i<n; i++) {
        QJsonObject oElement = oElements.at(i).toObject();
        restoreElement(oElement, pRoot);
    }
    std::sort(m_attr.eList.begin(), m_attr.eList.end(), [](eObject *a, eObject *b){
        return a->zValue() < b->zValue();
    });
    if(m_attr.index >= 0) {
        m_attr.strCur = m_attr.eList.at(m_attr.index);
    }
}

//void eAudio::setElement(const QJsonObject &json, Data &attr)
//{
//}

void eAudio::restoreElement(const QJsonObject &json, const QString &pRoot)
{
    QString  strMp3Name = json["Mp3Name"].toString();
    QFileInfo fileInfo(strMp3Name);
    if(fileInfo.exists()) {
        m_attr.eList.append(strMp3Name);
    }
}

eAudio::~eAudio()
{

}

void eAudio::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(m_attr.eList.isEmpty()) {
        QFont font;
        font.setFamily("Arial");
        font.setPointSize(12);
        QTextOption opt;
        opt.setAlignment(Qt::AlignCenter);
        painter->save();
        painter->fillRect(rect(),QColor(0, 0, 0));
        painter->setFont(font);
        painter->setPen(QColor(100, 100, 100));
        painter->drawText(rect(), m_attr.strCur, opt);
       // painter->setPen(QColor(255, 0, 0, 100));
       // painter->drawRect(rect());
        painter->restore();
    }
    eObject::paint(painter, option, widget);
}

void eAudio::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{

}

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

QWidget* eAudio::wAttrElement()
{
    eAudioAttr *w = new eAudioAttr(m_attr);
    connect(w, SIGNAL(sRequestUpdate()), this, SLOT(updateGeometry()));
    connect( this, SIGNAL(geometryChanged(const QRectF &)),w, SLOT(OnGeometryChanged(const QRectF &)));
 //   emit geometryChanged(rect());
    return w;
}

QJsonObject eAudio::save(const QString &pRoot)
{
    QJsonObject oRoot;
    QJsonArray  oElements;
    oRoot["elementType"]   = "Audio";
    oRoot["elementTypeId"] = type();
    oRoot["geometry"]      = eObject::elementJson();
    oRoot["index"]         = m_attr.index;
    int izIndex=0;
    foreach(QString stre, m_attr.eList) {
        QJsonObject oSize;
        QRectF r = geometry();
        oSize["order"] =izIndex;// zValue();
        oSize["x"] = 0;
        oSize["y"] = 0;
        oSize["w"] = (int)r.width();
        oSize["h"] = (int)r.height();
        QJsonObject temp ;//= e->save(pRoot);
        temp["geometry"]=oSize;
        oElements.append(temp);
        izIndex++;
    }
    oRoot["elements"] = oElements;
    return oRoot;
}

QStringList eAudio::filesList() const
{
    QStringList list;
    foreach(QString stre, m_attr.eList) {
        list.append(stre);
    }
    return list;
}

QJsonObject eAudio::elementJson() const
{
    QJsonObject oRoot;
    QJsonArray  oElements;
    oRoot["elementType"]   = "Window";
    oRoot["elementTypeId"] = type();
    oRoot["geometry"]      = eObject::elementJson();
    oRoot["index"]         = m_attr.index;
    foreach(QString stre, m_attr.eList) {
        //oElements.append(e->elementJson());
    }
    oRoot["elements"] = oElements;
    return oRoot;
}

void eAudio::playElectment()
{

}

void eAudio::stopElectment()
{

}
