#include "generalform.h"

#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>
#include <QLabel>
#include <QLineEdit>
#include <QEvent>
#include "event/EventCenter.h"
#include "json/jsonobject.h"
#include "SelectDialog.h"

#include "locker.h"
#include "languageswitcher.h"

GeneralForm::GeneralForm(QWidget* parent) : QWidget(parent)
{
	layout = new QVBoxLayout(this);
	QWidget* lanHeader = new QWidget(this);
	layout->addWidget(lanHeader);

	QHBoxLayout* lanHeaderLayout = new QHBoxLayout(lanHeader);
	QLabel* lbl_lan = new QLabel(tr("Language"));
	lanHeaderLayout->addWidget(lbl_lan);

	QPushButton* btnLan = new QPushButton(lanHeader);
	lanHeaderLayout->addWidget(btnLan);
	lanHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));

	QWidget* instHeader = new QWidget(this);
	layout->addWidget(instHeader);
	QHBoxLayout* instHeaderLayout = new QHBoxLayout(instHeader);
	QLabel* lbl_ins = new QLabel(tr("Institution Name"));
	instHeaderLayout->addWidget(lbl_ins);
	QLineEdit* instName = new QLineEdit(instHeader);
	instName->setMaximumSize(QSize(300, 32768));
	instHeaderLayout->addWidget(instName);
	instHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Fixed));

	QLabel* lbl_insaddr = new QLabel(tr("Institution Addr"));
	instHeaderLayout->addWidget(lbl_insaddr);
	QLineEdit* instAddr = new QLineEdit(instHeader);
	instHeaderLayout->addWidget(instAddr);
	instAddr->setMaximumSize(QSize(300, 32768));
	instHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));

	QWidget* lockHeader = new QWidget(this);
	layout->addWidget(lockHeader);
	QHBoxLayout* lockHeaderLayout = new QHBoxLayout(lockHeader);

	QLabel* lbl_lock = new QLabel(tr("Lock Screen"));
	lockHeaderLayout->addWidget(lbl_lock);

	QLineEdit* lockTime = new QLineEdit(lockHeader);
	lockTime->setMaximumSize(QSize(300, 32768));
	lockHeaderLayout->addWidget(lockTime);

	QLabel* ss = new QLabel(tr("s"));
	lockHeaderLayout->addWidget(ss);
	lockHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));

	//...
	layout->addSpacerItem(new QSpacerItem(20, 300, QSizePolicy::Minimum, QSizePolicy::Expanding));

	//init
	btnLan->setText(JsonObject::Instance()->defaultLanguage());
	instName->setText(JsonObject::Instance()->institutionName());
	instAddr->setText(JsonObject::Instance()->institutionAddr());
	lockTime->setText(JsonObject::Instance()->lockScreenTimeout());

	//connection
	connect(instName, &QLineEdit::textChanged, [=](QString str)
		{
			JsonObject::Instance()->setInstitutionName(str);
		});

	connect(instAddr, &QLineEdit::textChanged, [=](QString str)
		{
			JsonObject::Instance()->setInstitutionAddr(str);
		});
	connect(lockTime, &QLineEdit::textChanged, [=](QString str)
		{
			//take effect
			JsonObject::Instance()->setLockScreenTimeout(str);
			Locker::Instance()->setTimer(JsonObject::Instance()->lockerCount());

		});

	connect(btnLan, &QPushButton::clicked, [=]() {
		if (!dialog) {
			dialog = new SelectDialog(this);
			dialog->setWindowModality(Qt::WindowModal);
		}
		dialog->setAvailableDates(JsonObject::Instance()->language());
		dialog->setSelectedValue(JsonObject::Instance()->defaultLanguage());
		if (dialog->exec() == QDialog::Accepted)
		{
			QString lan = dialog->getSelectedValue();

			//take effect
			JsonObject::Instance()->setDefaultLanguage(lan);
			LanguageSwitcher::Instance()->setDefaultLanguage(lan);
			btnLan->setText(JsonObject::Instance()->defaultLanguage());

		}
		});

	connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() {
		lbl_lan->setText(tr("Language"));
		lbl_ins->setText(tr("Institution Addr"));
		lbl_insaddr->setText(tr("Institution Addr"));
		lbl_lock->setText(tr("Lock Screen"));
		});
}
