-----------------------------------------------------------------------------------------------------------------------------------------
-- VRP
-----------------------------------------------------------------------------------------------------------------------------------------
local Tunnel = module("vrp", "lib/Tunnel")
-----------------------------------------------------------------------------------------------------------------------------------------
-- CONNECTION
-----------------------------------------------------------------------------------------------------------------------------------------
Hensa = {}
Tunnel.bindInterface("lockpick", Hensa)
-----------------------------------------------------------------------------------------------------------------------------------------
-- VARIABLES
-----------------------------------------------------------------------------------------------------------------------------------------
local Promise = nil
local DefaultPinAmount = 4
local DefaultDifficulty = 2
-----------------------------------------------------------------------------------------------------------------------------------------
-- LOCKPICKSTRENGTH
-----------------------------------------------------------------------------------------------------------------------------------------
local LockpickStrength = {
	["lockpick"] = 0.6,
	["lockpick2"] = 0.3
}
-----------------------------------------------------------------------------------------------------------------------------------------
-- CLOSE
-----------------------------------------------------------------------------------------------------------------------------------------
RegisterNUICallback("close", function()
	SetNuiFocus(false, false)

	if Promise then
		Promise:resolve(false)
	end
end)
-----------------------------------------------------------------------------------------------------------------------------------------
-- SUCCEED
-----------------------------------------------------------------------------------------------------------------------------------------
RegisterNUICallback("succeed", function()
	SetNuiFocus(false, false)
	Promise:resolve(true)
end)
-----------------------------------------------------------------------------------------------------------------------------------------
-- FAILED
-----------------------------------------------------------------------------------------------------------------------------------------
RegisterNUICallback("failed", function()
	SetNuiFocus(false, false)
	Promise:resolve(false)
end)
-----------------------------------------------------------------------------------------------------------------------------------------
-- LOCK
-----------------------------------------------------------------------------------------------------------------------------------------
function Hensa.Lock(Item, Difficulty, Pins)
	TriggerEvent("Notify", "amarelo", "Para tentativa pressione <b>D</b>.", "Atenção", 5000)

	if type(Item) ~= "number" then
		Item = LockpickStrength[Item]
	end

	if not Difficulty or type(Difficulty) ~= "number" then
		Difficulty = DefaultDifficulty
	elseif Difficulty > 7 then
		Difficulty = 7
	end

	if not Pins or type(Pins) ~= "number" then
		Pins = DefaultPinAmount
	elseif Pins > 9 then
		Pins = 9
	end

	SendNUIMessage({ action = "startLockpick", data = { strength = Item, difficulty = Difficulty, pins = Pins } })
	SetNuiFocus(true, true)

	Promise = promise.new()

	local result = Citizen.Await(Promise)
	return result
end