-- **************************************************
-- ██████  ██████   █████  ██    ██ ███████ ███    ██ 
-- ██   ██ ██   ██ ██   ██ ██    ██ ██      ████   ██ 
-- ██████  ██████  ███████ ██    ██ █████   ██ ██  ██ 
-- ██   ██ ██   ██ ██   ██  ██  ██  ██      ██  ██ ██ 
-- ██████  ██   ██ ██   ██   ████   ███████ ██   ████
-- **************************************************

require "TimedActions/ISBaseTimedAction"

BB_FAO_ISTimedAction = ISBaseTimedAction:derive("BB_FAO_ISTimedAction")

BB_FAO_ISTimedAction.isValid = function(self)

    if self.typeTimeAction == "PracticeFirstAid" and FirstAidOverhaul.practiceCooldown > 0 then
        return false
    end

    return true
end

BB_FAO_ISTimedAction.update = function(self)

end

BB_FAO_ISTimedAction.start = function(self)

    if self.typeTimeAction == "PracticeFirstAid" then
        self:setActionAnim("Loot")
        self:setAnimVariable("LootPosition", "Low")
        BravensUtilsFAO.TryPlaySoundClip(self.character, "PracticeGore")
    end

    if self.typeTimeAction == "InjectSyringe" then
        self:setActionAnim(CharacterActionAnims.Bandage)
        self:setAnimVariable("BandageType", "LeftArm")
        BravensUtilsFAO.TryPlaySoundClip(self.character, "InjectSyringe")
    end
end

BB_FAO_ISTimedAction.stop = function(self)
    ISBaseTimedAction.stop(self)

    if self.typeTimeAction == "PracticeFirstAid" then
        BravensUtilsFAO.TryStopSoundClip(self.character, "PracticeGore")
    end

    if self.typeTimeAction == "InjectSyringe" then
        BravensUtilsFAO.TryStopSoundClip(self.character, "InjectSyringe")
    end
end

BB_FAO_ISTimedAction.perform = function(self)

    if self.typeTimeAction == "PracticeFirstAid" then

        BravensUtilsFAO.TirePlayer(self.character, 0.15)
        BravensUtilsFAO.TryStopSoundClip(self.character, "PracticeGore")

        local playerInv = self.playerObj:getInventory()
        if self.treatmentLevel == 1 then
            local item = playerInv:getItemFromType("Base.Bandage")
            if not item then item = playerInv:getItemFromType("Base.AlcoholBandage") end
            if item then playerInv:Remove(item) end
            self.playerObj:getXp():AddXP(Perks.Doctor, SandboxVars.FirstAidOverhaul.BandagePracticeXP)

        elseif self.treatmentLevel == 2 then
            local item = playerInv:getItemFromType("Base.SutureNeedle")
            if item then playerInv:Remove(item) end
            self.playerObj:getXp():AddXP(Perks.Doctor, SandboxVars.FirstAidOverhaul.SuturePracticeXP)

        elseif self.treatmentLevel == 3 then
            self.playerObj:getXp():AddXP(Perks.Doctor, SandboxVars.FirstAidOverhaul.StudyPracticeXP)

        elseif self.treatmentLevel == 4 then
            local item = playerInv:getItemFromType("Base.Scalpel")
            if item then item:setCondition(item:getCondition() - 1) end
            self.playerObj:getXp():AddXP(Perks.Doctor, SandboxVars.FirstAidOverhaul.ScalpelPracticeXP)
        end

        FirstAidOverhaul.practiceCooldown = SandboxVars.FirstAidOverhaul.PracticeCooldown
    end

    if self.typeTimeAction == "InjectSyringe" then
        BravensUtilsFAO.TryStopSoundClip(self.character, "InjectSyringe")
        local playerInv = self.playerObj:getInventory()
        local injectionType = self.syringeItem:getType()
        local item = playerInv:getItemFromType(injectionType)
        if item then playerInv:Remove(item) end

        local stats = self.playerObj:getStats(); if not stats then return end
        local bodyDamage = self.playerObj:getBodyDamage(); if not bodyDamage then return end

        if injectionType == "AdrenalineSyringe" then
            stats:setFatigue(stats:getFatigue() - 0.7)
            stats:setEndurance(stats:getEndurance() + 0.85)
            stats:setHunger(stats:getHunger() + 0.3)
            stats:setThirst(stats:getThirst() + 0.5)
            stats:setPanic(stats:getPanic() + 15)

            local playerHp = self.playerObj:getHealth()
            if playerHp < 1 then
                self.playerObj:setHealth(self.playerObj:getHealth() + 0.2)
            end
        end

        if injectionType == "Syringe1" then
            stats:setSickness(0)
            bodyDamage:setFoodSicknessLevel(0)
            bodyDamage:setInfected(false)
            bodyDamage:setInfectionLevel(0)

            local bodyParts = bodyDamage:getBodyParts()
            for i=0,BodyPartType.ToIndex(BodyPartType.MAX) - 1 do
                local bodyPart = bodyParts:get(i)

                if bodyPart:isInfectedWound() then
                    bodyPart:setInfectedWound(false)
                end
            end
        end

        playerInv:AddItem("Base.EmptySyringe")
    end

    ISBaseTimedAction.perform(self)
end

BB_FAO_ISTimedAction.PracticeFirstAid = function(self, corpse, playerObj, treatmentLevel)

    local action = ISBaseTimedAction.new(self, playerObj)
    action.typeTimeAction = "PracticeFirstAid"
    action.corpse = corpse
    action.playerObj = playerObj
    action.treatmentLevel = treatmentLevel
    action.stopOnWalk = true
    action.stopOnRun = true
    action.maxTime = treatmentLevel * 150
    action.fromHotbar = false

    if action.character:isTimedActionInstant() then action.maxTime = 1 end
    return action
end

BB_FAO_ISTimedAction.InjectSyringe = function(self, playerObj, syringeItem)

    local action = ISBaseTimedAction.new(self, playerObj)
    action.typeTimeAction = "InjectSyringe"
    action.playerObj = playerObj
    action.syringeItem = syringeItem
    action.stopOnWalk = true
    action.stopOnRun = true
    action.maxTime = 150
    action.fromHotbar = false

    if action.character:isTimedActionInstant() then action.maxTime = 1 end
    return action
end

return TimeAction