#include "savepathwidget.h"

#include "global.h"
#include "strconv.h"
#include "pesdkutiltiy.h"
#include "fbxsdkutility.h"
#include "titlebar.h"
#include "QWaitProgressBar.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QFileDialog>
#include <QDesktopServices>
#include <QMessageBox>
#include <QFileInfo>
#include <QTextCodec>
#include <QDebug>

#include <thread>


#include "simplelog.h"


SavePathWidget::SavePathWidget(QWidget *parent)
    : QMainWindow(parent)
{
    initUI();
    connects();
}

SavePathWidget::~SavePathWidget()
{

}

QString SavePathWidget::fbxFilePath()
{
    return mLineEditFbxPath->text();
}

void SavePathWidget::setFbxFilePath(const QString &path)
{
    mLineEditFbxPath->setText(path);
}

void SavePathWidget::setFieldPath(const QString &path)
{
    mFieldPath = path;
}

void SavePathWidget::initUI()
{
    mTitleBar = new TitleBar(this);
    mTitleBar->updateWidgetFlags();

    mCentralWidget = new QWidget;
    mMainVLayout = new QVBoxLayout;

    mHLayoutFbxPath = new QHBoxLayout;
    mLabelFbxPath = new QLabel("保存路径：");
    mLineEditFbxPath = new QLineEdit;
    mButtonSelectFbxSavePath = new QPushButton("选择");
    mButtonOpenFbxFileDir = new QPushButton("打开目录");
    mHLayoutFbxPath->addWidget(mLabelFbxPath);
    mHLayoutFbxPath->addWidget(mLineEditFbxPath);
    mHLayoutFbxPath->addWidget(mButtonSelectFbxSavePath);
    mHLayoutFbxPath->addWidget(mButtonOpenFbxFileDir);

    mHLayoutFunc = new QHBoxLayout;
    mButtonConvertFbx = new QPushButton("转换");
    mButtonCancelConvert = new QPushButton("取消");
    mButtonClose = new QPushButton("关闭");
    mHLayoutFunc->addStretch();
    mHLayoutFunc->addWidget(mButtonConvertFbx);
    mHLayoutFunc->addWidget(mButtonCancelConvert);
    mHLayoutFunc->addWidget(mButtonClose);

    mWaitProgressBar = new QWaitProgressBar(QWaitProgressBar::Petal);
    mWaitProgressBar->setForeColor(QColor(255, 0, 155));
    mWaitProgressBar->setMinimumSize(206, 206);
    mWaitProgressBar->setClockwiseRotate(false);
    mWaitProgressBar->hide();

    mMainVLayout->addLayout(mHLayoutFbxPath);
    mMainVLayout->addWidget(mWaitProgressBar);
    mMainVLayout->addLayout(mHLayoutFunc);

    mCentralWidget->setLayout(mMainVLayout);
    this->setCentralWidget(mCentralWidget);
    this->setWindowIcon(QIcon(":/images/logo.png"));
    this->setWindowTitle("转换fbx");
    this->resize(584, 340);
}

void SavePathWidget::connects()
{
    connect(mButtonSelectFbxSavePath, &QPushButton::clicked, this, &SavePathWidget::selectFbxSavePath);
    connect(mButtonOpenFbxFileDir, &QPushButton::clicked, this, &SavePathWidget::openFbxFileDir);
    connect(mButtonConvertFbx, &QPushButton::clicked, this, &SavePathWidget::startConvertFbx);
    connect(mButtonClose, &QPushButton::clicked, this, &SavePathWidget::cancel);

    connect(this, &SavePathWidget::convertCompleted, this, &SavePathWidget::convertCompletedTip);
}

bool SavePathWidget::conditionJudge()
{
    QString filename = mLineEditFbxPath->text();
    if(filename.isEmpty())
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("请输入保存的fbx路径");
        msgBox.exec();
        return false;
    }

    QFileInfo info(filename);
    if(!info.dir().exists())
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("保存fbx的目录不存在，请重新设置fbx保存路径");
        msgBox.exec();
        return false;
    }

    Global::Instance()->setConvertFbxPath(filename);
    return true;
}

void SavePathWidget::selectFbxSavePath()
{
    QString lastDirPath;
    QFileInfo info(mLineEditFbxPath->text());
    if(info.dir().exists())
    {
        lastDirPath = info.dir().path();
    }
    else
    {
        lastDirPath = Global::Instance()->rootDirPath();
    }
    QString fileName = QFileDialog::getSaveFileName(this, "转换fbx",
                                                    lastDirPath,
                                                    "FBX文件 (*.fbx)");
    if(fileName.isEmpty())
        return;
    mLineEditFbxPath->setText(fileName);
    Global::Instance()->setConvertFbxPath(fileName);
}

void SavePathWidget::openFbxFileDir()
{
    QFileInfo info(mLineEditFbxPath->text());
    QDesktopServices::openUrl(QUrl::fromLocalFile(info.dir().path()));
}

void SavePathWidget::startConvertFbx()
{
    std::string path = StrConv::UnicodeTochar(mFieldPath.toStdWString().c_str());
    VsDatabase* penode = Vis::fieldPathToVisNode(path);
    if(!penode)
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("哦，糟糕，您选中的节点，没有找到相关数据。");
        msgBox.exec();
        return;
    }

    if(!conditionJudge())
        return;

    mWaitProgressBar->show();

    QString _savePath = mLineEditFbxPath->text();
    std::string savePath = StrConv::UnicodeToUTF8(_savePath.toStdWString().c_str());
    std::thread t([=](){
        Vis::traverseVisNodeAndSaveAsFbxFile(penode, savePath);
        emit convertCompleted();
    });

    //t.join();
    t.detach();
}

void SavePathWidget::cancel()
{
    this->hide();
}

void SavePathWidget::convertCompletedTip()
{
    mWaitProgressBar->hide();
    this->hide();

    QMessageBox msgBox;
    msgBox.setWindowTitle("提示");
    msgBox.setText(QString("保存fbx(%1)文件完成").arg(mLineEditFbxPath->text()));
    msgBox.exec();
}
