#include "ewindowattr.h"
#include "ui_ewindowattr.h"
#include "etext.h"
#include "ephoto.h"
#include "emovie.h"
#include "egif.h"
#include "edclock.h"
#include "eaclock.h"
#include "etimer.h"
#include "ewindow.h"
#include "etemp.h"
#include "eweather.h"

eWindowAttr::eWindowAttr(eWindow::Data &data, QWidget *parent) :
    eAttr(parent),
    ui(new Ui::eWindowAttr),
    m_attr(data)
{
    ui->setupUi(this);

    ui->wList->setIconSize(QSize(20, 20));
    ui->wList->setProperty("ssType", "elementWindow");
    ui->wList->setProperty("ssName", "elementList");

    std::sort(data.eList.begin(), data.eList.end(), [](eObject *a, eObject *b){
        return a->zValue() < b->zValue();
    });
    foreach(eObject *e, data.eList) {
        restoreItem(e);
    }

    auto menu = new QMenu();
    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Text.png"),      tr("Text"))     ->setData(eObject::Text);
    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Photo.png"),     tr("Photo"))    ->setData(eObject::Photo);
    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Movie.png"),     tr("Movie"))    ->setData(eObject::Movie);
    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Gif.png"),       tr("Gif"))      ->setData(eObject::Gif);
    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/DClock.png"),    tr("DClock"))   ->setData(eObject::DClock);
    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/AClock.png"),    tr("AClock"))   ->setData(eObject::AClock);
//    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Office.png"),    tr("Office"))   ->setData(eObject::Office);
//    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Temp.png"),      tr("Temp"))     ->setData(eObject::Temp);
//    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Weather.png"),   tr("Weather"))  ->setData(eObject::Weather);
//    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Rss.png"),       tr("Rss"))      ->setData(eObject::Rss);
//    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Timer.png"),     tr("Timer"))    ->setData(eObject::Timer);
//    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/ColorText.png"), tr("ColorText"))->setData(eObject::ColorText);
//    menu->addAction(QIcon(":/res/ProgramManager/EditProgram/Window.png"),    tr("Window"))   ->setData(eObject::Window);
    ui->wAdd->setMenu(menu);

    connect(menu,        SIGNAL(triggered(QAction*)), this, SLOT(onCreateElement(QAction*)));
    connect(ui->wDel,    SIGNAL(clicked()),           this, SLOT(onDeleteElement()));
    connect(ui->wClean,  SIGNAL(clicked()),           this, SLOT(onClearElements()));
    connect(ui->wGoUp,   SIGNAL(clicked()),           this, SLOT(onGoUpElement()));
    connect(ui->wGoDown, SIGNAL(clicked()),           this, SLOT(onGoDownElement()));
    connect(ui->wList,   SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
            this,        SLOT(onCurrentElementChanged(QListWidgetItem*, QListWidgetItem*)));

    if(m_attr.index >= 0) {
        ui->wList->setCurrentRow(static_cast<int>(m_attr.index));
    }
}

eWindowAttr::~eWindowAttr()
{
    delete ui;
}

void eWindowAttr::restoreItem(eObject *e)
{
    QIcon   icon;
    QString name = "";
    int type = e->type();
    switch (type) {
    case eObject::Text :
        icon = QIcon(":/res/ProgramManager/EditProgram/Text.png");
        name = tr("Text");
        break;
    case eObject::Photo :
    {
        icon = QIcon(":/res/ProgramManager/EditProgram/Photo.png");
        QString strtempName=dynamic_cast<ePhoto *>(e)->getData().name;
        name = tr("Photo")+"  "+strtempName;
    }
        break;
    case eObject::Movie :
    {
        icon = QIcon(":/res/ProgramManager/EditProgram/Movie.png");
        QString strtempName=dynamic_cast<eMovie *>(e)->getData().strRawName;
        name = tr("Movie")+"  "+strtempName;
    }
        break;
    case eObject::Gif :
    {
        icon = QIcon(":/res/ProgramManager/EditProgram/Gif.png");
        QString strtempName=dynamic_cast<eGif *>(e)->getData().name;
        name = tr("Gif")+"  "+strtempName;
    }
        break;
    case eObject::DClock :
        icon = QIcon(":/res/ProgramManager/EditProgram/DClock.png");
        name = tr("DClock");
        break;
    case eObject::AClock :
        icon = QIcon(":/res/ProgramManager/EditProgram/AClock.png");
        name = tr("AClock");
        break;
    case eObject::Office :
        icon = QIcon(":/res/ProgramManager/EditProgram/Office.png");
        name = tr("Office");
        break;
    case eObject::Temp :
        icon = QIcon(":/res/ProgramManager/EditProgram/Temp.png");
        name = tr("Temp");
        break;
    case eObject::Weather :
        icon = QIcon(":/res/ProgramManager/EditProgram/Weather.png");
        name = tr("Weather");
        break;
    case eObject::Rss :
        icon = QIcon(":/res/ProgramManager/EditProgram/Rss.png");
        name = tr("Rss");
        break;
    case eObject::Timer :
        icon = QIcon(":/res/ProgramManager/EditProgram/Timer.png");
        name = tr("Timer");
        break;
    case eObject::ColorText :
        break;
    case eObject::Window :
        icon = QIcon(":/res/ProgramManager/EditProgram/Window.png");
        name = tr("Window");
        break;

    default:
        break;
    }
    if(!name.isEmpty()) {
        QListWidgetItem *item = new QListWidgetItem();
        item->setIcon(icon);
        item->setText(name);
        item->setData(Qt::UserRole, QVariant::fromValue(static_cast<void*>(e)));
        ui->wList->addItem(item);
    }
}
void eWindowAttr::OnGeometryChanged(const QRectF & rt)
{
    m_eWindowRect=rt;
    qDebug()<<"OnGeometryChanged"<<rt.width();
    qDebug()<<"OnGeometryChanged"<<rt.height();
}
void eWindowAttr::onCreateElement(QAction *act)
{
    QIcon   icon;
    QString name = "";
    int type = act->data().toInt();
    int order = ui->wList->count();
    eObject *element = nullptr;
    switch (type) {
    case eObject::Text : {
        element = new eText(m_eWindowRect,eObject::Static);
        icon = QIcon(":/res/ProgramManager/EditProgram/Text.png");
        name = tr("Text");
        break;
    }
    case eObject::Photo : {
//        QString file = LoAppTools::getInstance()->selectFile(ePhoto::filters(), this);
//        if(!file.isNull()) {
//            element = new ePhoto(QRect(0,0,16,16),file, eObject::Static);
//            icon = QIcon(":/res/ProgramManager/EditProgram/Photo.png");
//            name = tr("Photo");
//        }
        QStringList fileList = LoAppTools::getInstance()->selectPhotoFile(ePhoto::filters(), this);
        if(fileList.count()>0)
        {
            for(int i=0;i<fileList.count();i++)
            {
                eObject *element1 = nullptr;
                element1 = new ePhoto(m_eWindowRect,fileList.at(i), eObject::Static);
                qDebug()<<"OnGeometryChanged-m_eWindowRect"<<m_eWindowRect.width();
                qDebug()<<"OnGeometryChanged-m_eWindowRect"<<m_eWindowRect.height();
                icon = QIcon(":/res/ProgramManager/EditProgram/Photo.png");
                QString strtempName=dynamic_cast<ePhoto *>(element1)->getData().name;
                //dynamic_cast<ePhoto *>element1
                name = tr("Photo")+ "  "+strtempName;
                if(nullptr != element1) {
                    QListWidgetItem *item = new QListWidgetItem();
                    item->setIcon(icon);
                    item->setText(name);
                    item->setData(Qt::UserRole, QVariant::fromValue(static_cast<void*>(element1)));
                    element1->setZValue(order);
                    element1->setFlag(QGraphicsItem::ItemStacksBehindParent);
                    m_attr.eList.append(element1);
                    ui->wList->addItem(item);
                    if(i==fileList.count()-1)
                        ui->wList->setCurrentItem(item);
                }
            }
        }

        break;
    }
    case eObject::Gif : {
//        QString file = LoAppTools::getInstance()->selectFile(eGif::filters(), this);
//        if(!file.isNull()) {
//            element = new eGif(m_eWindowRect,file, eObject::Static);
//            icon = QIcon(":/res/ProgramManager/EditProgram/Gif.png");
//            name = tr("Gif");
//        }
        QStringList fileList = LoAppTools::getInstance()->selectPhotoFile(eGif::filters(), this);
        if(fileList.count()>0)
        {
            for(int i=0;i<fileList.count();i++)
            {
                eObject *element1 = nullptr;
                element1 = new eGif(m_eWindowRect,fileList.at(i), eObject::Static);
                icon = QIcon(":/res/ProgramManager/EditProgram/Gif.png");
                QString strtempName=dynamic_cast<eGif *>(element1)->getData().name;
                name = tr("Gif")+ "  "+strtempName;
                if(nullptr != element1) {
                    QListWidgetItem *item = new QListWidgetItem();
                    item->setIcon(icon);
                    item->setText(name);
                    item->setData(Qt::UserRole, QVariant::fromValue(static_cast<void*>(element1)));
                    element1->setZValue(order);
                    element1->setFlag(QGraphicsItem::ItemStacksBehindParent);
                    m_attr.eList.append(element1);
                    ui->wList->addItem(item);
                    if(i==fileList.count()-1)
                        ui->wList->setCurrentItem(item);
                }
            }
        }
        break;
    }
    case eObject::Movie : {
        QString file = LoAppTools::getInstance()->selectFile(eMovie::filters(), this,true);
        if(!file.isNull()) {
            element = new eMovie(m_eWindowRect,file,this, eObject::Static);
            icon = QIcon(":/res/ProgramManager/EditProgram/Movie.png");
            QString strtempName=dynamic_cast<eMovie *>(element)->getData().strRawName;
            name = tr("Movie")+"  "+strtempName;

        }
        break;
    }

    case eObject::DClock :
        element = new eDClock(m_eWindowRect,eObject::Static);
        icon = QIcon(":/res/ProgramManager/EditProgram/DClock.png");
        name = tr("DClock");
        break;
    case eObject::AClock :
        element = new eAClock(m_eWindowRect,eObject::Static);
        icon = QIcon(":/res/ProgramManager/EditProgram/AClock.png");
        name = tr("AClock");
        break;
    case eObject::Office :
        break;
    case eObject::Temp :
        element = new eTemp(m_eWindowRect,eObject::Static);
        icon = QIcon(":/res/ProgramManager/EditProgram/Temp.png");
        name = tr("Temp");
        break;
    case eObject::Weather :
        element = new eWeather(m_eWindowRect,eObject::Static);
        icon = QIcon(":/res/ProgramManager/EditProgram/Weather.png");
        name = tr("Weather");
        break;
    case eObject::Rss :
        break;
    case eObject::Timer :
        element = new eTimer(m_eWindowRect,eObject::Static);
        icon = QIcon(":/res/ProgramManager/EditProgram/Timer.png");
        name = tr("Timer");
        break;
    case eObject::ColorText :
         name = tr("ColorText");
        break;
    case eObject::Window :
        element = new eWindow(m_eWindowRect,eObject::Static);
        icon = QIcon(":/res/ProgramManager/EditProgram/Window.png");
        name = tr("Window");
        break;
    default:
        element = nullptr;
        break;
    }
    if(nullptr != element) {
        QListWidgetItem *item = new QListWidgetItem();
        item->setIcon(icon);
        item->setText(name);
        item->setData(Qt::UserRole, QVariant::fromValue(static_cast<void*>(element)));
        element->setZValue(order);
        element->setFlag(QGraphicsItem::ItemStacksBehindParent);
        m_attr.eList.append(element);
        ui->wList->addItem(item);
        ui->wList->setCurrentItem(item);
    }
}

void eWindowAttr::onCurrentElementChanged(QListWidgetItem *cur, QListWidgetItem *last)
{
    eObject *eCur  = nullptr;
    eObject *eLast = nullptr;
    QWidget *wCur  = nullptr;
    QWidget *wLast = nullptr;

    if(nullptr != cur) {
        eCur = static_cast<eObject*>(cur->data(Qt::UserRole).value<void*>());
        wCur = eCur->wAttrElement();
    }
    if(nullptr != last) {
        eLast = static_cast<eObject*>(last->data(Qt::UserRole).value<void*>());
        wLast = findChild<QWidget*>("elementAttr");
    }

    if(wCur != nullptr) {
        if(wLast==nullptr)
        {
            //static_cast<QBoxLayout*>(layout())->insertWidget(2, wCur);
            ui->verticalLayoutSubWidget->insertWidget(0,wCur);
            ui->verticalLayoutSubWidget->removeItem(ui->verticalSpacer);
        }
        else
        {
            //static_cast<QBoxLayout*>(layout())->replaceWidget(wLast,wCur);
            ui->verticalLayoutSubWidget->replaceWidget(wLast,wCur);
        }
    }
    if(wLast != nullptr) {
        delete wLast;
    }
    if(eLast != nullptr) {
        if(m_attr.self->scene() != nullptr)
            m_attr.self->scene()->removeItem(eLast);
        disconnect(m_attr.self, SIGNAL(rectChanged(const QRectF&)),         eLast, SLOT(setGeometry(const QRectF&)));
        //alahover 20200329 masked
        disconnect(      eLast, SIGNAL(requestUpdate(const QRectF&)), m_attr.self, SLOT(updateGeometry()));
        eLast->stopElectment();
    }
    if(eCur != nullptr) {
        eCur->setParentItem(m_attr.self);
        eCur->setGeometry(m_attr.self->rect());
        //窗口变化大小时，eobject跟随设置大小
        connect(m_attr.self, SIGNAL(rectChanged(const QRectF&)),          eCur, SLOT(setGeometry(const QRectF&)));
        // 窗口的绘制跟随窗口的大小动态更新
       //alahover 20200329 masked
        connect(       eCur, SIGNAL(requestUpdate(const QRectF&)), m_attr.self, SLOT(updateGeometry()));
        eCur->playElectment();
    }

    m_attr.eCur  = eCur;
    m_attr.index = ui->wList->currentRow();
 //alahover 20200329 masked  emit sRequestUpdate();
}

void eWindowAttr::flashOrder()
{
    int n = ui->wList->count();
    for(int i=0; i<n; i++) {
        static_cast<eObject*>(ui->wList->item(i)->data(Qt::UserRole).value<void*>())->setZValue(i);
    }
}

void eWindowAttr::onDeleteElement()
{
    QListWidgetItem *item = ui->wList->currentItem();
    if(item != nullptr) {
        eObject *e = static_cast<eObject*>(item->data(Qt::UserRole).value<void*>());
        m_attr.eList.removeOne(e);
        ui->wList->takeItem(ui->wList->currentRow());
        if(ui->wList->count() > 0) {
            ui->wList->setCurrentRow(0);
        }
        if(e!=nullptr)
        {
            switch(e->m_iType)
            {
            case eObject::Photo:
                dynamic_cast<ePhoto *>(e)->deleteContent();
            break;
            case eObject::Movie:
                //dynamic_cast<eMovie *>(e)->stopElectment();

                dynamic_cast<eMovie *>(e)->deleteContent();
            break;
            case eObject::Gif:
                dynamic_cast<eGif *>(e)->deleteContent();
            break;
            }

            delete e;

        }

        delete item;
        flashOrder();
    }
}

void eWindowAttr::onClearElements()
{
    ui->wList->clear();
    foreach(eObject *e, m_attr.eList) {
        delete e;
    }
    m_attr.eList.clear();
}

void eWindowAttr::onGoUpElement()
{
    QListWidgetItem *item = ui->wList->currentItem();
    if(item != nullptr) {
        int row = ui->wList->currentRow();
        if(row > 0) {
            ui->wList->takeItem(row);
            ui->wList->insertItem(row-1, item);
            ui->wList->setCurrentRow(row-1);
            flashOrder();
            m_attr.eList.swap(row,row-1);
        }
    }
}

void eWindowAttr::onGoDownElement()
{
    QListWidgetItem *item = ui->wList->currentItem();
    if(item != nullptr) {
        int cnt = ui->wList->count();
        int row = ui->wList->currentRow();
        if(row < cnt - 1) {
            ui->wList->takeItem(row);
            ui->wList->insertItem(row+1, item);
            ui->wList->setCurrentRow(row+1);
            flashOrder();
            m_attr.eList.swap(row,row+1);
        }
    }
}
