#include "ewindow.h"
#include "ewindowattr.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"

eWindow::eWindow(QRectF rectInit,InteractiveType type, QGraphicsItem *parent) :
    eObject(rectInit, type, parent)
{
    m_iType=eObject::Window;
    m_attr.eCur  = nullptr;
    m_attr.index = -1;
    m_attr.self  = this;
    m_attr.eList.clear();
}

eWindow::eWindow(const QJsonObject &json, const QString pRoot, InteractiveType type, QGraphicsItem *parent) :
    eObject(json["geometry"].toObject(), type, parent)
{
    m_iType=eObject::Window;
    QJsonArray oElements = json["elements"].toArray();
    m_attr.eCur  = nullptr;
    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) {
        if(m_attr.index>=m_attr.eList.count())
            m_attr.index=m_attr.eList.count()-1;
        if(m_attr.eList.count()>0)
        {
            m_attr.eCur = m_attr.eList.at(m_attr.index);
            m_attr.eCur->setParentItem(this);

        }
    }
}

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

void eWindow::restoreElement(const QJsonObject &json, const QString &pRoot)
{
    int type = json["elementTypeId"].toInt();
    eObject *element = nullptr;
    switch (type) {
    case eObject::Text :
        element = new eText(json, Static);
        break;
    case eObject::Photo :
        element = new ePhoto(json, pRoot, Static);
        break;
    case eObject::Movie :
        element = new eMovie(json, pRoot, Static);
        break;
    case eObject::Gif :
        element = new eGif(json, pRoot, Static);
        break;
    case eObject::DClock :
        element = new eDClock(json, Static);
        break;
    case eObject::AClock :
        element = new eAClock(json, Static);
        break;
    case eObject::Office :
        break;
    case eObject::Temp :
        element = new eTemp(json, Static);
        break;
    case eObject::Weather :
        element = new eWeather(json, Static);
        break;
    case eObject::Rss :
        break;
    case eObject::Timer :
        element = new eTimer(json, Static);
        break;
    case eObject::ColorText :
        break;
    case eObject::Window :
        element = new eWindow(json, pRoot, Static);
        break;
    default:
        element = nullptr;
        break;
    }
    if(element != nullptr) {
        element->setFlag(QGraphicsItem::ItemStacksBehindParent);
        m_attr.eList.append(element);
    }
}

eWindow::~eWindow()
{
    foreach(eObject *e, m_attr.eList) {
        delete e;
    }
}

void eWindow::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(), tr("Please add media on the right"), opt);
       // painter->setPen(QColor(255, 0, 0, 100));
       // painter->drawRect(rect());
        painter->restore();
    }
    eObject::paint(painter, option, widget);
}

void eWindow::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_attr.index >= 0) {
        m_attr.eCur->mouseDoubleClickEvent(event);
    }
}

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

QWidget* eWindow::wAttrElement()
{
    eWindowAttr *w = new eWindowAttr(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 eWindow::save(const QString &pRoot)
{
    QJsonObject oRoot;
    QJsonArray  oElements;
    oRoot["elementType"]   = "Window";
    oRoot["elementTypeId"] = type();
    oRoot["geometry"]      = eObject::elementJson();
    oRoot["index"]         = m_attr.index;
    int izIndex=0;
    foreach(eObject *e, 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 eWindow::filesList() const
{
    QStringList list;
    foreach(eObject *e, m_attr.eList) {
        list.append(e->filesList());
    }
    return list;
}

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

void eWindow::playElectment()
{
    foreach(eObject *e, m_attr.eList) {
        e->playElectment();
    }
}

void eWindow::stopElectment()
{
    foreach(eObject *e, m_attr.eList) {
        e->stopElectment();
    }
}
