#include "eaudio.h"
#include "cfg.h"
#include "tools.h"
#include "base/ffutil.h"
#include <QBoxLayout>
#include <QDir>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QMovie>
#include <QPainter>
#include <QPushButton>
#include <QSpinBox>

EAudio *EAudio::create(const QString &file, PageListItem *pageItem, EBase *multiWin) {
    int64_t dur;
    QString err = audioInfo(file.toUtf8(), &dur);
    if(! err.isEmpty()) {
        QMessageBox::critical(gMainWin, "Audio Error", err+"\n"+file);
        return nullptr;
    }
    QFileInfo info(file);
    return new EAudio(info.absolutePath(), info.fileName(), dur/1000000, pageItem, multiWin);
}
EAudio *EAudio::create(const QJsonObject &json, PageListItem *pageItem, EBase *multiWin) {
    auto dir = json["fileDir"].toString();
    auto name = json["fileName"].toString();
    if(! QFileInfo::exists(dir)) dir = pageItem->mPageDir;
    QString file = dir + "/" + name;
    if(QFileInfo::exists(file)) ;
    else if(QFileInfo::exists(file = pageItem->mPageDir + "/" + name)) dir = pageItem->mPageDir;
    else return nullptr;
    auto ins = new EAudio(dir, name, json["duration"].toInt(), pageItem, multiWin);
    ins->setBaseAttr(json);
    return ins;
}
QJsonObject EAudio::genProg(const QJsonObject &ele, const QString &dstDir) {
    auto name = ele["fileName"].toString();
    QString srcFile = ele["fileDir"].toString() + "/" + name;
    QFileInfo srcInfo(srcFile);
    if(! srcInfo.isFile()) return QJsonObject();
    QString id = Tools::fileMd5(srcFile);
    if(id.isEmpty()) return QJsonObject();
    QFile::copy(srcFile, dstDir+"/"+id);
    QJsonObject oRes;
    oRes["_type"] = "Audio";
    oRes["id"] = id;
    oRes["md5"] = id;
    oRes["name"] = name;
    oRes["size"] = srcInfo.size();
    oRes["fileExt"] = srcInfo.suffix().toLower();
    oRes["timeSpan"] = ele["duration"].toInt();
    oRes["enabled"] = true;
    return oRes;
}

EAudio::EAudio(const QString &dir, const QString &name, int dur, PageListItem *pageItem, EBase *multiWin) : EBase(multiWin), mDir(dir), mName(name), mDuration(dur), mPageItem(pageItem) {
    mType = EBase::Audio;
}

QJsonObject EAudio::attrJson() const {
    QJsonObject obj;
    addBaseAttr(obj);
    obj["elementType"] = "Audio";
    obj["fileDir"] = mDir;
    obj["fileName"] = mName;
    obj["duration"] = mDuration;
    return obj;
}

bool EAudio::save(const QString &pageDir) {
    QString oldFile = mDir + PAGEDEL_SUFFIX + "/" + mName;
    if(QFileInfo::exists(oldFile)) ;
    else if(QFileInfo::exists(oldFile = mDir + "/" + mName)) ;
    else return false;
    QFile::copy(oldFile, pageDir + "/" + mName);
    mDir = pageDir;
    return true;
}

void EAudio::paint(QPainter *painter, const QStyleOptionGraphicsItem *a, QWidget *b) {
    painter->setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
    static auto img = QPixmap(":/res/program/Audio.png");
    auto inner = innerRect();
    if(inner.width() > inner.height()) inner.setWidth(inner.height());
    else if(inner.height() > inner.width()) inner.setHeight(inner.width());
    painter->drawPixmap(inner, img, QRectF());
    EBase::paint(painter, a, b);
}

QWidget* EAudio::attrWgt() {
    auto wgtAttr = new QWidget();
    auto vBox = new QVBoxLayout(wgtAttr);
    vBox->setContentsMargins(6, 0, 6, 0);
    if(mMultiWin!=nullptr) vBox->setSpacing(3);

    addBaseAttrWgt(vBox);

    auto hBox = new QHBoxLayout();
    hBox->addWidget(new QLabel(tr("Basic Properties")));

    auto line = new QFrame();
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    hBox->addWidget(line, 1);

    vBox->addLayout(hBox);

    hBox = new QHBoxLayout();
    hBox->addSpacing(6);
    hBox->addWidget(new QLabel(tr("File")));

    auto fdFileName = new QLineEdit(mName);
    fdFileName->setReadOnly(true);
    hBox->addWidget(fdFileName);

    auto bnSelectFile = new QPushButton("...");
    bnSelectFile->setFixedWidth(30);
    bnSelectFile->setObjectName("bnSelectFile");
    hBox->addWidget(bnSelectFile);

    vBox->addLayout(hBox);

    hBox = new QHBoxLayout();
    hBox->addWidget(new QLabel(tr("Play Properties")));

    line = new QFrame();
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    hBox->addWidget(line, 1);

    vBox->addLayout(hBox);

    hBox = new QHBoxLayout();
    hBox->addSpacing(6);
    hBox->addWidget(new QLabel(tr("Play Duration")));

    auto fdDuration = new QSpinBox();
    fdDuration->setRange(1, 99999);
    fdDuration->setValue(mDuration);
    connect(fdDuration, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [this](int value) {
        mDuration = value;
    });
    connect(bnSelectFile, &QPushButton::clicked, this, [this, fdFileName, fdDuration] {
        auto file = QFileDialog::getOpenFileName(gMainWin, tr("Select File"), gFileHome, EAudio::filters());
        if(file.isEmpty()) return;
        int64_t dur;
        QString err = audioInfo(file.toUtf8(), &dur);
        if(! err.isEmpty()) {
            QMessageBox::critical(gMainWin, "Audio Error", err+"\n"+file);
            return;
        }
        QFileInfo info(file);
        mDir = info.absolutePath();
        gFileHome = mDir;
        mName = info.fileName();
        fdFileName->setText(mName);
        fdDuration->setValue(dur/1000000);
    });
    hBox->addWidget(fdDuration);
    hBox->addWidget(new QLabel(tr("s")));
    hBox->addStretch();

    vBox->addLayout(hBox);
    vBox->addStretch();
    return wgtAttr;
}
