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

local function getBoostOption(itemSkill)
    local boostOptionName = string.gsub(itemSkill, " ", "") .. "Boost"
    return SandboxVars.DressForSuccess[boostOptionName] or SandboxVars.DressForSuccess2[boostOptionName]
end

local function getMultiplierOption(itemSkill)
    local multiplierOptionName = string.gsub(itemSkill, " ", "") .. "Multiplier"
    return SandboxVars.DressForSuccess[multiplierOptionName] or SandboxVars.DressForSuccess2[multiplierOptionName]
end

DressForSuccess.TryModifyBoost = function(playerObj, boost)
    if getBoostOption(boost.skillName) then
        local playerXP = playerObj:getXp()
        local perk = PerkFactory.getPerkFromName(boost.skillName)
        local sandboxMultiplier = getMultiplierOption(boost.skillName)
        local newMultiplier = boost.base + (boost.count * sandboxMultiplier)
        playerXP:addXpMultiplier(perk, newMultiplier, 0, boost.maxLevel)
    end
end

local function everyMinute()
    local playerObj = getPlayer()
    if not playerObj then return end

    for _, boost in ipairs(DressForSuccess.Boosts) do
        boost.count = 0
    end

    for _, boostItem in ipairs(DressForSuccess.ItemList) do
        local wornItems = playerObj:getWornItems()
        for i = 0, wornItems:size() - 1 do
            local item = wornItems:get(i):getItem()
            if item:getFullType() == boostItem.itemType then
                for _, itemSkill in ipairs(boostItem.itemSkills) do
                    for _, boost in ipairs(DressForSuccess.Boosts) do
                        if boost.skillName == itemSkill then
                            boost.count = boost.count + 1
                        end
                    end
                end
            end
        end
    end

    for _, boost in ipairs(DressForSuccess.Boosts) do
        DressForSuccess.TryModifyBoost(playerObj, boost)
    end
end

DressForSuccess.ResetBoost = function(playerObj, boost)
    if getBoostOption(boost.skillName) then
        Events.EveryOneMinute.Remove(everyMinute)

        local playerXP = playerObj:getXp()
        local perk = PerkFactory.getPerkFromName(boost.skillName)
        local newMultiplier = boost.base

        if boost.maxLevel > playerObj:getPerkLevel(perk) then
            playerXP:addXpMultiplier(perk, newMultiplier, 0, boost.maxLevel)
        else
            playerXP:addXpMultiplier(perk, 0, 0, boost.maxLevel)
            boost.base = 0
            boost.maxLevel = playerObj:getPerkLevel(perk) + 1
        end

        Events.EveryOneMinute.Add(everyMinute)
    end
end

DressForSuccess.UpdateBoost = function(perk, maxLevel, playerObj)
    local playerXP = playerObj:getXp()
    local perkName = perk:getName()
    for _, boost in ipairs(DressForSuccess.Boosts) do
        if perkName == boost.skillName then
            local sandboxMultiplier = getMultiplierOption(boost.skillName)
            local multiplier = playerXP:getMultiplier(perk) - (boost.count * sandboxMultiplier)
            if multiplier then
                boost.base = multiplier
                boost.maxLevel = maxLevel
            end
        end
    end
end

local onLoadCharacter = function()
    local playerObj = getPlayer()
    if not playerObj then return end

    if playerObj:getHoursSurvived() >= 1 then
        local playerXP = playerObj:getXp()
        for _, boost in ipairs(DressForSuccess.Boosts) do

            local boostCount = 0
            for _, boostItem in ipairs(DressForSuccess.ItemList) do
                local wornItems = playerObj:getWornItems()
                for i = 0, wornItems:size() - 1 do
                    local item = wornItems:get(i):getItem()
                    if item:getFullType() == boostItem.itemType then
                        for _, itemSkill in ipairs(boostItem.itemSkills) do
                            if itemSkill == boost.skillName then
                                boostCount = boostCount + 1
                            end
                        end
                    end
                end
            end

            local perk = PerkFactory.getPerkFromName(boost.skillName)
            local sandboxMultiplier = getMultiplierOption(boost.skillName)
            local multiplier = playerXP:getMultiplier(perk) - (boostCount * sandboxMultiplier)
            if multiplier then
                boost.base = multiplier
            end
        end
    end

    Events.EveryOneMinute.Add(everyMinute)
end

Events.OnCreatePlayer.Add(onLoadCharacter)