-- changeObjectByDistance.lua - FS22 debug-olt változat

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.INT, "vehicle.changeObjectByDistance.roll#maxHoseLenght", "Max Hose Length")
    schema:register(XMLValueType.INT, "vehicle.changeObjectByDistance.roll#hoseObjectCount", "Hose Object Count")
    schema:register(XMLValueType.FLOAT, "vehicle.changeObjectByDistance.roll#speedScale", "Speed Scale")
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:getInt("vehicle.changeObjectByDistance.roll#maxHoseLenght", 220)
    spec.hoseObjectCount = xmlFile:getInt("vehicle.changeObjectByDistance.roll#hoseObjectCount", 13)
    spec.speedScale = xmlFile:getFloat("vehicle.changeObjectByDistance.roll#speedScale", 1)

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

    -- Roll elemek betöltése
    for i = 1, spec.hoseObjectCount do
        self.COBD["roll" .. i] = xmlFile:getValue(string.format("vehicle.changeObjectByDistance.roll#roll%d", i), nil, self.components, self.i3dMappings)
    end

    -- Debug üzenetek
    print("changeObjectByDistance: onLoad futott!")
    print("  CheckNode:", spec.CheckNode)
    print("  CheckNodeRef:", spec.CheckNodeRef)
    print("  objectToChange:", spec.objectToChange)
    print("  maxHoseLenght:", spec.maxHoseLenght)
    print("  hoseObjectCount:", spec.hoseObjectCount)
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)
    else
        self.COBD.distance = 0
    end

    -- Debug: aktuális távolság
    if self.COBD.distance then
        print(string.format("changeObjectByDistance: Aktuális távolság = %.2f m", self.COBD.distance))
    end

    if self.COBD.distance > spec.maxHoseLenght then
        self.COBD.distance = spec.maxHoseLenght
        print("changeObjectByDistance: Elérte a maximális hosszúságot!")
    end

    -- Dob forgatása
    if spec.objectToChange ~= nil then
        local rotations = (self.COBD.distance / spec.maxHoseLenght) * spec.hoseObjectCount
        local angle = math.rad(rotations * 360)
        setRotation(spec.objectToChange, 0, 0, angle)
        print(string.format("changeObjectByDistance: Dob szög = %.2f fok", math.deg(angle)))
    end

    -- Slagdarabok megjelenítése/eltüntetése
    for i = 1, spec.hoseObjectCount do
        local rollNode = self.COBD["roll" .. i]
        if rollNode ~= nil then
            if self.COBD.distance >= (spec.maxHoseLenght / spec.hoseObjectCount) * i then
                setVisibility(rollNode, false)
            else
                setVisibility(rollNode, true)
            end
        end
    end
end

function changeObjectByDistance:onDraw(isActiveForInput, isSelected)
    if self.isClient and isActiveForInput then
        setTextColor(1, 1, 0, 1) -- Sárga szín
        setTextAlignment(RenderText.ALIGN_CENTER)
        renderText(0.5, 0.45, 0.02, string.format("Kihúzott hossz: %.1f m", self.COBD.distance or 0))
        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
