#include "eaudioattr.h"
#include "ui_eaudioattr.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"

eAudioAttr::eAudioAttr(eAudio::Data &data, QWidget *parent) :
    eAttr(parent),
    ui(new Ui::eAudioAttr),
    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(QString stre, data.eList) {
        restoreItem(stre);
    }

//    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/Audio.png"),    tr("Window"))   ->setData(eObject::Window);
//    ui->wAdd->setMenu(menu);

//    connect(menu,        SIGNAL(triggered(QAction*)), this, SLOT(onCreateElement(QAction*)));
//
    connect(ui->wAdd,    SIGNAL(clicked()),           this, SLOT(onAddMp3()));
    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));
    }
}

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

void eAudioAttr::restoreItem(QString stre)
{
    QIcon   icon;
    QString name = "Mp3";
    icon = QIcon(":/res/ProgramManager/EditProgram/Audio.png");

    if(!name.isEmpty()) {
        QListWidgetItem *item = new QListWidgetItem();
        item->setIcon(icon);
        item->setText(name);
        item->setData(Qt::UserRole, stre);
        ui->wList->addItem(item);
    }
}
void eAudioAttr::OnGeometryChanged(const QRectF & rt)
{
    m_eWindowRect=rt;
    qDebug()<<"OnGeometryChanged"<<rt.width();
    qDebug()<<"OnGeometryChanged"<<rt.height();
}


void eAudioAttr::onCurrentElementChanged(QListWidgetItem *cur, QListWidgetItem *last)
{
    m_attr.strCur  = cur->data(Qt::UserRole).toString();
    m_attr.index = ui->wList->currentRow();
}

void eAudioAttr::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 eAudioAttr::onAddMp3()
{
    QIcon   icon;
    QString name = "";
    QStringList fileList = LoAppTools::getInstance()->selectPhotoFile(tr("Audio (*.mp3)"), this);
    if(fileList.count()>0)
    {
        for(int i=0;i<fileList.count();i++)
        {
            QString strFileName=fileList.at(i);
            m_attr.eList.append(strFileName);
            QFileInfo fileInfo(strFileName);
            QString strOnlyFileName=fileInfo.fileName();

            icon = QIcon(":/res/ProgramManager/EditProgram/Audio.png");
            name = tr("Mp3")+ "  "+strOnlyFileName;
            if(fileInfo.exists()) {
                QListWidgetItem *item = new QListWidgetItem();
                item->setIcon(icon);
                item->setText(name);
                item->setData(Qt::UserRole, strFileName);

                ui->wList->addItem(item);
                if(i==fileList.count()-1)
                    ui->wList->setCurrentItem(item);
            }
        }
    }
    if(fileList.count()>0) {
        QListWidgetItem *item = new QListWidgetItem();
        item->setIcon(icon);
        item->setText(name);
        ui->wList->addItem(item);
        ui->wList->setCurrentItem(item);
    }
}
void eAudioAttr::onDeleteElement()
{
    QListWidgetItem *item = ui->wList->currentItem();
    if(item != nullptr) {
        QString strSelected =item->data(Qt::UserRole).toString();// static_cast<QString>(item->data(Qt::UserRole).value<void*>());
        m_attr.eList.removeOne(strSelected);
        ui->wList->takeItem(ui->wList->currentRow());
        if(ui->wList->count() > 0) {
            ui->wList->setCurrentRow(0);
        }
        delete item;
        flashOrder();
    }
}

void eAudioAttr::onClearElements()
{
    ui->wList->clear();
    m_attr.eList.clear();
}

void eAudioAttr::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 eAudioAttr::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);
        }
    }
}
