#include "aboutwidget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QHBoxLayout>
#include <QPushButton>
#include "event/EventCenter.h"
#include <QStringList>
#include "json/cmdhelper.h"
#include <QThread>

AboutWidget::AboutWidget(QWidget* parent)
	: QWidget(parent)
{
	initUi();
}

AboutWidget::~AboutWidget()
{

}

void AboutWidget::initUi()
{
	const int subContentMargin = 10;
	const int subContentSpacing = 20;

	QVBoxLayout* pMainLayout;
	QHBoxLayout* pProductVerLayout;
	QLabel* pProductVer;
	QPushButton* pBtnHelp;
	QLabel* pCompanyCopyRight;
	QLabel* pGuiVer;
	QLabel* pEmbededSoftVer;
	QLabel* pReconSotfVer;
	QLabel* pFEBVer;
	QLabel* pQtVer;
	QLabel* pQtCopyright;
	//QLabel* pLogVer;
	//QLabel* pLogCopyright;
	QLabel* pJsoncppVer;
	QLabel* pJsoncppCopyright;
	QLabel* pDcmtkVer;
	QLabel* pDcmtkCopyright;

	QLabel* pSUSEVer;

	this->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
	pMainLayout = new QVBoxLayout(this);
	pMainLayout->setAlignment(Qt::AlignLeft);
	pMainLayout->setSpacing(10);

	pProductVerLayout = new QHBoxLayout(this);
	pMainLayout->addLayout(pProductVerLayout);

	pProductVer = new QLabel(this);
	pProductVer->setText(tr("HJ-USCT-01 V1.0"));
	pProductVerLayout->addWidget(pProductVer);

	pBtnHelp = new QPushButton(this);
	pBtnHelp->setText(tr("?"));
	pBtnHelp->setFixedWidth(50);
	pProductVerLayout->addWidget(pBtnHelp);

	pCompanyCopyRight = new QLabel(this);
	pCompanyCopyRight->setObjectName("normal");
	pCompanyCopyRight->setText(tr("Copyright © 2017-2022 Zhejiang Equilibrium Nine Medical Equipment Co., Ltd. All Rights Reversed"));
	pCompanyCopyRight->setContentsMargins(subContentMargin, 0, 0, 0);
	pMainLayout->addWidget(pCompanyCopyRight);

	pMainLayout->addSpacing(subContentSpacing);

	pGuiVer = new QLabel(this);
	pGuiVer->setText(tr("GUI Software V1.3"));
	pGuiVer->setContentsMargins(subContentMargin, 0, 0, 0);
	pMainLayout->addWidget(pGuiVer);

	pEmbededSoftVer = new QLabel(this);
	pEmbededSoftVer->setText(tr("Embedded Software V1.5"));
	pEmbededSoftVer->setContentsMargins(subContentMargin, 0, 0, 0);
	pMainLayout->addWidget(pEmbededSoftVer);

	pReconSotfVer = new QLabel(this);
	pReconSotfVer->setText(tr("Reconstruction Software V1.2"));
	pReconSotfVer->setContentsMargins(subContentMargin, 0, 0, 0);
	pMainLayout->addWidget(pReconSotfVer);
	pMainLayout->addSpacing(subContentSpacing);

	pFEBVer = new QLabel(this);
	pFEBVer->setText(tr("FEB Information"));
	pFEBVer->setContentsMargins(subContentMargin, 0, 0, 0);
	pMainLayout->addWidget(pFEBVer);
	pMainLayout->addSpacing(subContentSpacing);


	pQtVer = new QLabel(tr("Qt 5.12.0"), this);
	pMainLayout->addWidget(pQtVer);
	//pQtCopyright = new QLabel(tr("Copyright (c) 2017 The Qt Company Ltd."), this);
	//pQtCopyright->setObjectName("normal");
	//pQtCopyright->setContentsMargins(subContentMargin, 0, 0, 0);
	//pMainLayout->addWidget(pQtCopyright);
	pMainLayout->addSpacing(subContentSpacing);

	//pLogVer = new QLabel(this);
	//pLogVer->setText(tr("spdlog 1.8.2"));
	//pLogVer->setObjectName("lbl_aboutwidget_log4cxx");
	//pMainLayout->addWidget(pLogVer);
	//pLogCopyright = new QLabel(this);
	//pLogCopyright->setText(tr("Copyright (c) 2016 Gabi Melman."));
	//pLogCopyright->setContentsMargins(subContentMargin, 0, 0, 0);
	//pMainLayout->addWidget(pLogCopyright);




	pSUSEVer = new QLabel(this);
	pSUSEVer->setText(tr("Loading..."));
	pMainLayout->addWidget(pSUSEVer);
	pMainLayout->addSpacing(subContentSpacing);

	pDcmtkVer = new QLabel(this);
	pDcmtkVer->setText(tr("Loading..."));
	pMainLayout->addWidget(pDcmtkVer);
	pDcmtkCopyright = new QLabel(this);
	pDcmtkCopyright->setObjectName("normal");
	pDcmtkCopyright->setText(tr("Copyright (c) 1994-2021, OFFIS e.V."));
	pDcmtkCopyright->setContentsMargins(subContentMargin, 0, 0, 0);
	pMainLayout->addWidget(pDcmtkCopyright);
	pMainLayout->addSpacing(subContentSpacing);


	pJsoncppVer = new QLabel(this);
	pJsoncppVer->setText(tr("cJSON"));
	pMainLayout->addWidget(pJsoncppVer);
	pJsoncppCopyright = new QLabel(this);
	pJsoncppCopyright->setObjectName("normal");
	pJsoncppCopyright->setText(tr("Copyright (c) 2009-2017 Dave Gamble"));
	pJsoncppCopyright->setContentsMargins(subContentMargin, 0, 0, 0);
	pMainLayout->addWidget(pJsoncppCopyright);
	pMainLayout->addSpacing(subContentSpacing);

	pMainLayout->addStretch();


	QThread* t = QThread::create([=]() {
		pSUSEVer->setText(cmdHelper::Instance()->getLinuxVersion());
		pDcmtkVer->setText(cmdHelper::Instance()->getDCMTKVersion());
		});
	t->start();
	connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));


	connect(pBtnHelp, SIGNAL(clicked()), this, SLOT(openHelpFile()));
	connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() {
		pProductVer->setText(tr("HJ-USCT-01 V1.0"));
		pBtnHelp->setText(tr("?"));
		pCompanyCopyRight->setText(tr("Copyright © 2017-2020 Zhejiang Equilibrium Nine Medical Equipment Co., Ltd. All Rights Reversed"));
		pGuiVer->setText(tr("GUI Software V1.3"));
		pEmbededSoftVer->setText(tr("Embedded Software V1.5"));
		pReconSotfVer->setText(tr("Reconstruction Software V1.2"));
		pFEBVer->setText(tr("FEB Information"));
		});
}

void AboutWidget::openHelpFile()
{
	QString userManulFile = QCoreApplication::applicationDirPath() + "/userManual.pdf";

	QFileInfo file(userManulFile);
	if (file.exists())
	{
		QDesktopServices::openUrl(QUrl::fromLocalFile(userManulFile));
	}
	//else
	//{
	//	EQ9MessageBox box;
	//	box.setMessage(tr("can't find the user manual file!"));
	//	box.exec();
	//}
}
