---
-- FAPAttacherHeightFix
--
--
-- @author      nilBrain
-- @date        2026-07-05
--
-- Copyright (C) nilBrain | Philipp Jaeger, Confidential, All Rights Reserved.

FAPAttacherHeightFix = {}

---Checks if all prerequisite specializations are loaded
-- @param table specializations specializations
-- @return boolean hasPrerequisite true if all prerequisite specializations are loaded
function FAPAttacherHeightFix.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Attachable, specializations)
end

---Register all events that should be called for this specialization
-- @param table vehicleType vehicle type
function FAPAttacherHeightFix.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onPostAttach", FAPAttacherHeightFix)
end

---Called after attach
-- @param table attacherVehicle vehicle this implement got attached to
-- @param integer inputJointDescIndex input attacher joint index of this implement
-- @param integer jointDescIndex attacher joint index of the attacherVehicle
function FAPAttacherHeightFix:onPostAttach(attacherVehicle, inputJointDescIndex, jointDescIndex)
    if attacherVehicle == nil or attacherVehicle.spec_attacherJoints == nil then
        return
    end

    local jointDesc = attacherVehicle:getAttacherJointByJointDescIndex(jointDescIndex)
    if jointDesc == nil then
        return
    end

    attacherVehicle:updateAttacherJointSettingsByObject(self, false, false, true)
    jointDesc.moveAlpha = jointDesc.moveDown and jointDesc.lowerAlpha or jointDesc.upperAlpha

    attacherVehicle:updateAttacherJointRotationNodes(jointDesc, jointDesc.moveAlpha)
    attacherVehicle:updateAttacherJointRotation(jointDesc, self)
end
