require "TimedActions/ISWashYourself"


local old_ISWashYourself_GetRequiredSoap = ISWashYourself.GetRequiredSoap
local getRequiredSoap = function(character)
	if SandboxVars.TakeABathAndShower.EnableBodyGrime then
		local unit = old_ISWashYourself_GetRequiredSoap(character)
		local bodyGrime = character:getModData().BodyGrime or 0
		if bodyGrime >= 10 then
			local requiredSoap = bodyGrime * SandboxVars.TakeABathAndShower.WashSelfRemoveGrimeRatioUseSoap * 0.001
			unit = unit + math.floor(requiredSoap)
		end
		return unit
	else
		return old_ISWashYourself_GetRequiredSoap(character)
	end
end
ISWashYourself.GetRequiredSoap = getRequiredSoap


local old_ISWashYourself_perform = ISWashYourself.perform
local washYourSelfPerform = function(self)
	if SandboxVars.TakeABathAndShower.EnableBodyGrime then
		local bodyGrime = self.character:getModData().BodyGrime or 0
		local decrease
		if bodyGrime > 0 then
			if self.useSoap then
				decrease = bodyGrime * SandboxVars.TakeABathAndShower.WashSelfRemoveGrimeRatioUseSoap * 0.01
				local totalUse = math.floor(decrease * 0.1)
				if bodyGrime >= 10 and totalUse > 0 then
					for i=1, totalUse do
						for j=1, #self.soaps do
							local soap = self.soaps[j]
							if soap and soap:getCurrentUses() > 0 then
								soap:UseAndSync()
								totalUse = totalUse - 1
							end
						end
					end
				end
			else
				decrease = bodyGrime * SandboxVars.TakeABathAndShower.WashSelfRemoveGrimeRatio * 0.01
			end
			self.character:getModData().BodyGrime = round(bodyGrime - decrease, 2)
			-- print("TABAS Body Grime: " .. bodyGrime .. " to " .. tostring(self.character:getModData().BodyGrime))
		end
	end
	old_ISWashYourself_perform(self)
end
ISWashYourself.perform = washYourSelfPerform

