ScreenStateToggle = {}

function ScreenStateToggle.prerequisitesPresent(specializations)
    return true
end

function ScreenStateToggle.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onLoad", ScreenStateToggle)
    SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", ScreenStateToggle)
end

local function ScreenStateToggle_setState(self, isOn)
    local spec = self.spec_screenStateToggle
    if spec == nil then
        return
    end

    if spec.screenOnNode ~= nil and spec.screenOnNode ~= 0 then
        setVisibility(spec.screenOnNode, isOn)
    end

    if spec.screenOffNode ~= nil and spec.screenOffNode ~= 0 then
        setVisibility(spec.screenOffNode, not isOn)
    end

    spec.lastIsOn = isOn
end

function ScreenStateToggle:onLoad(savegame)
    local spec = {}
    self.spec_screenStateToggle = spec

    spec.screenOnNode = I3DUtil.indexToObject(self.components, "0>0|9|0|0|0", self.i3dMappings)
    spec.screenOffNode = I3DUtil.indexToObject(self.components, "0>0|9|0|0|12", self.i3dMappings)
    spec.lastIsOn = nil

    local isOn = false
    if self.getIsMotorStarted ~= nil then
        isOn = self:getIsMotorStarted()
    elseif self.getIsMotorRunning ~= nil then
        isOn = self:getIsMotorRunning()
    end

    ScreenStateToggle_setState(self, isOn)
end

function ScreenStateToggle:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
    local spec = self.spec_screenStateToggle
    if spec == nil then
        return
    end

    local isOn = false
    if self.getIsMotorStarted ~= nil then
        isOn = self:getIsMotorStarted()
    elseif self.getIsMotorRunning ~= nil then
        isOn = self:getIsMotorRunning()
    end

    if spec.lastIsOn ~= isOn then
        ScreenStateToggle_setState(self, isOn)
    end
end
