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

local OnCreateWorldObjectContextMenu = ISWorldObjectContextMenu.createMenu

local function PracticeFirstAid(corpse, playerObj, treatmentLevel)

	if FirstAidOverhaul.practiceCooldown > 0 then
		playerObj:Say(getText("IGUI_OnCooldown"))
		return
	end

    -- Snippet extracted from TIS code, modified.
	if corpse:getSquare() and luautils.walkAdj(playerObj, corpse:getSquare()) then
		if playerObj:getPrimaryHandItem() then
			ISTimedActionQueue.add(ISUnequipAction:new(playerObj, playerObj:getPrimaryHandItem(), 50))
		end
		if playerObj:getSecondaryHandItem() and playerObj:getSecondaryHandItem() ~= playerObj:getPrimaryHandItem() then
			ISTimedActionQueue.add(ISUnequipAction:new(playerObj, playerObj:getSecondaryHandItem(), 50))
		end
        ISTimedActionQueue.add(BB_FAO_ISTimedAction:PracticeFirstAid(corpse, playerObj, treatmentLevel))
	end
end

ISWorldObjectContextMenu.createMenu = function(player, worldobjects, x, y, test)

    local context = OnCreateWorldObjectContextMenu(player, worldobjects, x, y, test)
	local corpse = IsoObjectPicker.Instance:PickCorpse(x, y)
    if not corpse then return context end

	local playerObj = getSpecificPlayer(player)
	local doctorLevel = playerObj:getPerkLevel(Perks.Doctor)
	local playerInv = playerObj:getInventory(); if not playerInv:getItemCount("Base.CorpseMale") == 0 then return context end
	if test == true then return true end

	local practiceOption = context:addOptionOnTop(getText("ContextMenu_Practice_FirstAid"))
	local practiceContext = ISContextMenu:getNew(context)
	context:addSubMenu(practiceOption, practiceContext)

	local treatmentOption1 = practiceContext:addOptionOnTop(getText("ContextMenu_Treatment_Option_1"), corpse, PracticeFirstAid, playerObj, 1)
	if doctorLevel < 0
	or (playerInv:getCountTypeEvalRecurse("Bandage", buildUtil.predicateMaterial) == 0
	and playerInv:getCountTypeEvalRecurse("AlcoholBandage", buildUtil.predicateMaterial) == 0
	and playerInv:getCountTypeEvalRecurse("AlcoholBandage2", buildUtil.predicateMaterial) == 0) then
		treatmentOption1.notAvailable = true
		if doctorLevel < 0 then BravensUtilsFAO.addTooltip(getText("Tooltip_LowLevel") .. doctorLevel .. "/0", treatmentOption1) end
	end

	local treatmentOption2 = practiceContext:addOptionOnTop(getText("ContextMenu_Treatment_Option_2"), corpse, PracticeFirstAid, playerObj, 2)
	if doctorLevel < 3
	or playerInv:getCountTypeEvalRecurse("SutureNeedle", buildUtil.predicateMaterial) == 0 then
		treatmentOption2.notAvailable = true
		if doctorLevel < 3 then BravensUtilsFAO.addTooltip(getText("Tooltip_LowLevel") .. doctorLevel .. "/3", treatmentOption2) end
	end

	local treatmentOption3 = practiceContext:addOptionOnTop(getText("ContextMenu_Treatment_Option_3"), corpse, PracticeFirstAid, playerObj, 3)
	if doctorLevel < 5 then
		treatmentOption3.notAvailable = true
		BravensUtilsFAO.addTooltip(getText("Tooltip_LowLevel") .. doctorLevel .. "/5", treatmentOption3)
	end

	local treatmentOption4 = practiceContext:addOptionOnTop(getText("ContextMenu_Treatment_Option_4"), corpse, PracticeFirstAid, playerObj, 4)
	if doctorLevel < 7
	or playerInv:getCountTypeEvalRecurse("Scalpel", buildUtil.predicateMaterial) == 0 then
		treatmentOption4.notAvailable = true
		if doctorLevel < 7 then BravensUtilsFAO.addTooltip(getText("Tooltip_LowLevel") .. doctorLevel .. "/7", treatmentOption4) end
	end

    return context
end