require "NPCs/MainCreationMethods"
require "Insurgent/InsurgentTraitDefinitions"

local function createTraits()
    for _, traitDef in ipairs(InsurgentTraitDefinitions.traits) do
        traitDef.Create()
        if traitDef.insurgentExclusive and InsurgentTraitDefinitions.restrictTraitsToProfession then
            ProfessionExclusiveTrait:new(traitDef.name, {"insurgent"})
        end
    end
end

local function setMutualExclusions()
    local existingTraits = {} -- So we don't try to set mutual exclusions for an add-on trait that is not present
    for _, traitDef in ipairs(InsurgentTraitDefinitions.traits) do
        existingTraits[traitDef.name] = true
    end

    for _, traitDef in ipairs(InsurgentTraitDefinitions.traits) do
        if traitDef.mutualExclusions then
            for _, exclusion in ipairs(traitDef.mutualExclusions) do
                if existingTraits[exclusion] then
                    TraitFactory.setMutualExclusive(traitDef.name, exclusion);
                end
            end
        end
    end
end

local function initInsurgentTraits()
    createTraits()
    setMutualExclusions()
end

local function initInsurgentProfs()
    local insurgent = ProfessionFactory.addProfession("insurgent", getText("UI_prof_insurgent"), "profession_insurgent", 12);
    insurgent:addFreeTrait("HighlyTrained");
    insurgent:addFreeTrait("Desensitized");	
    insurgent:addXPBoost(Perks.Aiming, 6);
    insurgent:addXPBoost(Perks.Reloading, 6);
    insurgent:addXPBoost(Perks.Fitness, 2);	
    insurgent:addXPBoost(Perks.Strength, 2);

	local profsList = ProfessionFactory.getProfessions()
    for i = 1, profsList:size() do
        local profession = profsList:get(i - 1);
        BaseGameCharacterDetails.SetProfessionDescription(profession);
    end
end

local original_traits = BaseGameCharacterDetails.DoTraits
BaseGameCharacterDetails.DoTraits = function()
    original_traits()
    initInsurgentTraits()
end

local original_profs = BaseGameCharacterDetails.DoProfessions
BaseGameCharacterDetails.DoProfessions = function()
    original_profs()
    initInsurgentProfs()
end

Events.OnGameBoot.Remove(original_profs);
Events.OnGameBoot.Remove(original_traits);
Events.OnGameBoot.Add(BaseGameCharacterDetails.DoTraits);
Events.OnGameBoot.Add(BaseGameCharacterDetails.DoProfessions);
