#include "etCameraTest.h"
#include "ui_etCameraTest.h"

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

#include "CAppInfo.h"
#include "awMsgBox.h"
#include "logproc.h"
#include "CCommonTools.h"

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

    setStyleSheet(g_appInfoPtr->m_sQssStr);



    m_nCameraOpenStatus = 2;

 //   m_cam.set(CV_CAP_PROP_FOURCC, CV_FOURCC('F', 'M', 'P', '4'));
//    int inWidth = ui->widget_etct_camera->width();
//    m_cam.set(CV_CAP_PROP_FRAME_WIDTH, inWidth);
//    int inHeight = ui->widget_etct_camera->height();
//    m_cam.set(CV_CAP_PROP_FRAME_HEIGHT, inHeight);
//    m_cam.set(CV_CAP_PROP_FPS, 5);
//	m_cam.set(CV_CAP_PROP_BRIGHTNESS, 60);//亮度
//	m_cam.set(CV_CAP_PROP_CONTRAST, 30);//对比度 40
//	m_cam.set(CV_CAP_PROP_HUE, 50);//色调 50
//	m_cam.set(CV_CAP_PROP_EXPOSURE, 60);//曝光 50 获取摄像头参数

//    m_pinitTimer = std::make_shared<QTimer>();
//    m_pinitTimer->setInterval(1000);
//    connect(m_pinitTimer.get(), &QTimer::timeout, this, [&](){
//        m_pinitTimer->stop();
//		bool bOpened = true;
//        if(!m_cam.open(0))
//        {
//            ShowMsg(QString::fromLocal8Bit("打开摄像头失败"), this, MSG_ICON_TYPE::mit_error);
//			bOpened = false;
//        }

//        m_pVideoTimer->start();

//		ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("摄像头正常启用"));
//		QWidget *widget = new QWidget;
//		QHBoxLayout *layout = new QHBoxLayout;
//		widget->setLayout(layout);
//		QLabel *icon = new QLabel;

//		if(bOpened)
//		{
//			ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("正常"));
//            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));
//		}
//		else
//		{
//			ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
//            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(0, 2, widget);
//    });

    //ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("摄像头正常启用"));

    //		if(bOpened)
    //		{
    //			ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("正常"));
    //            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));
    //		}
    //		else
    //		{
    //			ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
    //            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));
    //		}
    

    m_pVideoTimer = std::make_shared<QTimer>();
    m_pVideoTimer->setInterval(200);
	
    connect(m_pVideoTimer.get(), &QTimer::timeout, this, [&](){

		if (!m_bSetCameraStaus)
		{
			m_bSetCameraStaus = true;
			setCameraStaus(true);
		}
        cv::Mat frame;
        {
            std::scoped_lock lock(m_imageMutex);
            frame = m_nCurImage;
        }

        if(frame.empty())
        {
            qDebug()<<"widgetCameraTest frame is empty";
            return;
        }

        QImage img = CCommonTools::Mat2QImage(frame);
        ui->widget_etct_camera->setAutoFillBackground(true);
        QPalette palette;
        palette.setBrush(QPalette::Window, QBrush(img.scaled(ui->widget_etct_camera->width(), ui->widget_etct_camera->height())));
        ui->widget_etct_camera->setPalette(palette);
    });

    
}

etCameraTest::~etCameraTest()
{
    m_pVideoTimer->stop();
    g_clientVideoProcPtr->stopTest(true);

    awMsgBox::clear(this);
    delete ui;
}

void etCameraTest::setCameraStaus(bool bOpened)
{
    QWidget *widget = new QWidget;
    QHBoxLayout *layout = new QHBoxLayout;
    widget->setLayout(layout);
    QLabel *icon = new QLabel;

	ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("摄像头正常启用"));
    ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
    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));
    if(bOpened)
    {
        ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("正常"));
        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));
    }
    else
    {
        ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
        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(0, 2, widget);

}

void etCameraTest::onRenderVideoFrame(const char* userId, TRTCVideoStreamType streamType, TRTCVideoFrame* frame)
{
    if(g_clientVideoProcPtr->isCameraTest())
    {        
        __int64 nServerTime = g_appInfoPtr->serverMTime();
        if(nServerTime - m_lastFaceTime >= 100)
        {
            m_lastFaceTime = nServerTime;
            cv::Mat matImg;
            cv::cvtColor(cv::Mat(frame->height, frame->width, CV_8UC4, frame->data), matImg, CV_RGBA2RGB);
            if(matImg.empty())
            {
                qDebug()<<"widgetCameraTest frame is empty";
                return;
            }
            std::scoped_lock lock(m_imageMutex);
            m_nCurImage = matImg;
        }
    }
}

int etCameraTest::setUI(const int nLeft, const int nTop, const int nWidth)
{
    setGeometry(nLeft, nTop, nWidth, g_appInfoPtr->m_fRate*507);
    ui->widget_etct_camera->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
                                        g_appInfoPtr->m_fRate*536, g_appInfoPtr->m_fRate*300);
    ui->btn_IsNotSelf->setGeometry(ui->widget_etct_camera->x() + ui->widget_etct_camera->width() + g_appInfoPtr->m_fRate*20,
                                   ui->widget_etct_camera->y(), g_appInfoPtr->m_fRate*184, g_appInfoPtr->m_fRate*140);
    ui->btn_IsNotSelf->setIconSize(QSize(g_appInfoPtr->m_fRate*32, g_appInfoPtr->m_fRate*32));
    ui->btn_IsNotSelf->setIcon(QIcon(":/images/icon-is-not-self.png"));
    ui->btn_IsSelf->setGeometry(ui->btn_IsNotSelf->x(), ui->btn_IsNotSelf->y() + ui->btn_IsNotSelf->height() + g_appInfoPtr->m_fRate*20,
                                ui->btn_IsNotSelf->width(), ui->btn_IsNotSelf->height());
    ui->btn_IsSelf->setIconSize(QSize(g_appInfoPtr->m_fRate*32, g_appInfoPtr->m_fRate*32));
    ui->btn_IsSelf->setIcon(QIcon(":/images/icon-is-self.png"));
    ui->label_HLine->setGeometry(ui->widget_etct_camera->x(), ui->widget_etct_camera->y() + ui->widget_etct_camera->height() + g_appInfoPtr->m_fRate*30,
                                 width() - g_appInfoPtr->m_fRate*30*2, g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1);

    ui->tablewt_envTest->setGeometry(ui->widget_etct_camera->x(), ui->label_HLine->y() + ui->label_HLine->height(),
                                     width() - g_appInfoPtr->m_fRate*30*2, height() - ui->label_HLine->y() - ui->label_HLine->height());

    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);

    for(int i = 0; i < 2; i++)
    {
        ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("")));
        ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit(""))); 
    }	

	setCameraStaus(false);

	g_clientVideoProcPtr->startTest(this, true);
	m_pVideoTimer->start();
    return height();
}

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

int etCameraTest::getCheckStatus()
{
    return m_nCameraOpenStatus;
}

void etCameraTest::on_btn_IsSelf_clicked()
{
    ui->tablewt_envTest->item(1, 0)->setText(QString::fromLocal8Bit("视频显示的是电脑操作者本人"));
    ui->tablewt_envTest->item(1, 1)->setText(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(1, 2, widget);
	m_nCameraOpenStatus = 1;
}

void etCameraTest::on_btn_IsNotSelf_clicked()
{
    ui->tablewt_envTest->item(1, 0)->setText(QString::fromLocal8Bit("视频显示的是电脑操作者本人"));
    ui->tablewt_envTest->item(1, 1)->setText(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(1, 2, widget);
	m_nCameraOpenStatus = 0;
}
