local connection = exports["mysql"]:getConnection()
function loginHandler(client,username, password)
   if (client) and (client == source) then
      local account = getAccount(username)
      local hashedPass = getAccountData(account, "hashed_Password")
      dbQuery(function(queryHandler)
      local result, num_affected_row = dbPoll(queryHandler, 0)
        if account ~= nil and passwordVerify(password, hashedPass, function(isVaild) if not isVaild then exports["notification-system"]:sendClientMessage("ERROR :Password is invaild!", source, 255, 0, 0) end end) and num_affected_row > 1 then
            if (logIn(source, account, hashedPass) == true ) then
	            triggerClientEvent(source, "hideLoginWindow", root)
		        setElementData(source, "account:id", result.id)
		        dbExec(connection, "UPDATE accounts SET lastlogin=? WHERE username=?", "NOW()", tostring(username))
		        exports["notification-system"]:sendClientMessage("INFO: You successfuly logged in!", source ,0, 255, 0)
	        else
	           exports["notification-system"]:sendClientMessage("ERROR: That account is already logged in!", source, 255, 0, 0) 
	        end
	    else
	        exports["notification-system"]:sendClientMessage("ERROR: Username or passwrod is invailed!", source, 255, 0, 0)
		end	
      end, connection, "SELECT * FROM accounts WHERE username = ? AND password = ?", tostring(username), tostring(hashedPass))
   end
end
function reigsterHandler(client,username, email, password)
    if (client) and  (client == source) then
       local account = getAccount(username)
	   dbQuery(function(queryHandler)
	        local result, num_affected_row = dbPoll(queryHandler, 0)
	        if account ~= nil and num_affected_row > 1 then
	            exports["notification-system"]:sendClientMessage("ERROR: That username is already exist!", source, 255, 0, 0)
	        else
	            passwordHash(password, "bcrypt", {}, function(hashedPassword)
		            local account = addAccount(username,hashedPassword)
		            setAccountData(account, "hashed_Password", hashedPassword)
		            local ip = getPlayerIP(source)
		            local serial = getPlayerSerial(source)
		            local r = getRealTime()
		            local regDate = ("%04d-%02d-%02d %02d:%02d:%02d"):format(r.year+1900, r.month+1, r.monthday, r.hour, r.minute, r.second)
		            dbExec(connection, "INSERT INTO accounts (username, password ,email, ip, serial, registerdate) VALUES(?, ?, ?, ?, ?, ?)", tostring(username), tostring(hashedPassword), tostring(email), tostring(ip), tostring(serial), tostring(regDate)) 
		            triggerClientEvent(source, "hideRegisterWindow", root)
		            exports["notification-system"]:sendClientMessage("INFO: You successfuly register!", source, 0, 255, 0)
	 	        end)
	        end
	   end,connection, "SELECT username FROM accounts WHERE username =?",tostring(username))
	end
end
addEvent("submitLogin", true)
addEvent("submitRegister", true)
addEventHandler("submitLogin", root, loginHandler)
addEventHandler("submitRegister", root, reigsterHandler)
local sw, sh = guiGetScreenSize()
showChat(false)
setPlayerHudComponentVisible("all", false)
local GUIEditor = {
    window = {},
	label = {},
	edit = {},
	memo = {},
	checkbox = {},
	button = {},
}
function createLoginWindow()
    GUIEditor.window[1] = guiCreateWindow((sw-662)/2, (sh-285)/2, 662, 285, "Login", false)
	guiWindowSetSizable(GUIEditor.window[1], false)
	guiWindowSetMovable(GUIEditor.window[1], false)
	GUIEditor.label[1] = guiCreateLabel(20, 49, 84, 19, "Username:", false, GUIEditor.window[1])
	guiSetFont(GUIEditor.label[1], "default-blod-small")
	GUIEditor.edit[1] = guiCreateEdit(94, 39, 156, 36, "", false, GUIEditor.window[1])
	guiEditSetMaxLength(GUIEditor.edit[1], 20)
	GUIEditor.label[2] = guiCreateLabel(20, 95, 84, 19, "Password:", false, GUIEditor.window[1])
	guiSetFont(GUIEditor.label[2], "default-blod-small")
	GUIEditor.edit[2] = guiCreateEdit(94, 85, 156, 36, "", false, GUIEditor.window[1])
	guiEditSetMaxLength(GUIEditor.edit[2], 50)
	guiEditSetMasked(GUIEditor.edit[2], true)
	guiSetText(GUIEditor.edit[1], tostring(loadSavedData("username", "")))
	guiSetText(GUIEditor.edit[2], tostring(loadSavedData("password", "")))
	GUIEditor.button[1] = guiCreateButton(94, 185, 156, 36, "Login", false, GUIEditor.window[1])
	GUIEditor.button[2] = guiCreateButton(94, 231, 156, 36, "Register", false, GUIEditor.window[1])
	GUIEditor.checkbox[1] = guiCreateCheckBox(94, 136, 149, 15, "Rememeber information", false, false, GUIEditor.window[1])
	GUIEditor.memo[1] = guiCreateMemo(296, 39, 351, 231, "Hey, you there!\n\nWelcome to the great experience of Countyside Roleplay.\n\nWe hope to enjoy your time here", false, GUIEditor.window[1])
	guiMemoSetReadOnly(GUIEditor.memo[1], true)
	addEventHandler("onClientGUIClick", GUIEditor.button[1], onClientSubmitLogin, false)
	addEventHandler("onClientGUIClick", GUIEditor.button[2], createRegisterWindow, false)
end
function createRegisterWindow()
    guiSetVisible(GUIEditor.window[1], false)
    GUIEditor.window[2] = guiCreateWindow((sw-311)/2, (sh-285)/2, 311, 285, "Registration", false)
	guiWindowSetMovable(GUIEditor.window[2], false)
	guiWindowSetSizable(GUIEditor.window[2], false)
	GUIEditor.label[3] = guiCreateLabel(20, 49, 84, 19, "Username:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[3], "default-blod-small")
	GUIEditor.edit[3] = guiCreateEdit(94, 39, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[3], 20)
	GUIEditor.label[4] = guiCreateLabel(20, 95, 84, 19, "Email:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[4], "default-blod-small")
	GUIEditor.edit[4] = guiCreateEdit(94, 85, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[4], 100)
	GUIEditor.label[5] = guiCreateLabel(20, 146, 84, 19, "Password:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[5], "default-blod-small")
	GUIEditor.edit[5] = guiCreateEdit(94, 136, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[5], 50)
	guiEditSetMasked(GUIEditor.edit[5], true)
	GUIEditor.button[3] = guiCreateButton(94, 185, 156, 36, "Register", false, GUIEditor.window[2])
	GUIEditor.button[4] = guiCreateButton(94, 231, 156, 36, "Back", false, GUIEditor.window[2])
	addEventHandler("onClientGUIClick", GUIEditor.button[3], onClientSubmitRegister, false)
	addEventHandler("onClientGUIClick", GUIEditor.button[4], hideRegisterWindow, false)
	guiSetVisible(GUIEditor.window[2], true)
end
function reousceStart()
    createLoginWindow()
	if GUIEditor.window[1] ~= nil then
	   guiSetVisible(GUIEditor.window[1], true)
	else
	   exports["notification-system"]:sendClientMessage("ERROR: An ERROR has been occupied!", 255, 0, 0)
	end
	showCursor(true)
	guiSetInputEnabled(true)
	guiSetInputMode("no_binds")
end	
local oldXmlFileName = "settings.xml"
local migratedSettingsFile = "@migratedsettings.empty"
local xmlFileName = "@settings.xml"
function loadSavedData(parameter, default)
	if not fileExists(migratedSettingsFile) then
		if not fileExists(xmlFileName) and fileExists(oldXmlFileName) then
			fileRename(oldXmlFileName, xmlFileName)
		end
		fileClose(fileCreate(migratedSettingsFile))
	end
	local xmlRoot = xmlLoadFile( xmlFileName )
	if (xmlRoot) then
		local xmlNode = xmlFindChild(xmlRoot, parameter, 0)
		if (xmlNode) then
			return xmlNodeGetValue(xmlNode)
		end
	end
	return default or false
end
function appendSavedData(parameter, value)
	setElementData(localPlayer, parameter, value, false)
	local xmlFile = xmlLoadFile ( xmlFileName )
	if not (xmlFile) then
		xmlFile = xmlCreateFile( xmlFileName, "login" )
	end
	
	local xmlNode = xmlFindChild (xmlFile, parameter, 0)
	if not (xmlNode) then
		xmlNode = xmlCreateChild(xmlFile, parameter)
	end
	xmlNodeSetValue ( xmlNode, value )
	xmlSaveFile(xmlFile)
	xmlUnloadFile(xmlFile)
end
function onClientSubmitLogin(button, state)
    if button == "left" and state == "up" then
	   local username = guiGetText(GUIEditor.edit[1])
	   local password = guiGetText(GUIEditor.edit[2])
	   if username ~= "" and password ~= "" then
	        if username == "" then 
		        exports["notification-system"]:sendClientMessage("ERROR:  You need enter username!" , 255, 0, 0) 
	        elseif password == "" then 
		        exports["notification-system"]:sendClientMessage("ERROR: You need  enter a password!", 255, 0, 0) 
		    end
		    if guiCheckBoxGetSelected(GUIEditor.checkbox[1]) then
	           appendSavedData("username", tostring(username))
	           appendSavedData("password", tostring(password))
	        else
	           appendSavedData("username", "")
	           appendSavedData("password", "")
		    end
	       triggerServerEvent("submitLogin", resourceRoot,  username, password)
		else
		   exports["notification-system"]:sendClientMessage("ERROR: You need enter username and password!!", 255, 0, 0)
	   end
	end
end
function onClientSubmitRegister(button, state)
    if button == "left" and state == "up" then
	   local username = guiGetText(GUIEditor.edit[3])
	   local email = guiGetText(GUIEditor.edit[4])
	   local password = guiGetText(GUIEditor.edit[5])
	   if username ~="" and email ~= "" and password ~= "" then
	        if username == "" then
		        exports["notification-system"]:sendClientMessage("ERROR: You need enter username!", 255, 0, 0)
		    elseif email == "" then
		        exports["notification-system"]:sendClientMessage("ERROR You need enter a email!", 255, 0, 0)
		    elseif password == "" then
		        exports["notification-system"]:sendClientMessage("ERROR: You need  enter a password!", 255, 0, 0) 
		    end
		    if string.len(username) < 3  then
		        exports["notification-system"]:sendClientMessage("ERROR: Username is too short you need 3 characters long!" , 255, 0, 0)
		    end
		    if string.len(password) < 6 then
		       exports["notification-system"]:sendClientMessage("ERROR: Password is too short you need 6 characters long!", 255, 0, 0)
		    end
		    if not string.find(email, "@") then 
		        exports["notification-system"]:sendClientMessage("ERROR: Invaild email!", 255, 0, 0)
		    end
	     triggerServerEvent("submitRegister", resourceRoot, username, email, password)
		else
		  exports["notification-system"]:sendClientMessage("ERROR: You need enter username, email and password!!", 255, 0, 0)
		end
	end
end
function hideLoginWindow()
    guiSetVisible(GUIEditor.window[1], false)
	showChat(true)
	setPlayerHudComponentVisible("all", true)
	guiSetInputEnabled(false)
	showCursor(false)
end
function hideRegisterWindow()
    createLoginWindow()
	guiSetVisible(GUIEditor.window[2], false)
end
addEvent("hideLoginWindow", true)
addEvent("hideRegisterWindow", true)
addEventHandler("hideLoginWindow", root, hideLoginWindow)
addEventHandler("hideRegisterWindow", root, hideRegisterWindow)
addEventHandler("onClientResourceStart", resourceRoot, reousceStart)
	Nothing work nether login or register no error or warnings