WayPointGPSSpec = {}

function WayPointGPSSpec.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Enterable, specializations)
       and SpecializationUtil.hasSpecialization(Drivable, specializations)
end

function WayPointGPSSpec.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onLoad",                 WayPointGPSSpec)
    SpecializationUtil.registerEventListener(vehicleType, "onDelete",               WayPointGPSSpec)
    SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", WayPointGPSSpec)
    SpecializationUtil.registerEventListener(vehicleType, "onUpdate",               WayPointGPSSpec)
end

function WayPointGPSSpec.registerOverwrittenFunctions(vehicleType)
end

function WayPointGPSSpec:onLoad(savegame)
    self.spec_waypointGps = {
        actionEvents = {},
        wpgpsRouteProgressIndex = nil,
        wpgpsRouteProgressSignature = nil,
        wpgpsRouteProgressDistance = nil
    }
end

function WayPointGPSSpec:onDelete()
    if self.spec_waypointGps ~= nil then
        self.spec_waypointGps.actionEvents = nil
    end
end

function WayPointGPSSpec:onUpdate(dt)
end

function WayPointGPSSpec:onRegisterActionEvents(_, isOnActiveVehicle)
    if not self.isClient then return end

    local spec = self.spec_waypointGps
    if spec == nil then return end
    spec.actionEvents = {}

    if isOnActiveVehicle then
        local ok, evId

        ok, evId = InputBinding.registerActionEvent(
            g_inputBinding, "WPGPS_TOGGLE_DISPLAY", self, WayPointGPSSpec.onActionToggleDisplay,
            false, true, false, true)
        if ok then
            spec.actionEvents.display = evId
            g_inputBinding:setActionEventTextVisibility(evId, false)
        end

        ok, evId = InputBinding.registerActionEvent(
            g_inputBinding, "WPGPS_SETTINGS", self, WayPointGPSSpec.onActionSettings,
            false, true, false, true)
        if ok then
            spec.actionEvents.settings = evId
            g_inputBinding:setActionEventTextVisibility(evId, false)
        end

        ok, evId = InputBinding.registerActionEvent(
            g_inputBinding, "WPGPS_TOGGLE_ARROW", self, WayPointGPSSpec.onActionToggleArrow,
            false, true, false, true)
        if ok then
            spec.actionEvents.arrow = evId
            g_inputBinding:setActionEventTextVisibility(evId, false)
        end

        ok, evId = InputBinding.registerActionEvent(
            g_inputBinding, "WPGPS_CYCLE_COLOR", self, WayPointGPSSpec.onActionCycleColor,
            false, true, false, true)
        if ok then
            spec.actionEvents.color = evId
            g_inputBinding:setActionEventTextVisibility(evId, false)
        end

        ok, evId = InputBinding.registerActionEvent(
            g_inputBinding, "WPGPS_CLEAR_TARGET", self, WayPointGPSSpec.onActionClearTarget,
            false, true, false, true)
        if ok then
            spec.actionEvents.clear = evId
            g_inputBinding:setActionEventTextVisibility(evId, false)
        end
    end
end


function WayPointGPSSpec.onActionToggleDisplay(self, actionName, inputValue, callbackState, isAnalog)
    if WayPointGPS ~= nil and WayPointGPS.handleInputAction ~= nil then
        WayPointGPS.handleInputAction("toggleDisplay", "vehicleAction")
    elseif WayPointGPS ~= nil then
        WayPointGPS.toggleDisplay()
    end
end

function WayPointGPSSpec.onActionSettings(self, actionName, inputValue, callbackState, isAnalog)
    if WayPointGPS ~= nil and WayPointGPS.handleInputAction ~= nil then
        WayPointGPS.handleInputAction("openSettings", "vehicleAction")
    elseif WayPointGPS ~= nil then
        if WayPointGPS.openSettingsDialog ~= nil then
            WayPointGPS.openSettingsDialog("actionEvent")
        elseif WayPointGPS.settingsDialog ~= nil and g_gui ~= nil then
            g_gui:showDialog("WayPointGPSSettingsDialog")
        end
    end
end

function WayPointGPSSpec.onActionToggleArrow(self, actionName, inputValue, callbackState, isAnalog)
    if WayPointGPS ~= nil and WayPointGPS.handleInputAction ~= nil then
        WayPointGPS.handleInputAction("toggleArrow", "vehicleAction")
    elseif WayPointGPS ~= nil and WayPointGPS.toggleArrowDisplay ~= nil then
        WayPointGPS.toggleArrowDisplay()
    end
end

function WayPointGPSSpec.onActionClearTarget(self, actionName, inputValue, callbackState, isAnalog)
    if WayPointGPS ~= nil and WayPointGPS.handleInputAction ~= nil then
        WayPointGPS.handleInputAction("clearTarget", "vehicleAction")
    elseif WayPointGPS ~= nil then
        WayPointGPS.clearTarget(true)
    end
end

function WayPointGPSSpec.onActionCycleColor(self, actionName, inputValue, callbackState, isAnalog)
    if WayPointGPS ~= nil and WayPointGPS.handleInputAction ~= nil then
        WayPointGPS.handleInputAction("cycleColor", "vehicleAction")
    elseif WayPointGPS ~= nil and WayPointGPS.cycleRouteColor ~= nil then
        WayPointGPS.cycleRouteColor()
    end
end

