#include "systemsettingform.h"
#include "ui_systemsettingform.h"

#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>
#include <QLabel>
#include <QLineEdit>
#include <QEvent>
#include <QToolButton>

#include "SelectDialog.h"
#include "components/imageswitch.h"
#include "network/networkcfgdialog.h"
#include "network/dicomcfgdialog.h"
#include "network/getadminpsw.h"

#include "json/jsonobject.h"
#include "event/EventCenter.h"
#include "device/DeviceManager.h"
#include "json/cmdhelper.h"
#include "appvals/AppGlobalValues.h"

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

	ui->lbl_size->setText(tr("Loading..."));
	ui->lbl_used->setText(tr("Loading..."));

	//style init
	//ui->btn_dicom->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); 
	ui->btn_dicom->setIcon(QIcon(":/icons/dicomsettings.png"));
	//ui->btn_dicom->setText(tr("DICOM"));
	ui->btn_dicom->setIconSize(QSize(80, 80));

	//ui->btn_network->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
	ui->btn_network->setIcon(QIcon(":/icons/networksettings.png"));
	//ui->btn_network->setText(tr("IP"));
	ui->btn_network->setIconSize(QSize(80, 80));

	ui->swt_verify->setChecked(true);
	//ui->btnFlt->setFixedWidth(200);
	//ui->btnPro->setFixedWidth(200);
	//ui->lbl_protocal->setFixedWidth(100);
	ui->lbl_verify->setFixedWidth(100);

	//data init
	ui->btnPro->setText(JsonObject::Instance()->defaultProtocal());
	ui->btnPro->setObjectName("BigBtn");
	ui->btnFlt->setText(JsonObject::Instance()->defaultFilter());
	ui->btnFlt->setObjectName("BigBtn");

	////test begin
	//flag_disksize = true;
	//m_disksize = 75.0;
	////test end
	//updateStorageSize();
	////test begin
	//flag_diskuse = true;
	//m_diskuse = 23.0;
	////test end
	//updateStorageUsed();


	diskInfoCaller = QThread::create([=]() {

		double disksize = 0;
		flag_disksize = cmdHelper::Instance()->getDiskSize(disksize);
		m_disksize = disksize;
		updateStorageSize();

		while (true)
		{
			double duse = 0;
			flag_diskuse = cmdHelper::Instance()->getDiskUsed(duse);
			m_diskuse = duse;
			updateStorageUsed();
			QMetaObject::invokeMethod(ui->batIcon, "startAnimation", Qt::QueuedConnection);
			QThread::msleep(30000);
		}
		});
	diskInfoCaller->start();

	//connection
	connect(ui->swt_verify, &ImageSwitch::clicked, [=]() {
		if (ui->swt_verify->getChecked())
		{
			////
		}
		});
	connect(ui->btn_network, &QToolButton::clicked, [=]() {
		GetAdminPsw dialog(this);
		if (dialog.exec() == QDialog::Accepted)
		{
			JsonObject::Instance()->setPassword(dialog.getPsw());
			networkCfgDialog dia(this);
			dia.setWindowModality(Qt::WindowModal);
			dia.exec();
		}
		});

	connect(ui->btn_dicom, &QToolButton::clicked, [=]() {
		dicomCfgDialog dia(this);
		dia.setWindowModality(Qt::WindowModal);
		dia.exec();
		});



	connect(ui->btnPro, &QPushButton::clicked, [=]() {
		if (!sd_protocal) {
			sd_protocal = new SelectDialog(this);
			sd_protocal->setWindowModality(Qt::WindowModal);
		}
		sd_protocal->setAvailableDates(JsonObject::Instance()->protocals());
		sd_protocal->setSelectedValue(JsonObject::Instance()->defaultProtocal());
		if (sd_protocal->exec() == QDialog::Accepted)
		{
			QString pro = sd_protocal->getSelectedValue();
			//take effect
			JsonObject::Instance()->setDefaultProtocal(pro);
			ui->btnPro->setText(JsonObject::Instance()->defaultProtocal());
		}
		});

	connect(ui->btnFlt, &QPushButton::clicked, [=]() {
		if (!sd_filter) {
			sd_filter = new SelectDialog(this);
			sd_filter->setWindowModality(Qt::WindowModal);
		}
		sd_filter->setAvailableDates(JsonObject::Instance()->worklistFilters());
		sd_filter->setSelectedValue(JsonObject::Instance()->defaultFilter());
		if (sd_filter->exec() == QDialog::Accepted)
		{
			QString flt = sd_filter->getSelectedValue();
			//take effect
			JsonObject::Instance()->setDefaultFilter(flt);
			ui->btnFlt->setText(JsonObject::Instance()->defaultFilter());
		}
		});

	connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() {
		ui->retranslateUi(this);
		ui->btnPro->setText(JsonObject::Instance()->defaultProtocal());
		ui->btnFlt->setText(JsonObject::Instance()->defaultFilter());
		ui->swt_verify->setChecked(true);
		updateStorageSize();
		updateStorageUsed();

		});
}


systemSettingForm::~systemSettingForm()
{
    diskInfoCaller->terminate();
//	diskInfoCaller->quit();
//	diskInfoCaller->wait();
    delete diskInfoCaller;
}

void systemSettingForm::updateStorageUsed()
{

	if (flag_diskuse)
	{
		ui->batIcon->setValue(m_diskuse);
		ui->lbl_used->setText(tr("used:\t%1G").arg(m_diskuse));
	}
	else
	{
		ui->lbl_used->setText(tr("Get disk used size fail!"));
	}

}
void systemSettingForm::updateStorageSize()
{
	//bool flag = AppGlobalValues::StorageFlag();
	if (flag_disksize)
	{
		//double total_size = AppGlobalValues::StorageSize();
		ui->batIcon->setMaxValue(m_disksize);
		double aValue = (m_disksize - JsonObject::Instance()->storageAlarmSize().toDouble()) / m_disksize;
		ui->batIcon->setAlarmValue(aValue);
		ui->lbl_size->setText(tr("total:\t%1G").arg(m_disksize));
	}
	else
	{
		ui->lbl_size->setText(tr("Get disk total size fail!"));
	}
}
