-- ************************************************************************
-- **        ██████  ██████   █████  ██    ██ ███████ ███    ██          **
-- **        ██   ██ ██   ██ ██   ██ ██    ██ ██      ████   ██          **
-- **        ██████  ██████  ███████ ██    ██ █████   ██ ██  ██          **
-- **        ██   ██ ██   ██ ██   ██  ██  ██  ██      ██  ██ ██          **
-- **        ██████  ██   ██ ██   ██   ████   ███████ ██   ████          **
-- ************************************************************************
-- ** All rights reserved. This content is protected by © Copyright law. **
-- ************************************************************************

local onReadBook_Perform = ISReadABook.perform

function ISReadABook:perform()
    local skillTrained = self.item:getSkillTrained()
    if not skillTrained or not SkillBook[skillTrained] or self.character:getAlreadyReadBook():contains(self.item:getFullType()) then
        onReadBook_Perform(self)
        return
    end

    local trainedStuff = SkillBook[skillTrained]
    local maxLevel = self.item:getMaxLevelTrained()
    DressForSuccess.UpdateBoost(trainedStuff.perk, maxLevel, self.character)
    onReadBook_Perform(self)
end

local onReadBook_Update = ISReadABook.update

function ISReadABook:update()

    local skillTrained = self.item:getSkillTrained()
    if not skillTrained or not SkillBook[skillTrained] or self.character:getAlreadyReadBook():contains(self.item:getFullType()) then
        onReadBook_Update(self)
        return
    end

    local trainedStuff = SkillBook[skillTrained]
    local maxLevel = self.item:getMaxLevelTrained()
    DressForSuccess.UpdateBoost(trainedStuff.perk, maxLevel, self.character)
    onReadBook_Update(self)
end

local function onLevelPerk(character, perk, level, levelUp)
    if levelUp then
        for _, boost in ipairs(DressForSuccess.Boosts) do
            if perk:getName() == boost.skillName then
                DressForSuccess.ResetBoost(character, boost)
            end
        end
    end
end

Events.LevelPerk.Add(onLevelPerk)