UI = {}

UI.isCheckoutOpen = false
UI.isReservationOpen = false
UI.isTrackingOverlayOpen = false
UI.isTabletOpen = false

function UI:SendConfigVars()
	SendNUIMessage({ action = "SendConfigVars", resourceName = GetCurrentResourceName() })
end

function UI:OpenDisplay(departmentData, departmentKey, districtCoverageData)
	SetNuiFocus(true, true)
	SendNUIMessage({ action = "OpenDisplay", department = departmentData, departmentKey = departmentKey, districtCoverageData = districtCoverageData })
	self.isCheckoutOpen = true
end

function UI:SetDepartmentData(departmentData)
	SendNUIMessage({ action = "SetDepartmentData", department = departmentData })
end

function UI:OpenReservation(reservationData, departmentKey, isNearStation, districtCoverageData)
	SetNuiFocus(true, true)
	SendNUIMessage({ action = "OpenReservation", reservation = reservationData, departmentKey = departmentKey, isNearStation = isNearStation, districtCoverageData = districtCoverageData })
	self.isReservationOpen = true
end

function UI:UpdateReservationStatus(status)
	SendNUIMessage({ action = "UpdateReservationStatus", status = status })
end

function UI:SetTabletState(state, department, supervisor)
	self.isTabletOpen = state
	SetNuiFocus(state, state)
	SendNUIMessage({ action = "SetTabletState", state = state, department = department, supervisor = supervisor })
end

function UI:SendTabletRecords(tabletTable)
	if self.isTabletOpen then
		SendNUIMessage({ action = "SendTabletRecords", time = string.format("%s:%s:00", GetClockHours(), GetClockMinutes()), tabletTable = tabletTable, usersName = GetPlayerName(PlayerId()) })
	end
end

function UI:SetTrackingOverlayState(state)
	SendNUIMessage({ action = "SetTrackingOverlayState", state = state })
	UI.isTrackingOverlayOpen = state
end

function UI:SetTrackingOverlayData(trackingData)
	SendNUIMessage({ action = "SetTrackingOverlayData", trackingData = trackingData })
end

function UI:SetBypassInfo(isMedia, rewardsModels)
	SendNUIMessage({ action = "SetBypassInfo", isMedia = isMedia, rewardsModels = rewardsModels })
end

function UI:CloseDisplay()
	SetNuiFocus(false, false)
	SendNUIMessage({ action = "Close" })
	
	UI.isCheckoutOpen = false
	UI.isReservationOpen = false
	
	MAIN:SetUpdateRequests(false)
end

--[[Loading overlay]]
function UI:SetLoadingState(state, message)
	SendNUIMessage({ action = "SetLoadingState", state = state, message = message })
end

--[[Dialog for errors and limit notifcations]]
function UI:SetDialogActive(header, message)
	SendNUIMessage({ action = "SetDialogActive", header = header, message = message })
end

--[[Callback for JS -> LUA to close tablet on html button]]
RegisterNUICallback('Close', function(cb)
	UI:CloseDisplay()
	if cb then
		cb('ok')
	end
end)

--[[Callback for JS -> LUA to close tablet on html button]]
RegisterNUICallback('CloseTablet', function(cb)
	MAIN:SetTabletUpdateRequests(false)
	UI:SetTabletState(false)
	if cb then
		cb('ok')
	end
end)

--[[Callback for JS -> LUA to check if vehicle is avaliable]]
RegisterNUICallback('ValidateCheckout', function(data, cb)
	MAIN:ValidateCheckout(data)
	if cb then
		cb('ok')
	end
end)

--[[Callback for JS -> LUA to toggle vehicle count updates.]]
RegisterNUICallback('SetUpdateRequests', function(state, cb)
	MAIN:SetUpdateRequests(state)
	if cb then
		cb('ok')
	end
end)

--[[Callback for JS -> LUA to check in vehicle.]]
RegisterNUICallback('CheckIn', function(cb)
	MAIN:CheckInVehicle()
	if cb then
		cb('ok')
	end
end)

--[[Respawn Vehicle.]]
RegisterNUICallback('RespawnVehicle', function(cb)
	MAIN:RespawnVehicle()
	if cb then
		cb('ok')
	end
end)

--[[Set is viewing allotment send update requests, don't open reservation.]]
RegisterNUICallback('ViewingAllotment', function(state, cb)
	UI.isCheckoutOpen = state
	MAIN:SetUpdateRequests(state)
	if cb then
		cb('ok')
	end
end)

--[[New OOS Record.]]
RegisterNUICallback('NewOOS', function(oosData, cb)
	MAIN:SubmitNewOOS(oosData)
	if cb then
		cb('ok')
	end
end)

--[[Request extension on vehicle reservation.]]
RegisterNUICallback('ExtendReservation', function(cb)
	MAIN:ExtendReservation(true)
	if cb then
		cb('ok')
	end
end)

--[[Toggles tracking state on vehicle.]]
RegisterNUICallback('ToggleTracking', function(trackingData, cb)
	local vehicleNetId = trackingData.vehicleNetId
	local state = trackingData.state
	
	MAIN:SetTracking(vehicleNetId, state)
	UI:SetTrackingOverlayState(state)
	
	if cb then
		cb('ok')
	end
end)

--[[Report vehicle as stolen.]]
RegisterNUICallback('ReportStolen', function(data, cb)
	local vehicleNetId = data.vehicleNetId
	local state = data.state
	
	MAIN:SetStolenStatus(vehicleNetId, state)
	
	if cb then
		cb('ok')
	end
end)

--On screen GTA V notification
function UI:ShowNotification(text)
	SetNotificationTextEntry('STRING')
	AddTextComponentString(text)
	DrawNotification(true, true)
end

--On screen help text (upper, left)
function UI:DisplayHelpMessage(message, displayTime)
	SetTextComponentFormat('STRING')
	AddTextEntry('vms_help', message)
	AddTextComponentSubstringTextLabel('vms_help')
	DisplayHelpTextFromStringLabel(0, false, true, displayTime*1000)
end
