-----------------------------------
-- Spell: Cura
-- Restores hp in area of effect. Self target only
-- From what I understand, Cura's base potency is the same as Cure II's.
-- With Afflatus Misery Bonus, it can be as potent as a Curaga III
-- Modeled after our cure_ii.lua, which was modeled after the below reference
-- Shamelessly stolen from http://members.shaw.ca/pizza_steve/cure/Cure_Calculator.html
-----------------------------------
---@type TSpell
local spellObject = {}

spellObject.onMagicCastingCheck = function(caster, target, spell)
    if caster:getID() ~= target:getID() then
        return xi.msg.basic.CANNOT_PERFORM_TARG
    else
        return 0
    end
end

spellObject.onSpellCast = function(caster, target, spell)
    local divisor = 0
    local constant = 0
    local basepower = 0
    local power = 0
    local basecure = 0
    local final = 0

    local minCure = 60
    if xi.settings.main.USE_OLD_CURE_FORMULA then
        power = getCurePowerOld(caster)
        divisor = 1
        constant = 20
        if power > 170 then
                divisor = 35.6666
                constant = 87.62
        elseif power > 110 then
                divisor = 2
                constant = 47.5
        end
    else
        power = getCurePower(caster)
        if power < 70 then
            divisor = 1
            constant = 60
            basepower = 40
        elseif power < 125 then
            divisor = 5.5
            constant = 90
            basepower = 70
        elseif power < 200 then
            divisor = 7.5
            constant = 100
            basepower = 125
        elseif power < 400 then
            divisor = 10
            constant = 110
            basepower = 200
        elseif power < 700 then
            divisor = 20
            constant = 130
            basepower = 400
        else
            divisor = 999999
            constant = 145
            basepower = 0
        end
    end

    if xi.settings.main.USE_OLD_CURE_FORMULA then
        basecure = getBaseCureOld(power, divisor, constant)
    else
        basecure = getBaseCure(power, divisor, constant, basepower)
    end

    --Apply Afflatus Misery Bonus to Final Result
    if caster:hasStatusEffect(xi.effect.AFFLATUS_MISERY) then
        if caster:getID() == target:getID() then -- Let's use a local var to hold the power of Misery so the boost is applied to all targets,
            caster:setLocalVar('Misery_Power', caster:getMod(xi.mod.AFFLATUS_MISERY))
        end

        local misery = caster:getLocalVar('Misery_Power')

        --THIS IS LARELY SEMI-EDUCATED GUESSWORK. THERE IS NOT A
        --LOT OF CONCRETE INFO OUT THERE ON CURA THAT I COULD FIND

        --Not very much documentation for Cura II known at all.
        --As with Cura, the Afflatus Misery bonus can boost this spell up
        --to roughly the level of a Curaga 3. For Cura I vs Curaga II,
        --this is document at ~175HP, 15HP less than the cap of 190HP. So
        --for Cura II, i'll go with 15 less than the cap of Curaga III (390): 375
        --So with lack of available formula documentation, I'll go with that.

        basecure = basecure + misery

        if basecure > 375 then
            basecure = 375
        end

        --Afflatus Misery Mod Gets Used Up
        caster:setMod(xi.mod.AFFLATUS_MISERY, 0)
    end

    final = getCureFinal(caster, spell, basecure, minCure, false)
    final = final + (final * (target:getMod(xi.mod.CURE_POTENCY_RCVD) / 100))

    --Applying server mods
    final = final * xi.settings.main.CURE_POWER

    target:addHP(final)

    target:wakeUp()

    -- pass in fixed enmity values of 75 CE and 75 VE
    caster:updateEnmityFromCure(target, final, 75, 75)

    if target:getID() == spell:getPrimaryTargetID() then
        spell:setMsg(xi.msg.basic.MAGIC_RECOVERS_HP)
    else
        spell:setMsg(xi.msg.basic.SELF_HEAL_SECONDARY)
    end

    local mpBonusPercent = (final * caster:getMod(xi.mod.CURE2MP_PERCENT)) / 100
    if mpBonusPercent > 0 then
        caster:addMP(mpBonusPercent)
    end

    return final
end

return spellObject
