-- changeObjectByDistance.lua - FS22 belső Lua objektumokkal (javított, korrekciós szorzóval)

changeObjectByDistance = {}

changeObjectByDistance.MOD_NAME = g_currentModName

function changeObjectByDistance.prerequisitesPresent(specializations)
    return true
end

function changeObjectByDistance.initSpecialization()
    local schema = Vehicle.xmlSchema

    schema:register(XMLValueType.NODE_INDEX, "vehicle.changeObjectByDistance.roll#CheckNode", "Check Node")
    schema:register(XMLValueType.NODE_INDEX, "vehicle.changeObjectByDistance.roll#CheckNodeRef", "Check Node Ref")
    schema:register(XMLValueType.NODE_INDEX, "vehicle.changeObjectByDistance.roll#objectToChange", "Object to Change")
    schema:register(XMLValueType.FLOAT, "vehicle.changeObjectByDistance.roll#maxHoseLenght", "Max Hose Length")
end

function changeObjectByDistance.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onLoad", changeObjectByDistance)
    SpecializationUtil.registerEventListener(vehicleType, "onUpdate", changeObjectByDistance)
    SpecializationUtil.registerEventListener(vehicleType, "onDraw", changeObjectByDistance)
    SpecializationUtil.registerEventListener(vehicleType, "onDelete", changeObjectByDistance)
end

function changeObjectByDistance:onLoad(savegame)
    self.COBD = {}
    self.COBD.roll = {}

    local spec = self.COBD.roll
    local xmlFile = self.xmlFile

    spec.CheckNode = xmlFile:getValue("vehicle.changeObjectByDistance.roll#CheckNode", nil, self.components, self.i3dMappings)
    spec.CheckNodeRef = xmlFile:getValue("vehicle.changeObjectByDistance.roll#CheckNodeRef", nil, self.components, self.i3dMappings)
    spec.objectToChange = xmlFile:getValue("vehicle.changeObjectByDistance.roll#objectToChange", nil, self.components, self.i3dMappings)
    spec.maxHoseLenght = xmlFile:getValue("vehicle.changeObjectByDistance.roll#maxHoseLenght", 1000)

    spec.distanceCorrection = 1.251  -- Finomhangolt korrekció a modell alapján

    self.COBD.rollNodes = {}
    for i = 53, 0, -1 do
        local node = I3DUtil.indexToObject(self.components, string.format("5|0|%d", i))
        if node ~= nil then
            table.insert(self.COBD.rollNodes, node)
        else
            print(string.format("Figyelmeztetés: Nem található node: 5|0|%d", i))
        end
    end

    self.COBD.textsize = g_currentMission.hud.speedMeter.cruiseControlTextSize * 1.5

    print("changeObjectByDistance: onLoad futott!")
end

function changeObjectByDistance:onUpdate(dt)
    local spec = self.COBD.roll

    if spec.CheckNode and spec.CheckNodeRef then
        local x, y, z = localToLocal(spec.CheckNode, spec.CheckNodeRef, 0, 0, 0)
        self.COBD.distance = math.abs(z) * (spec.distanceCorrection or 1)
    else
        self.COBD.distance = 0
    end

    if spec.objectToChange ~= nil then
        local rotations = (self.COBD.distance / spec.maxHoseLenght) * 54
        local angle = math.rad(rotations * 360)
        setRotation(spec.objectToChange, 0, 0, -angle)
    end

    local stepLength = 5.5
    for i, rollNode in ipairs(self.COBD.rollNodes) do
        if rollNode ~= nil then
            local threshold = stepLength * i
            local x, y, z = getTranslation(rollNode)
            if self.COBD.distance >= threshold then
                setTranslation(rollNode, x, 4000, z)
            else
                setTranslation(rollNode, x, 0, z)
            end
        end
    end
end

function changeObjectByDistance:onDraw(isActiveForInput, isSelected)
    if self.isClient and isActiveForInput then
        local limit = self.COBD.roll.maxHoseLenght or 1000
        local distance = self.COBD.distance or 0

        if distance > limit then
            setTextColor(1, 0, 0, 1) -- piros szín
            local warningText = g_i18n:getText("ui_changeObjectByDistance_hoseLimit"):format(string.format("%.1f", limit))
            renderText(0.5, 0.05, 0.02, warningText)
        else
            setTextColor(1, 1, 0, 1) -- sárga szín
            local infoText = g_i18n:getText("ui_changeObjectByDistance_hoseLength"):format(string.format("%.1f", distance))
            renderText(0.5, 0.05, 0.02, infoText)
        end

        setTextAlignment(RenderText.ALIGN_LEFT)
    end
end

function changeObjectByDistance:onDelete()
    print("changeObjectByDistance: onDelete futott.")
end

function changeObjectByDistance:onReadStream(streamId, connection)
    print("changeObjectByDistance: onReadStream futott.")
end

function changeObjectByDistance:onWriteStream(streamId, connection)
    print("changeObjectByDistance: onWriteStream futott.")
end
