#include "loginwindow.h"
#include <QVBoxLayout>
#include <QLineEdit>
#include <QToolButton>
#include <QFrame>

#include <QInputMethodEvent>
#include <QtWidgets/QLabel>
#include "guimacros.h"
#include <QCryptographicHash>
#include <src/event/EventCenter.h>
#include "db/SQLHelper.h"
#include "InputObject.h"
#include "models/User.h"
#include "log/UserOperationLog.h"
#include "json/jsonobject.h"
#include "guimessagedialog.h"

LoginWindow::LoginWindow(QWidget* parent)
	: QDialog(parent)
	, m_pLoginButton(nullptr)
	, m_pVMainLayout(nullptr)
	, m_pUserCodeFrame(nullptr)
	, m_pUserCodeLayout(nullptr)
	, m_pUserCodeEdit(nullptr)
	, m_pPassWordEdit(nullptr)
{
	initUi();
	init();
	setWindowFlags(windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
	//Dialog::Instance()->InitForm();
	m_pUserCodeEdit->setText(JsonObject::Instance()->defaultUser());


}

LoginWindow::~LoginWindow()
{
}



void LoginWindow::initUi()
{

    this->setObjectName("loginform");
	m_pVMainLayout = new QVBoxLayout(this);
	m_pVMainLayout->setContentsMargins(0, 0, 0, 0);
	m_pVMainLayout->setSpacing(0);
	m_pVMainLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding));

	QLabel* title = new QLabel(this);
	title->setObjectName("title");
	title->setText(tr("U S C T"));
	m_pVMainLayout->addWidget(title, 0, Qt::AlignCenter);
	m_pUserCodeFrame = new QFrame(this);
	m_pUserCodeFrame->setObjectName("login_frame_username");
	m_pUserCodeLayout = new QVBoxLayout(m_pUserCodeFrame);
	m_pUserCodeLayout->setSpacing(30);
	m_pUserCodeLayout->setContentsMargins(20, 60, 20, 20);
	warn = new QLabel(this);
	warn->setObjectName("warn");
	warn->setText("Login failed, username or password error!");
	warn->setVisible(false);
	m_pUserCodeLayout->addWidget(warn, 0, Qt::AlignCenter);
	m_pUserCodeEdit = new QLineEdit(m_pUserCodeFrame);
	m_pUserCodeEdit->setObjectName("combobox_UserName");

	//m_pUserCodeEdit->setLineEdit(new QLineEdit(m_pUserCodeFrame));

	m_pUserCodeEdit->setPlaceholderText(tr("Username"));
	m_pUserCodeLayout->addWidget(m_pUserCodeEdit);
	m_pVMainLayout->addWidget(m_pUserCodeFrame, 0, Qt::AlignCenter);

	m_pPassWordEdit = new QLineEdit(this);
	m_pPassWordEdit->setObjectName("edt_Password");
	m_pPassWordEdit->setEchoMode(QLineEdit::Password);
	m_pPassWordEdit->setPlaceholderText(tr("Password"));
	m_pUserCodeLayout->addWidget(m_pPassWordEdit);

	ADD_TOOL_BTN_TO_LAYOUT(login, ":/icons/login.png", m_pUserCodeLayout);
	btnlogin->setText(tr("Login"));
	m_pUserCodeLayout->removeWidget(btnlogin);
	m_pUserCodeLayout->addWidget(btnlogin, 0, Qt::AlignCenter);
	m_pLoginButton = btnlogin;
    btnlogin->setObjectName("btnlogin");
	//    m_pUserCodeLayout->addWidget(m_pLoginButton, 0, Qt::AlignCenter);
	m_pVMainLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding));
    connect(EventCenter::Default(), &EventCenter::DeviceErrorRaise, [=](QObject* parent, QObject* msg) {
        if (!this->isVisible()) return;
        //默认旧模式
        GUIMessageDialog msgDialog(this);
        msgDialog.setOpacity(1.0);
        if (msg) {
            QString* str = (QString*)msg;
            msgDialog.showMessage(*str);
        }
        else {
            msgDialog.showMessage("Something went error!");
        }
        msgDialog.stopLoading();
        msgDialog.showExitButton();
//        msgDialog.setWindowModality(Q);
        msgDialog.exec();
    });
	initUserList();
}

void LoginWindow::init()
{
	connect(m_pLoginButton, SIGNAL(clicked()), this, SLOT(doLogin()));
}

void LoginWindow::initUserList()
{
}

void LoginWindow::clearInputData()
{
	m_pUserCodeEdit->clear();
	m_pPassWordEdit->clear();
	//m_pUserCodeEdit->setFocus();
}


void LoginWindow::doLogin()
{
	QString strUserCode = m_pUserCodeEdit->text();
	QString strPassWord = m_pPassWordEdit->text();

	QString encryptedPassword = strPassWord;
	strPassWord = "123456";

	QString encryptPwd = User::getEncryptedPassword(strPassWord);
	QString sql = QString("select UserCode from Account where UserCode='%1' and Password='%2'")
		.arg(strUserCode).arg(encryptPwd);
	if (User::QueryUser(strUserCode, encryptPwd))
	{
		accept();
		LOG_USER_OPERATION(Login);
		JsonObject::Instance()->setDefaultUser(strUserCode);
	}
	else {
		warn->setVisible(true);
	}

}


