#include "mapthread.h"
#include "qcoreapplication.h"
#include "qelapsedtimer.h"
#include "translator.h"

#include <QDebug>
#include <QProgressBar>


LoadTiffSceneData::LoadTiffSceneData(SceneControl* pSceneControl,UGDataSource* pDataSource,UGString datasetName,QString strPath,UGint importModel,QObject *parent)
    : QObject{parent},QRunnable()
{
    this->pSceneControl = pSceneControl;
    this->pDataSource = pDataSource;
    this->datasetName = datasetName;
    this->strPath = strPath;
    this->importModel = importModel;

    setAutoDelete(true); //任务执行完毕,自动销毁
}

void LoadTiffSceneData::run()
{
    UGString datasourceName = pDataSource->GetAlias();  //数据源名
    UGString layerName = datasetName + _U("@") + datasourceName;// 数据集名@数据源名,图层默认命名格式,也是通过该格式的名称获得关联数据
    if(importModel == UGImportParams::ModeIMG)  //添加影像底图
    {
        QElapsedTimer mstimer;
        mstimer.start();
        bool ss = geoFileParser.importTiffData(pDataSource,datasetName,strPath,true);
        float time = (double)mstimer.nsecsElapsed() / (double)1000000;
        qDebug() <<Translator::UGStr2QStr(layerName)<< "Time:::" << time/1000;
        if(ss)
        {
            pSceneControl->GetSceneEditWnd()->GetScene3D()->m_Layers.AddLayer(layerName);
//            qDebug()<<"******************:"<<Translator::UGStr2QStr(layerName);
        }
    }

    if(importModel == UGImportParams::ModeGrid) //添加地形底图
    {
        QElapsedTimer mstimer;
        mstimer.start();
        bool ss = geoFileParser.importTiffData(pDataSource,datasetName,strPath,true,
                                     UGImportParams::ModeGrid,UGDataCodec::encLZW);
        float time = (double)mstimer.nsecsElapsed() / (double)1000000;
        qDebug() <<Translator::UGStr2QStr(layerName)<< "Time:::" << time/1000;
        if(ss)
        {
            pSceneControl->GetSceneEditWnd()->GetScene3D()->m_TerrainLayer3Ds.AddTerrainLayer(layerName,datasetName);
        }
    }

    emit sendTaskName(Translator::UGStr2QStr(datasetName));

}



ShowProgressDialog::ShowProgressDialog(QObject *parent)
{
//    setAutoDelete(true); //任务执行完毕,自动销毁
}

void ShowProgressDialog::run()
{
    //显示进度条
    progressDlg = new QProgressDialog();
    progressDlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowMinimizeButtonHint);
//    progressDlg->setWindowFlags(Qt::WinToTop->windowFlags()|Qt::WindowStayOnTopHint);
    progressDlg->setWindowTitle("提示");
    progressDlg->setCancelButton(nullptr);
    QProgressBar* bar = new QProgressBar(progressDlg);
    bar->setRange(0, 0);
    bar->setValue(0);
    progressDlg->setBar(bar);
    progressDlg->setMinimumWidth(500);
    progressDlg->setMinimumDuration(0);
    progressDlg->setWindowModality(Qt::WindowModal);
    progressDlg->setValue(0);
    progressDlg->showNormal();
//    QCoreApplication::processEvents();//避免界面冻结

    /*
    progressDlg = new QProgressDialog();
//    progressDlg->setMinimumDuration(0);
    progressDlg->setWindowTitle("Please wait...");
    progressDlg->setFixedWidth(500);
    progressDlg->setRange(0, 100);
    progressDlg->show();
    currentValue = 0;
    progressDlg->setValue(currentValue);
//    timer = new QTimer();
////    connect(timer, &QTimer::timeout, this, &ShowProgressDialog::updateProgressDialog);
//    connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressDialog()));
//    timer->start(100);//开启一个没有终点的定时器
    */
}

void ShowProgressDialog::updateProgressDialog()
{
    qDebug()<<"***************:"<<currentValue;
    currentValue++;
    if( currentValue == 100 )
        currentValue = 0;
    progressDlg ->setValue(currentValue);

    QCoreApplication::processEvents();//避免界面冻结
    if(progressDlg->wasCanceled())
        progressDlg->setHidden(true);//隐藏对话框

}

void ShowProgressDialog::closedProgressDialog()
{
//    progressDlg->close();
    /*
    //耗时操作完成后,关闭进度对话框
      timer->stop();//停止定时器
      if(currentValue != 100)
          currentValue = 100;
      progressDlg->setValue(currentValue);//进度达到最大值
      delete progressDlg;//关闭进度对话框
      */
}



ProgressDialogTimer::ProgressDialogTimer(QObject *parent)
{

}

void ProgressDialogTimer::run()
{
//    timer = new QTimer();
//    connect(timer, &QTimer::timeout, progressDlg, &ShowProgressDialog::updateProgressDialog);
//    timer->start(100);//开启一个没有终点的定时器
}