---
-- CreateNodes
--
-- @author      nilBrain
-- @date        2025-01-07
--
-- Copyright (C) nilBrain | Philipp Jaeger, Confidential, All Rights Reserved.

CreateNodes = {}
CreateNodes.XML_PATH = "vehicle.createNodes.node"

local modName = g_fapModName

CreateNodes.SPEC_NAME = modName .. ".createNodes"
CreateNodes.SPEC_TABLE_NAME = "spec_" .. modName .. ".createNodes"

---Called while initializing the specialization
function CreateNodes.initSpecialization()
    local schema = Vehicle.xmlSchema
    local path = CreateNodes.XML_PATH .. "(?)"
    schema:register(XMLValueType.STRING, path .. "#name", "Name registered as i3d mapping")
    schema:register(XMLValueType.NODE_INDEX, path .. "#linkNode", "Parent node to link the new transform group to (defaults to rootNode)")
    schema:register(XMLValueType.VECTOR_TRANS, path .. "#position", "Local translation")
    schema:register(XMLValueType.VECTOR_ROT, path .. "#rotation", "Local rotation")
    schema:register(XMLValueType.VECTOR_SCALE, path .. "#scale", "Local scale")
end

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

---Checks if all prerequisite specializations are loaded
-- @param table specializations specializations
-- @return boolean hasPrerequisite true if all prerequisite specializations are loaded
function CreateNodes.prerequisitesPresent(specializations)
    return true
end

---Called on load
-- @param table savegame savegame
function CreateNodes:onLoad(savegame)
    local spec = self[CreateNodes.SPEC_TABLE_NAME]
    spec.nodes = {}

    for _, key in self.xmlFile:iterator(CreateNodes.XML_PATH) do
        local linkNode = self.xmlFile:getNode(key .. "#linkNode", nil, self.components, self.i3dMappings)
        if linkNode == nil then
            linkNode = self.rootNode
        end

        local name = self.xmlFile:getString(key .. "#name")
        if string.isNilOrWhitespace(name) then
            Logging.xmlError("Error: Node Name is nil in: %s '%s'", key, self.configFileName)
            continue
        end

        if self.i3dMappings[name] ~= nil then
            Logging.xmlError("Error: Name '%s' already exist in i3dMappings in '%s'", name, self.configFileName)
            continue
        end

        local x, y, z = self.xmlFile:getTranslation(key .. "#position")
        local rx, ry, rz = self.xmlFile:getRotation(key .. "#rotation")
        local sx, sy, sz = self.xmlFile:getScale(key .. "#scale")

        local newNode = createTransformGroup(name)
        link(linkNode, newNode)

        if x ~= nil then
            setTranslation(newNode, x, y, z)
        end

        if rx ~= nil then
            setRotation(newNode, rx, ry, rz)
        end

        if sx ~= nil then
            setScale(newNode, sx, sy, sz)
        end

        table.insert(spec.nodes, newNode)

        self.i3dMappings[name] = {
            nodeId = newNode,
            rootNode = self:getParentComponent(newNode)
        }
    end
end
