#include "etTimeTest.h"
#include "ui_etTimeTest.h"

#include "CAppInfo.h"

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



etTimeTest::etTimeTest(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::etTimeTest)
{
    ui->setupUi(this);
	
    setStyleSheet(g_appInfoPtr->m_sQssStr);

	bStart = true;
	
	QueryPerformanceFrequency(&m_freq);	
	QueryPerformanceCounter(&m_start_t);
	m_startTime = QDateTime::currentDateTime();
	
	m_pTimer = std::make_shared<QTimer>();
	m_pTimer->setInterval(5000);
	connect(m_pTimer.get(), &QTimer::timeout, this, [&]() {
		m_pTimer->stop();
		QueryPerformanceCounter(&m_stop_t);
		m_endTime = QDateTime::currentDateTime();
		__int64 nMSeconds = m_startTime.msecsTo(m_endTime);
		__int64 exe_time = 1e3*(m_stop_t.QuadPart - m_start_t.QuadPart) / m_freq.QuadPart;
		if (abs(nMSeconds - exe_time) < 30)
		{
            m_bTime = true;
			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);
		}
		else
		{
            m_bTime = false;

			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_pTimer->start();
}

etTimeTest::~etTimeTest()
{
    delete ui;
}

int etTimeTest::setUI(const int nLeft, const int nTop, const int nWidth)
{
    setGeometry(nLeft, nTop, nWidth, g_appInfoPtr->m_fRate*146);
    ui->tablewt_envTest->setGeometry(g_appInfoPtr->m_fRate*30, 0, width() - g_appInfoPtr->m_fRate*30*2, 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);

	{
		int i = 0;
		QDateTime currentTime = QDateTime::currentDateTime();
		QTimeZone currentZone = currentTime.timeZone();
		QString currentZoneName = currentZone.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName);//当前时间的当前时区
		const int LOCAL_TIME_ZONE = 24 - QDateTime(QDate(1970, 1, 2), QTime(0, 0, 0), Qt::LocalTime).toTime_t() / 3600;
		ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("电脑时区")));
		ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(currentZoneName));
		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));
		if(LOCAL_TIME_ZONE == 8)
		{ 
            m_bTimeZone = true;
			icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
		}
		else
		{
            m_bTimeZone = false;
			icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 4));
		}
		
		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 etTimeTest::widgetHeight()
{
    return height();
}


int etTimeTest::getCheckStatus()
{
    return (m_bTime && m_bTimeZone);
}

