#include "wxAnswer.h"
#include "ui_wxAnswer.h"

#include "CAppInfo.h"
#include "awMsgBox.h"
#include "CQREncode.h"
#include "awMsgBox.h"
#include <QDesktopWidget>
#include "CAudioPlayerProc.h"
#include <QFile>
#include <QFileInfo>
#include <QSettings>


wxAnswer::wxAnswer(QString sAnswerType, int nOrder, QWidget *parent) :
    CBaseWidget(parent),
    ui(new Ui::wxAnswer), m_nOrder(nOrder), m_sAnswerType(sAnswerType)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

    qRegisterMetaType<CGetWXQrCode>("CGetWXQrCode");
    connect(g_httpBllPtr.get(), &CHttpBll::sgnGetWXQrCode, this, &wxAnswer::onGetWXQrCode);
    qRegisterMetaType<CSaveUploadedFileAcknowledgeStatus>("CSaveUploadedFileAcknowledgeStatus");
    connect(g_httpBllPtr.get(), &CHttpBll::sgnSaveUploadedFileAcknowledgeStatus, this, &wxAnswer::onSaveUploadedFileAcknowledgeStatus);
    qRegisterMetaType<CDownLoadFileInfo>("CDownLoadFileInfo");
    connect(g_httpBllPtr.get(), &CHttpBll::sgnDownLoadFile, this, &wxAnswer::onDownLoadFile);

    if (g_websocketPtr == nullptr)
    {
        g_websocketPtr = std::make_shared<CWebsocketProc>();
    }

    if(!g_websocketPtr->isConnected())
    {
        g_websocketPtr->open(QString("wss://%1:%2/api/ws/fileAnswer")
            .arg(g_appInfoPtr->m_sHttpServer).arg(g_appInfoPtr->m_sHttpPort),
            g_httpBllPtr->getToken(), g_httpBllPtr->getKey());
    }
    connect(g_websocketPtr.get(), &CWebsocketProc::mobileStatus, this, &wxAnswer::onMobileStatus);
    connect(g_websocketPtr.get(), &CWebsocketProc::fileAnswer, this, &wxAnswer::onFileAnswer);

//    if(m_picList == nullptr)
//    {
//        if(m_picList == nullptr)
//        {
//            m_picList = std::make_shared<wPictureList>(g_appInfoPtr->m_fRate*108, g_appInfoPtr->m_fRate*10, ui->widget_wxa);
//        }

//        connect(m_picList.get(), &wPictureList::picChange, this, [&](){
//            int nHeight = m_picList->setUI(g_appInfoPtr->m_fRate*30, ui->label_wxa_qrcode->y() + ui->label_wxa_qrcode->height() + g_appInfoPtr->m_fRate*20,
//                                   ui->widget_wxa->width() - g_appInfoPtr->m_fRate*30*2);
//            ui->btn_wxa_comfirm->setGeometry((ui->widget_wxa->width() - g_appInfoPtr->m_fRate*120)/2, m_picList->y() + m_picList->height() + g_appInfoPtr->m_fRate*30,
//                                             g_appInfoPtr->m_fRate*120, g_appInfoPtr->m_fRate*40);
//            ui->widget_wxa->setGeometry((width() - g_appInfoPtr->m_fRate*760)/2, (height() - (ui->btn_wxa_comfirm->y() + ui->btn_wxa_comfirm->height() + g_appInfoPtr->m_fRate*30))/2,
//                                        g_appInfoPtr->m_fRate*760, ui->btn_wxa_comfirm->y() + ui->btn_wxa_comfirm->height() + g_appInfoPtr->m_fRate*30);


//            m_picList->show();

//            if(m_picList->itemCount() > 5)
//            {
//                ui->label_wxa_qrcode->setVisible(false);
//            }
//            else
//            {
//                ui->label_wxa_qrcode->setVisible(true);
//            }
//        });
//    }

    CHttpRequestPackage hrp;
    hrp.sUri = "/api/ecs_oe_student/examControl/getQrCode";
    hrp.nRequestType = RequestType::rtGetWXQrCode;
    Json::Value jBody = Json::Value::null;
    jBody["examRecordDataId"] = g_appInfoPtr->m_oExamInfo.nExamRecordDataId;
    jBody["order"] = nOrder;
    if(sAnswerType == "SINGLE_AUDIO")
    {
        jBody["transferFileType"] = "AUDIO";
    }
    else
    {
       jBody["transferFileType"] = "PIC";
    }
    jBody["testEnv"] = false;
    hrp.sParamList.push_back(QString("CustomBody,%1").arg(jBody.toStyledString().c_str()));
    hrp.eParamType = HttpParamType::hptCustomBody;
    g_httpBllPtr->post(hrp);

    QFileInfo file("coe.cfgi");
    QString sFilePath = file.absoluteFilePath();
    QSettings set(sFilePath, QSettings::IniFormat);

    m_nPlayVol = set.value("config/playVol", 100).toInt();

    ui->hslider_etvt_volume->setValue(m_nPlayVol);

    if (g_audioPalyerPtr == nullptr)
    {
        g_audioPalyerPtr = std::make_shared<CAudioPlayerProc>();
    }

    m_pTimer = std::make_shared<QTimer>();
    m_pTimer->setInterval(1000);
    connect(m_pTimer.get(), &QTimer::timeout, this, [&]() {
        ui->hslider_etvt_voice->setValue(ui->hslider_etvt_voice->value() + 1);
        if (ui->hslider_etvt_voice->value() >= m_nAudioSecord)
        {
            m_pTimer->stop();
            g_audioPalyerPtr->stopPlay();
        }
        else
        {
            std::string sDeciceName = "";
            if (!g_audioPalyerPtr->getDeviceinfo(sDeciceName))
            {
                m_pTimer->stop();
                QString sErrorStr = g_audioPalyerPtr->getErrMsg();
                ShowMsg(sErrorStr, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
            }

            if (sDeciceName != m_sRecordDevice)
            {
                ShowMsg(QString::fromLocal8Bit("播放设备拔出，请检查设备！"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
                //            myDebug()<<QString::fromLocal8Bit("播放设备拔出，请检查设备！");
            }
        }

    });
	
}

wxAnswer::~wxAnswer()
{
	QFileInfo file("coe.cfgi");
	QString sFilePath = file.absoluteFilePath();
	QSettings set(sFilePath, QSettings::IniFormat);
	
	set.setValue("config/playVol", m_nPlayVol);

    delete ui;
}

int wxAnswer::setUI(const int nWidth)
{
    if(m_sAnswerType == "SINGLE_AUDIO")
    {
        ui->label_wxa_title->setVisible(false);
        ui->btn_wxa_close->setVisible(false);
        ui->label_HLine->setVisible(false);
        ui->btn_wxa_comfirm->setVisible(false);
        setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*340);
        ui->widget_bg->setGeometry(0, 0, width(), height());
        ui->widget_wxa->setGeometry(0, 0, width(), height());    
        ui->label_wxa_qrcode->setGeometry(0, 0,
                                          g_appInfoPtr->m_fRate*180, g_appInfoPtr->m_fRate*180);
        ui->label_wxa_tips->setFixedWidth(g_appInfoPtr->m_fRate*490);
        ui->label_wxa_tips->setText(QString::fromLocal8Bit(R"(<html>
                                                           <style>
                                                               p{
                                                                   font-family: "Microsoft YaHei";
                                                                   font-size: %1px;
                                                                   font-weight: 400;
                                                                   color: #717171;
                                                               }
                                                               span
                                                               {
                                                                   font-family: Microsoft YaHei;
                                                                   font-size: %2px;
                                                                   font-weight: 400;
                                                                   color: #13BA8C;
                                                               }
                                                           </style>
                                                       <body>
                                                           <p>
                                                           请使用<span>微信</span>扫描二维码后，在微信小程序上录音，并上传文件。
                                                       </p>
                                                       <p>
                                                           上传期间，请勿关闭二维码。
                                                       </p>
                                                       </body>
                                                       </html>
                                                    )").arg((int)(g_appInfoPtr->m_fRate*24)).arg((int)(g_appInfoPtr->m_fRate*24)));

        ui->label_wxa_tips->adjustSize();
        ui->label_wxa_tips->setGeometry(ui->label_wxa_qrcode->x() + ui->label_wxa_qrcode->width() + g_appInfoPtr->m_fRate*30,
                                        ui->label_wxa_qrcode->y(), g_appInfoPtr->m_fRate*490, ui->label_wxa_tips->height());
        ui->label_wxa_statusIcon->setGeometry(ui->label_wxa_tips->x(), ui->label_wxa_tips->y() + ui->label_wxa_tips->height() + g_appInfoPtr->m_fRate*30,
                                              g_appInfoPtr->m_fRate*28, g_appInfoPtr->m_fRate*28);
        ui->label_wxa_status->setFixedWidth(g_appInfoPtr->m_fRate*490);
        ui->label_wxa_status->adjustSize();
        ui->label_wxa_status->setGeometry(ui->label_wxa_statusIcon->x() + ui->label_wxa_statusIcon->width() + g_appInfoPtr->m_fRate*10,
                                          ui->label_wxa_statusIcon->y() + (ui->label_wxa_statusIcon->height() - ui->label_wxa_status->height())/2,
                                          ui->label_wxa_status->width(), ui->label_wxa_status->height());
        ui->label_wxa_answerTips->adjustSize();
        ui->label_wxa_answerTips->setGeometry(ui->label_wxa_qrcode->x(), ui->label_wxa_qrcode->y() + ui->label_wxa_qrcode->height() + g_appInfoPtr->m_fRate*20,
                                              ui->label_wxa_answerTips->width(), ui->label_wxa_answerTips->height());
        ui->widget_etvt_voice->setVisible(false);
        ui->widget_etvt_voice->setGeometry(ui->label_wxa_answerTips->x() + ui->label_wxa_answerTips->width() + g_appInfoPtr->m_fRate*10,
                                           ui->label_wxa_answerTips->y(),
                                           g_appInfoPtr->m_fRate*423, g_appInfoPtr->m_fRate*40);
        ui->btn_etvt_play->setGeometry(g_appInfoPtr->m_fRate*8, (ui->widget_etvt_voice->height() - g_appInfoPtr->m_fRate*24)/2,
                                       g_appInfoPtr->m_fRate*24, g_appInfoPtr->m_fRate*24);
        ui->hslider_etvt_voice->setGeometry(ui->btn_etvt_play->x() + ui->btn_etvt_play->width() + g_appInfoPtr->m_fRate*21,
                                            (ui->widget_etvt_voice->height() - g_appInfoPtr->m_fRate*16)/2,
                                            g_appInfoPtr->m_fRate*224, g_appInfoPtr->m_fRate*12);
        ui->btn_etvt_volume->setGeometry(ui->hslider_etvt_voice->x() + ui->hslider_etvt_voice->width() + g_appInfoPtr->m_fRate*20,
                                         ui->btn_etvt_play->y(), ui->btn_etvt_play->width(), ui->btn_etvt_play->height());
        ui->hslider_etvt_volume->setGeometry(ui->btn_etvt_volume->x() + ui->btn_etvt_volume->width() + g_appInfoPtr->m_fRate * 10, ui->hslider_etvt_voice->y(),
                                            g_appInfoPtr->m_fRate*50, ui->hslider_etvt_voice->height());
    }
    else
    {
        if(m_picList == nullptr)
        {
            if(m_picList == nullptr)
            {
                m_picList = std::make_shared<wPictureList>(g_appInfoPtr->m_fRate*108, g_appInfoPtr->m_fRate*10, ui->widget_wxa);
            }

            connect(m_picList.get(), &wPictureList::picChange, this, [&](){
                int nHeight = m_picList->setUI(g_appInfoPtr->m_fRate*30, ui->label_wxa_qrcode->y() + ui->label_wxa_qrcode->height() + g_appInfoPtr->m_fRate*20,
                                       ui->widget_wxa->width() - g_appInfoPtr->m_fRate*30*2);
                ui->btn_wxa_comfirm->setGeometry((ui->widget_wxa->width() - g_appInfoPtr->m_fRate*120)/2, m_picList->y() + m_picList->height() + g_appInfoPtr->m_fRate*30,
                                                 g_appInfoPtr->m_fRate*120, g_appInfoPtr->m_fRate*40);
                ui->widget_wxa->setGeometry((width() - g_appInfoPtr->m_fRate*760)/2, (height() - (ui->btn_wxa_comfirm->y() + ui->btn_wxa_comfirm->height() + g_appInfoPtr->m_fRate*30))/2,
                                            g_appInfoPtr->m_fRate*760, ui->btn_wxa_comfirm->y() + ui->btn_wxa_comfirm->height() + g_appInfoPtr->m_fRate*30);


                m_picList->show();

                if(m_picList->itemCount() > 5)
                {
                    ui->label_wxa_qrcode->setVisible(false);
                }
                else
                {
                    ui->label_wxa_qrcode->setVisible(true);
                }
            });
        }

        ui->widget_bg->setStyleSheet(QString(R"(QWidget#widget_bg
                                             {
                                                 background:rgba(89,89,89,0.8);
                                             })"));
        ui->widget_wxa->setStyleSheet(QString(R"(QWidget#widget_wxa
                                              {
                                                  border-radius:%1px;
                                                  background:rgba(255,255,255,1);
                                              })").arg((int)(g_appInfoPtr->m_fRate*16)));
        
        ui->label_wxa_answerTips->setVisible(false);
        ui->widget_etvt_voice->setVisible(false);        
        ui->btn_etvt_play->setVisible(false);
        ui->hslider_etvt_voice->setVisible(false);
        ui->btn_etvt_volume->setVisible(false);
        QDesktopWidget *dekwiget = QApplication::desktop();
        setGeometry(0, 0, dekwiget->width(), dekwiget->height());
        ui->widget_bg->setGeometry(0, 0, width(), height());
        ui->widget_wxa->setGeometry((width() - g_appInfoPtr->m_fRate*760)/2, (height() - g_appInfoPtr->m_fRate*395)/2, g_appInfoPtr->m_fRate*760, g_appInfoPtr->m_fRate*395);
        ui->label_wxa_title->adjustSize();
        ui->label_wxa_title->setGeometry(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*16,
                                         ui->label_wxa_title->width(), ui->label_wxa_title->height());
        ui->btn_wxa_close->setGeometry(ui->widget_wxa->width() - g_appInfoPtr->m_fRate*(20+16), g_appInfoPtr->m_fRate*19, g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16);
        ui->label_HLine->setGeometry(0, g_appInfoPtr->m_fRate*54, ui->widget_wxa->width(), g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1);
        ui->label_wxa_qrcode->setGeometry(g_appInfoPtr->m_fRate*30, ui->label_HLine->y() + ui->label_HLine->height() + g_appInfoPtr->m_fRate*30,
                                          g_appInfoPtr->m_fRate*180, g_appInfoPtr->m_fRate*180);
        ui->label_wxa_tips->setFixedWidth(g_appInfoPtr->m_fRate*490);
        ui->label_wxa_tips->setText(QString::fromLocal8Bit(R"(<html>
                                                           <style>
                                                               p{
                                                                   font-family: "Microsoft YaHei";
                                                                   font-size: %1px;
                                                                   font-weight: 400;
                                                                   color: #717171;
                                                               }
                                                               span
                                                               {
                                                                   font-family: Microsoft YaHei;
                                                                   font-size: %2px;
                                                                   font-weight: 400;
                                                                   color: #13BA8C;
                                                               }
                                                           </style>
                                                       <body>
                                                           <p>
                                                           请使用<span>微信</span>扫描二维码后，在微信小程序上拍照，并上传文件。
                                                       </p>
                                                       <p>
                                                           上传期间，请勿关闭二维码(图片上传最多支持6张)。
                                                       </p>
                                                       </body>
                                                       </html>
                                                    )").arg((int)(g_appInfoPtr->m_fRate*24)).arg((int)(g_appInfoPtr->m_fRate*24)));
        ui->label_wxa_tips->adjustSize();
        ui->label_wxa_tips->setGeometry(ui->label_wxa_qrcode->x() + ui->label_wxa_qrcode->width() + g_appInfoPtr->m_fRate*30,
                                        ui->label_wxa_qrcode->y(), g_appInfoPtr->m_fRate*490, ui->label_wxa_tips->height());
        ui->label_wxa_statusIcon->setGeometry(ui->label_wxa_tips->x(), ui->label_wxa_tips->y() + ui->label_wxa_tips->height() + g_appInfoPtr->m_fRate*20,
                                              g_appInfoPtr->m_fRate*28, g_appInfoPtr->m_fRate*28);
        ui->label_wxa_status->setFixedWidth(g_appInfoPtr->m_fRate*490);
        ui->label_wxa_status->adjustSize();
        ui->label_wxa_status->setGeometry(ui->label_wxa_statusIcon->x() + ui->label_wxa_statusIcon->width() + g_appInfoPtr->m_fRate*10,
                                          ui->label_wxa_statusIcon->y() + (ui->label_wxa_statusIcon->height() - ui->label_wxa_status->height())/2,
                                          ui->label_wxa_status->width(), ui->label_wxa_status->height());
        ui->btn_wxa_comfirm->setGeometry((ui->widget_wxa->width() - g_appInfoPtr->m_fRate*120)/2, ui->widget_wxa->height() - g_appInfoPtr->m_fRate*(30 + 40),
                                         g_appInfoPtr->m_fRate*120, g_appInfoPtr->m_fRate*40);
    }
    ui->label_wxa_statusIcon->setVisible(false);
    ui->label_wxa_status->setVisible(false);
    return height();
}

void wxAnswer::onGetWXQrCode(CGetWXQrCode getWXQrCode)
{
    if (getWXQrCode.nCode == 200)
    {
        ui->label_wxa_qrcode->setPixmap(CQREncode::GenerateQRcode(QString("%1%2")
                                                                  .arg(getWXQrCode.sUrl).arg(QByteArray(("&apiServer=" + g_httpBllPtr->getHttpUrl()).toLocal8Bit()).toPercentEncoding().data()), ui->label_wxa_qrcode->width()));
    }
    else
    {       
        if(getWXQrCode.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("获取二维码失败"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(getWXQrCode.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }

    }
}

QString wxAnswer::getFileUrl()
{
    return m_sFileUrl;
}

void wxAnswer::setPics(QString sFileUrls)
{
	if (sFileUrls.isEmpty())
	{
		return;
	}
    m_sFileUrl = sFileUrls;
    if(m_picList == nullptr)
    {
        m_picList = std::make_shared<wPictureList>(g_appInfoPtr->m_fRate*108, g_appInfoPtr->m_fRate*10, ui->widget_wxa);
    }

	m_picList->clearPic();
	if (!sFileUrls.isEmpty())
	{
		QStringList fileList = sFileUrls.split(",");
		for (QString sFile : fileList)
		{
			m_picList->AddPic(sFile);
		}
	}

	int nHeight = m_picList->setUI(g_appInfoPtr->m_fRate * 30, ui->label_wxa_qrcode->y() + ui->label_wxa_qrcode->height() + g_appInfoPtr->m_fRate * 20,
		ui->widget_wxa->width() - g_appInfoPtr->m_fRate * 30 * 2);
	ui->btn_wxa_comfirm->setGeometry((ui->widget_wxa->width() - g_appInfoPtr->m_fRate * 120) / 2, m_picList->y() + m_picList->height() + g_appInfoPtr->m_fRate * 30,
		g_appInfoPtr->m_fRate * 120, g_appInfoPtr->m_fRate * 40);
	ui->widget_wxa->setGeometry((width() - g_appInfoPtr->m_fRate * 760) / 2, (height() - (ui->btn_wxa_comfirm->y() + ui->btn_wxa_comfirm->height() + g_appInfoPtr->m_fRate * 30)) / 2,
		g_appInfoPtr->m_fRate * 760, ui->btn_wxa_comfirm->y() + ui->btn_wxa_comfirm->height() + g_appInfoPtr->m_fRate * 30);
    
    
    m_picList->show();
}


void wxAnswer::onMobileStatus(__int64 nExamRecordDataId, int nOrder, QString sScanStatus)
{
    if (sScanStatus == "SCANNED" && nOrder == m_nOrder)
    {
        ui->label_wxa_statusIcon->setVisible(true);
        ui->label_wxa_status->setVisible(true);
        ui->label_wxa_status->setText(QString::fromLocal8Bit("已扫码"));
    }
}

void wxAnswer::onFileAnswer(__int64 nExamRecordDataId, int nOrder, QString sFileType, QString sFileUrl)
{
    if(nOrder == m_nOrder)
    {
        
		CHttpRequestPackage hrp;
		hrp.sUri = "/api/ecs_oe_student/examControl/saveUploadedFileAcknowledgeStatus";
		hrp.nRequestType = RequestType::rtSaveUploadedFileAcknowledgeStatus;
		Json::Value jBody = Json::Value::null;
		jBody["examRecordDataId"] = nExamRecordDataId;
		jBody["order"] = nOrder;
		jBody["filePath"] = sFileUrl.toStdString();
		jBody["acknowledgeStatus"] = "CONFIRMED";
		hrp.sParamList.push_back(QString("CustomBody,%1").arg(jBody.toStyledString().c_str()));
		hrp.sCommonStr = sFileUrl;
		hrp.eParamType = HttpParamType::hptCustomBody;
		g_httpBllPtr->post(hrp);

        if(m_sAnswerType == "SINGLE_AUDIO")
        {      
			m_sFileUrl = sFileUrl;
            {
                QString sFileName = sFileUrl.right(sFileUrl.length() - sFileUrl.lastIndexOf("/") - 1);
                sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
                CHttpRequestPackage hrp;
                hrp.sUri = sFileUrl;
                hrp.sCommonStr = sFileName;
                hrp.sCommonStr1 = __FILE__;
                hrp.nRequestType = RequestType::rtDownLoadFile;
                hrp.nRetryCount = 3;
                g_httpBllPtr->downLoad(hrp);
            }
        }
        else
        {						
            if(m_picList == nullptr)
            {
                m_picList = std::make_shared<wPictureList>(g_appInfoPtr->m_fRate*108, g_appInfoPtr->m_fRate*10, ui->widget_wxa);
            }

            QStringList fileList;
            if(!sFileUrl.isEmpty())
            {
                fileList = sFileUrl.split(",");
                for (QString sFile : fileList)
                {
                    m_picList->AddPic(sFile);
                }
            }

            if(m_picList->itemCount() > 5)
            {
                ui->label_wxa_qrcode->setVisible(false);
            }

            int nHeight = m_picList->setUI(g_appInfoPtr->m_fRate*30, ui->label_wxa_qrcode->y() + ui->label_wxa_qrcode->height() + g_appInfoPtr->m_fRate*20,
                                   ui->widget_wxa->width() - g_appInfoPtr->m_fRate*30*2);
            ui->btn_wxa_comfirm->setGeometry((ui->widget_wxa->width() - g_appInfoPtr->m_fRate*120)/2, m_picList->y() + m_picList->height() + g_appInfoPtr->m_fRate*30,
                                             g_appInfoPtr->m_fRate*120, g_appInfoPtr->m_fRate*40);
            ui->widget_wxa->setGeometry((width() - g_appInfoPtr->m_fRate*760)/2, (height() - (ui->btn_wxa_comfirm->y() + ui->btn_wxa_comfirm->height() + g_appInfoPtr->m_fRate*30))/2,
                                        g_appInfoPtr->m_fRate*760, ui->btn_wxa_comfirm->y() + ui->btn_wxa_comfirm->height() + g_appInfoPtr->m_fRate*30);

			  
			m_picList->show();
        }
        ui->label_wxa_status->setText(QString::fromLocal8Bit("已上传"));
        ShowMsg(QString::fromLocal8Bit("小程序作答已更新"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_information);
    }
}

void wxAnswer::onSaveUploadedFileAcknowledgeStatus(CSaveUploadedFileAcknowledgeStatus saveUploadedFileAcknowledgeStatus)
{
    if (saveUploadedFileAcknowledgeStatus.nCode == 200)
    {
        if(m_sFileUrl == saveUploadedFileAcknowledgeStatus.sFileUrl)
        {
            if(m_sAnswerType == "SINGLE_AUDIO")
            {
                emit audioAnswered(m_sFileUrl);
            }
           /* else
            {
                emit picConfirm(m_sFileUrl);
            }*/
        }
    }
    else
    {
        if(saveUploadedFileAcknowledgeStatus.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("保存答案信息失败"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(saveUploadedFileAcknowledgeStatus.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
    }
}


void wxAnswer::setAnswer(QString sAnswer)
{
	if (!sAnswer.isEmpty())
	{		
		m_sFileUrl = sAnswer;
		QString sFileName = m_sFileUrl.right(m_sFileUrl.length() - m_sFileUrl.lastIndexOf("/") - 1);
		sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
		CHttpRequestPackage hrp;
		hrp.sUri = m_sFileUrl;
		hrp.sCommonStr = sFileName;
		hrp.sCommonStr1 = __FILE__;
		hrp.nRequestType = RequestType::rtDownLoadFile;
		hrp.nRetryCount = 3;
		g_httpBllPtr->downLoad(hrp);
	}
}

int wxAnswer::widgetType()
{
    return wt_WXAnswer;
}

void wxAnswer::onDownLoadFile(CDownLoadFileInfo downLoadFileInfo)
{
    QString sFileName = m_sFileUrl.right(m_sFileUrl.length() - m_sFileUrl.lastIndexOf("/") - 1);
    sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
    if (downLoadFileInfo.sFileName == sFileName &&
        downLoadFileInfo.sModuleName == __FILE__)
    {
        if (downLoadFileInfo.nCode == 200)
        {
            ui->label_wxa_answerTips->setText(QString::fromLocal8Bit("考生答案："));
            ui->label_wxa_answerTips->adjustSize();
            ui->label_wxa_answerTips->setGeometry(ui->label_wxa_qrcode->x(), ui->label_wxa_qrcode->y() + ui->label_wxa_qrcode->height() + g_appInfoPtr->m_fRate*20,
                                                  ui->label_wxa_answerTips->width(), ui->label_wxa_answerTips->height());
            ui->widget_etvt_voice->setVisible(true);
            ui->widget_etvt_voice->setGeometry(ui->label_wxa_answerTips->x() + ui->label_wxa_answerTips->width() + g_appInfoPtr->m_fRate*10,
                                               ui->label_wxa_answerTips->y(),
                                               g_appInfoPtr->m_fRate*423, g_appInfoPtr->m_fRate*40);
        }
        else
        {
            if(downLoadFileInfo.sMessage.isEmpty())
            {
                ShowMsg(QString::fromLocal8Bit("下载失败"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
            }
            else
            {
                ShowMsg(downLoadFileInfo.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
            }
        }
    }
}




void wxAnswer::on_btn_wxa_close_clicked()
{
	emit exit();
}

void wxAnswer::on_btn_wxa_comfirm_clicked()
{
	if (m_picList == nullptr)
	{
		emit exit();
		return;
	}
    m_sFileUrl = m_picList->getFileUrls();
    int nCount = m_sFileUrl.split(",").count();
    if(nCount > 6)
    {
        ShowMsg(QString::fromLocal8Bit("请删除多余的图片，图片总数不应该超过6张！"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        return;
    }

	emit picConfirm(m_sFileUrl);
  /*  CHttpRequestPackage hrp;
    hrp.sUri = "/api/ecs_oe_student/examControl/saveUploadedFileAcknowledgeStatus";
    hrp.nRequestType = RequestType::rtSaveUploadedFileAcknowledgeStatus;
    Json::Value jBody = Json::Value::null;
    jBody["examRecordDataId"] = g_appInfoPtr->m_oExamInfo.nExamRecordDataId;
    jBody["order"] = m_nOrder;
    jBody["filePath"] = m_sFileUrl.toStdString();
    jBody["acknowledgeStatus"] = "CONFIRMED";
    hrp.sParamList.push_back(QString("CustomBody,%1").arg(jBody.toStyledString().c_str()));
    hrp.sCommonStr = m_sFileUrl;
    hrp.eParamType = HttpParamType::hptCustomBody;
    g_httpBllPtr->post(hrp);*/
}


void wxAnswer::on_btn_etvt_play_clicked()
{
    QString sFileName = m_sFileUrl.right(m_sFileUrl.length() - m_sFileUrl.lastIndexOf("/") - 1);
    sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
    if (QFile::exists(sFileName))
    {
        m_nAudioSecord = g_audioPalyerPtr->GetMediaDuration(sFileName);
        ui->hslider_etvt_voice->setMaximum(m_nAudioSecord);
        ui->hslider_etvt_voice->setValue(0);
        g_audioPalyerPtr->setVol(m_nPlayVol);

        if (!g_audioPalyerPtr->getDeviceinfo(m_sRecordDevice))
        {
            m_pTimer->stop();
            QString sErrorStr = g_audioPalyerPtr->getErrMsg();
            ShowMsg(sErrorStr, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }

        g_audioPalyerPtr->playAudio(sFileName);
        m_pTimer->start();

    }
}

void wxAnswer::on_hslider_etvt_volume_valueChanged(int value)
{    
    m_nPlayVol = value;
    if (m_pTimer && m_pTimer->isActive())
    {
        g_audioPalyerPtr->setVol(m_nPlayVol);
    }
}

void wxAnswer::on_btn_etvt_volume_clicked()
{
    if(m_bMute)
    {
        ui->btn_etvt_volume->setStyleSheet("border-image:url(:/images/btn-etvt-volume.png);");
        ui->hslider_etvt_volume->setValue(m_nLastVol);
    }
    else
    {
        ui->btn_etvt_volume->setStyleSheet("border-image:url(:/images/btn-etvt-mute.png);");
        m_nLastVol = ui->hslider_etvt_volume->value();
        ui->hslider_etvt_volume->setValue(0);
    }
    m_bMute = !m_bMute;
}
