require "Insurgent/InsurgentTraitDefinitions"

EmotionlessTrait = CreateInsurgentTrait()
EmotionlessTrait.name = "Emotionless"
EmotionlessTrait.insurgentExclusive = true
EmotionlessTrait.mutualExclusions = {"NervesOfSteel", "CombatAnxiety", "Cowardly", "Brave"}

function EmotionlessTrait.Create()
    return TraitFactory.addTrait("Emotionless", getText("UI_trait_Emotionless"), 5, getText("UI_trait_EmotionlessDesc"), false, false)
end

local function DoEmotionOverloadCheck(_player, _emoteValue, _maxEmote, _headacheRange)
    local bodydamage = _player:getBodyDamage();
    local headacheIntensity = 0;
    local newEmoteVal = _emoteValue;
    if _emoteValue > _headacheRange then
        --If the player is given a sudden influx of emotion, become overwhelmed and given a headache
       headacheIntensity = (_emoteValue - _maxEmote) * 5
       local part = _player:getBodyDamage():getBodyParts():get(8);
       part:setAdditionalPain(headacheIntensity);
       newEmoteVal = _maxEmote * 0.8
    else
        newEmoteVal = _emoteValue - 0.05;
    end
    return newEmoteVal
end

function EmotionlessTrait.OnPlayerUpdate(player)
    if not player:HasTrait("Emotionless") then
        return
    end

    local stats = player:getStats();
    local maxEmote = 0.15
    local headacheRange = 0.75
    if stats:getStress() > maxEmote then
        stats:setStress(DoEmotionOverloadCheck(player, stats:getStress(), maxEmote, headacheRange));
    end
    if stats:getPanic() > maxEmote then
        stats:setPanic(DoEmotionOverloadCheck(player, stats:getPanic(), maxEmote, 0.9));
    end
    if player:getBodyDamage():getUnhappynessLevel() > maxEmote then
        player:getBodyDamage():setUnhappynessLevel(DoEmotionOverloadCheck(player, player:getBodyDamage():getUnhappynessLevel(), maxEmote, headacheRange))
    end
    if player:getBodyDamage():getBoredomLevel() > maxEmote then
        player:getBodyDamage():setBoredomLevel(DoEmotionOverloadCheck(player, player:getBodyDamage():getBoredomLevel(), maxEmote, headacheRange))
    end
end

function EmotionlessTrait.RegisterEvents()
    Events.OnPlayerUpdate.Add(EmotionlessTrait.OnPlayerUpdate)
end