--
-- register
--
-- @autor nilBrain
-- @date 26/01/2021
-- @node register Utils for Samson FAP
-- Copyright (C) nilBrain, Confidential, All Rights Reserved.
FapRegister = {}

local modName = g_currentModName;
local modDir = g_currentModDirectory;

FapRegister.modName = modName;
FapRegister.modDir = modDir;

local specializationRegister = function(specializationName, specializationClassName, specializationFilename, searchedSpecializations, blockSpecializations)
	local neddedSpec = 0;
    local hasForNeed = 0;
	local specializationObject = g_specializationManager:getSpecializationObjectByName(modName .. "."..specializationName);
    if specializationObject == nil then
		g_specializationManager:addSpecialization(specializationName, specializationClassName, specializationFilename, nil);
		specializationObject = g_specializationManager:getSpecializationObjectByName(modName .. "."..specializationName);
        --return;
    end;
    for typeName, vehicleType in pairs(g_vehicleTypeManager:getTypes()) do
        if searchedSpecializations ~= nil then
            neddedSpec = #searchedSpecializations;
            for _, search in pairs(searchedSpecializations) do
                local specObject = g_specializationManager:getSpecializationObjectByName(search);
                if SpecializationUtil.hasSpecialization(specObject, vehicleType.specializations) then
                    hasForNeed = hasForNeed + 1;
                end;
            end;
        end;
        if specializationObject.prerequisitesPresent(vehicleType.specializations) then
            if neddedSpec <= hasForNeed then
                g_vehicleTypeManager:addSpecialization(typeName, modName .. "." .. specializationName);
            end;
        end;
    end;
end;

local loadspecializationsFromXML = function()
    local modDesc = loadXMLFile("modDesc", modDir .. "modDesc.xml")
    local baseKey = "modDesc.registerSpecializations.specialization"
    local count = 0
    while true do
        local key = string.format("%s(%d)", baseKey, count)
        if not hasXMLProperty(modDesc, key) then
            break
        end
        local name = getXMLString(modDesc, key .. "#name")
        local className = getXMLString(modDesc, key .. "#className")
        local filename = Utils.getFilename(getXMLString(modDesc, key .. "#filename"), modDir)

        local searchedString = getXMLString(modDesc, key .. "#searchedSpecializations")
        local blockString = getXMLString(modDesc, key .. "#blockSpecializations")

        local searchedSpecializations = searchedString and string.split(searchedString, " ") or {}
        local blockSpecializations = blockString and string.split(blockString, " ") or {}

        specializationRegister(name, className, filename, searchedSpecializations, blockSpecializations)
        count = count + 1
    end
    delete(modDesc)
end

TypeManager.validateTypes = Utils.appendedFunction(TypeManager.validateTypes, function(typeManager)
    if typeManager.typeName == "vehicle" then
        loadspecializationsFromXML();
    end;
end);