#include "etMobileTest.h"
#include "ui_etMobileTest.h"

#include <QScrollBar>
#include <QHBoxLayout>
#include <QLabel>

#include "CAppInfo.h"
#include "CQREncode.h"
#include "awMsgBox.h"

etMobileTest::etMobileTest(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::etMobileTest)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

    m_nUploadStatus = 2;


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

	if (g_websocketPtr == nullptr)
	{
		g_websocketPtr = std::make_shared<CWebsocketProc>();
	}
	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, &etMobileTest::onMobileStatus);
	connect(g_websocketPtr.get(), &CWebsocketProc::fileAnswer, this, &etMobileTest::onFileAnswer);

	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.nExamId;
	jBody["order"] = 1;
	jBody["transferFileType"] = "AUDIO";
	jBody["testEnv"] = true;	
	hrp.sParamList.push_back(QString("CustomBody,%1").arg(jBody.toStyledString().c_str()));
	hrp.eParamType = HttpParamType::hptCustomBody;
	g_httpBllPtr->post(hrp);

}

etMobileTest::~etMobileTest()
{
    awMsgBox::clear(this);
	g_websocketPtr->close();
    delete ui;
}

int etMobileTest::setUI(const int nLeft, const int nTop, const int nWidth)
{
    setGeometry(nLeft, nTop, nWidth, g_appInfoPtr->m_fRate*427);
    ui->label_etmv_qrcode->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
                                        g_appInfoPtr->m_fRate*220, g_appInfoPtr->m_fRate*220);
    ui->label_etmt_tips->setGeometry(ui->label_etmv_qrcode->x() + ui->label_etmv_qrcode->width() + g_appInfoPtr->m_fRate*30,
                                     ui->label_etmv_qrcode->y(), g_appInfoPtr->m_fRate*490, g_appInfoPtr->m_fRate*66);
    ui->label_etmt_tips->setText(QString::fromLocal8Bit(R"(<html>
                                         <body>
                                         <style type="text/css">
                                         #normalText {
                                           color: #717171;
                                           font-size:%1px;
                                           font-family:Microsoft YaHei;
                                           text-align: left;
                                         }
                                         #hoverText {
                                           color: #13BA8C;
                                           font-size:%2px;
                                           font-family:Microsoft YaHei;
                                           text-align: left;
                                         }
                                        </style>

                                        <span id="normalText">请使用</span>
                                        <span id="hoverText">微信</span>
                                        <span id="normalText">扫描二维码后，在微信小程序上录音，并上传文件</span>
                                         </body>
                                         </html>)").arg((int)(g_appInfoPtr->m_fRate*24)).arg((int)(g_appInfoPtr->m_fRate*24)));
    ui->label_HLine->setGeometry(ui->label_etmt_tips->x(), ui->label_etmt_tips->y() + ui->label_etmt_tips->height() + g_appInfoPtr->m_fRate*20,
                                 g_appInfoPtr->m_fRate*490, g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1);
    ui->label_etmt_status->adjustSize();
    ui->label_etmt_status->setGeometry(ui->label_etmt_tips->x(), ui->label_HLine->y() + ui->label_HLine->height() + 20,
                                       g_appInfoPtr->m_fRate*490, ui->label_etmt_status->height());
    ui->btn_etmt_canotScan->setGeometry(ui->label_etmt_tips->x(), ui->label_etmv_qrcode->y() + ui->label_etmv_qrcode->height() - g_appInfoPtr->m_fRate*40,
                                        g_appInfoPtr->m_fRate*240, g_appInfoPtr->m_fRate*40);
    ui->btn_etmt_canotScan->setIconSize(QSize(g_appInfoPtr->m_fRate*13, g_appInfoPtr->m_fRate*13));
    ui->btn_etmt_canotScan->setIcon(QIcon(":/images/btn-can-not-play"));
    ui->btn_etmt_uploadFaild->setGeometry(ui->btn_etmt_canotScan->x() + ui->btn_etmt_canotScan->width() + g_appInfoPtr->m_fRate*10,
                                          ui->btn_etmt_canotScan->y(), ui->btn_etmt_canotScan->width(), ui->btn_etmt_canotScan->height());
    ui->btn_etmt_uploadFaild->setIconSize(QSize(g_appInfoPtr->m_fRate*13, g_appInfoPtr->m_fRate*13));
    ui->btn_etmt_uploadFaild->setIcon(QIcon(":/images/btn-can-not-play"));
    ui->tablewt_envTest->setGeometry(ui->label_etmv_qrcode->x(), ui->label_etmv_qrcode->y() + ui->label_etmv_qrcode->height() + g_appInfoPtr->m_fRate*30,
                                     width() - g_appInfoPtr->m_fRate*30*2, height() - ui->label_etmv_qrcode->y() - ui->label_etmv_qrcode->height() - g_appInfoPtr->m_fRate*30);

    ui->tablewt_envTest->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //整行选中的方式
    ui->tablewt_envTest->setSelectionBehavior(QAbstractItemView::SelectRows);
    //设置为只能选中一行
    ui->tablewt_envTest->setSelectionMode(QAbstractItemView::SingleSelection);
    //隐藏列表头
    ui->tablewt_envTest->verticalHeader()->setVisible(false);
    //隐藏边框
    ui->tablewt_envTest->setShowGrid(false);
    //表头不高亮显示
    ui->tablewt_envTest->horizontalHeader()->setHighlightSections(false);
    //设置行数
    ui->tablewt_envTest->setRowCount(2);
    //设置列数
    ui->tablewt_envTest->setColumnCount(3);

    ui->tablewt_envTest->setHorizontalScrollMode(QTableWidget::ScrollPerPixel);
    ui->tablewt_envTest->horizontalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate*5);

    QStringList sHeadStr;
    sHeadStr << QString::fromLocal8Bit("检测项") <<
                QString::fromLocal8Bit("值") <<
                QString::fromLocal8Bit("状态");

    ui->tablewt_envTest->setHorizontalHeaderLabels(sHeadStr);
    ui->tablewt_envTest->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    ui->tablewt_envTest->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);

    ui->tablewt_envTest->verticalHeader()->setDefaultSectionSize(g_appInfoPtr->m_fRate*48);
    ui->tablewt_envTest->setColumnWidth(0, g_appInfoPtr->m_fRate*525);
    ui->tablewt_envTest->setColumnWidth(1, g_appInfoPtr->m_fRate*170);
    ui->tablewt_envTest->setColumnWidth(2, g_appInfoPtr->m_fRate*24);
    ui->tablewt_envTest->horizontalHeader()->setStretchLastSection(true);


    {
		int i = 0;
        ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("扫描二维码")));
        ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("")));
       /* QWidget *widget = new QWidget;
        QHBoxLayout *layout = new QHBoxLayout;
        widget->setLayout(layout);
        QLabel *icon = new QLabel;
        icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
        icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
        layout->addWidget(icon);
        layout->addStretch();
        ui->tablewt_envTest->setCellWidget(i, 2, widget);*/
    }

	{
		int i = 1;
		ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("上传录音")));
		ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("")));
		/*QWidget *widget = new QWidget;
		QHBoxLayout *layout = new QHBoxLayout;
		widget->setLayout(layout);
		QLabel *icon = new QLabel;
		icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
		icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
		layout->addWidget(icon);
		layout->addStretch();
		ui->tablewt_envTest->setCellWidget(i, 2, widget);*/
	}

    return height();
}

int etMobileTest::widgetHeight()
{
    return height();
}

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


void etMobileTest::onMobileStatus(__int64 nExamRecordDataId, int nOrder, QString sScanStatus)
{
	if (sScanStatus == "SCANNED")
	{
		int i = 0;
		ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("扫描二维码")));
		ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("正常")));
		QWidget *widget = new QWidget;
		QHBoxLayout *layout = new QHBoxLayout;
		widget->setLayout(layout);
		QLabel *icon = new QLabel;
		icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
		icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
		layout->addWidget(icon);
		layout->addStretch();
        ui->tablewt_envTest->setCellWidget(i, 2, widget);

        ui->label_etmt_status->setText(QString::fromLocal8Bit("已扫码"));
	}
}

void etMobileTest::onFileAnswer(__int64 nExamRecordDataId, int nOrder, QString sFileType, QString sFileUrl)
{
	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.eParamType = HttpParamType::hptCustomBody;
	g_httpBllPtr->post(hrp);
}

void etMobileTest::onSaveUploadedFileAcknowledgeStatus(CSaveUploadedFileAcknowledgeStatus saveUploadedFileAcknowledgeStatus)
{
	if (saveUploadedFileAcknowledgeStatus.nCode == 200)
	{
		int i = 1;
		ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("上传录音")));
		ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("正常")));
		QWidget *widget = new QWidget;
		QHBoxLayout *layout = new QHBoxLayout;
		widget->setLayout(layout);
		QLabel *icon = new QLabel;
		icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
		icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
		layout->addWidget(icon);
		layout->addStretch();
		ui->tablewt_envTest->setCellWidget(i, 2, widget);
        m_nUploadStatus = 1;
        ui->label_etmt_status->setText(QString::fromLocal8Bit("已上传"));
        ShowMsg(QString::fromLocal8Bit("小程序作答已更新"), this, MSG_ICON_TYPE::mit_information);
	}
	else
	{
		int i = 1;
		ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("上传录音")));
		ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("出错")));
		QWidget *widget = new QWidget;
		QHBoxLayout *layout = new QHBoxLayout;
		widget->setLayout(layout);
		QLabel *icon = new QLabel;
		icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
		icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
		layout->addWidget(icon);
		layout->addStretch();
		ui->tablewt_envTest->setCellWidget(i, 2, widget);
        ui->label_etmt_status->setText(QString::fromLocal8Bit("上传出错"));
	}
}

void etMobileTest::on_btn_etmt_canotScan_clicked()
{
    int i = 0;
    ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("扫描二维码")));
    ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("出错")));
    QWidget *widget = new QWidget;
    QHBoxLayout *layout = new QHBoxLayout;
    widget->setLayout(layout);
    QLabel *icon = new QLabel;
    icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
    icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
    layout->addWidget(icon);
    layout->addStretch();
    ui->tablewt_envTest->setCellWidget(i, 2, widget);
    m_nUploadStatus = 0;
}

void etMobileTest::on_btn_etmt_uploadFaild_clicked()
{
    int i = 1;
    ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("上传录音")));
    ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("出错")));
    QWidget *widget = new QWidget;
    QHBoxLayout *layout = new QHBoxLayout;
    widget->setLayout(layout);
    QLabel *icon = new QLabel;
    icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
    icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
    layout->addWidget(icon);
    layout->addStretch();
    ui->tablewt_envTest->setCellWidget(i, 2, widget);
    m_nUploadStatus = 0;
}


int etMobileTest::getCheckStatus()
{
    return m_nUploadStatus;
}
