-- This Source Code Form is subject to the terms of the bCDDL, v. 1.1.
-- If a copy of the bCDDL was not distributed with this
-- file, You can obtain one at http://beamng.com/bCDDL-1.1.txt

local M = {}

local hasSentMapData = false
local vehicleID = obj:getID()
local naviScreenName = "@dw800_screen"

local function onInit()
    hasSentMapData = false
    obj:queueGameEngineLua('be:getObjectByID('..vehicleID..'):createUITexture("'..naviScreenName..'", "local://local/vehicles//DWC/navi_screenA.html", 256, 128, UI_TEXTURE_USAGE_AUTOMATIC, 30)')
    obj:queueGameEngineLua("map.requestVehicleDashboardMap('"..naviScreenName.."')")
    hasSentMapData = true
end

local function onReset()
    --local destroy = 'be:getObjectByPhysicsID('..vehicleID..'):destroyUITexture("'..naviScreenName..'")'
    --obj:queueGameEngineLua(destroy)
    onInit()
end

local function updateGFX(dt)
    if hasSentMapData then
        local pos = obj:getPosition()
        local rotation = math.deg(obj:getDirection()) + 180
        local speed = (electrics.values.airspeed or 0) * 3.6
        local zoom = math.min(150 + speed * 1.5, 250)

        obj:queueGameEngineLua("be:getObjectByID("..vehicleID.."):queueJSUITexture('"..naviScreenName.."', 'map.setPosition("..pos.x..", "..pos.y..", "..rotation..")')")
        obj:queueGameEngineLua("be:getObjectByID("..vehicleID.."):queueJSUITexture('"..naviScreenName.."', 'map.setZoom("..zoom..")')")

        --obj:queueGameEngineLua("be:getObjectByPhysicsID("..vehicleID.."):queueJSUITexture('"..gaugesScreenName.."', 'updateData("..math.ceil(speed)..", "..drivetrain.gear..")')")
    end
end

-- public interface
M.onInit    = onInit
M.onReset   = onReset
M.updateGFX = updateGFX

return M