--***********************************************************
--**                    ROBERT JOHNSON                     **
--***********************************************************

require "TimedActions/ISBaseTimedAction"

ISTakeWaterAction = ISBaseTimedAction:derive("ISTakeWaterAction");

function ISTakeWaterAction:isValid()
	-- If the player is very thirsty, the destination item may get drained while filling it.
	-- When drained, the item may turn into another "empty" item, removing the item we're filling
	-- from it's container.
	if self.item and not self.item:getContainer() then return false end
	return self.waterObject:hasWater()
end

function ISTakeWaterAction:update()
    if self.item ~= nil then
        self.item:setJobDelta(self:getJobDelta());
        self.item:setUsedDelta(self.startUsedDelta + (self.endUsedDelta - self.startUsedDelta) * self:getJobDelta());
    end
    if self.waterObject then
        self.character:faceThisObject(self.waterObject);
    end
end

function ISTakeWaterAction:start()
    if self.item ~= nil then
		self.item:setBeingFilled(true)
	    self.item:setJobType(getText("ContextMenu_Fill") .. self.item:getName());
	    self.item:setJobDelta(0.0);
        if instanceof(self.waterObject, "IsoThumpable") then -- play the drink sound for rain barrel
            self.character:playSound("PZ_GetWater");
--            getSoundManager():PlayWorldSoundWav("PZ_GetWater", self.character:getCurrentSquare(), 0, 2, 0.8, true);
        else
            self.character:playSound("PZ_Drinking");
--            getSoundManager():PlayWorldSoundWav("PZ_Drinking", self.character:getCurrentSquare(), 0, 2, 0.8, true);
        end
    else
        if instanceof(self.waterObject, "IsoThumpable") then -- play the drink sound for rain barrel
            self.character:playSound("PZ_DrinkingFromBottle");
--            getSoundManager():PlayWorldSoundWav("PZ_DrinkingFromBottle", self.character:getCurrentSquare(), 0, 2, 0.8, true);
        else
            self.character:playSound("PZ_DrinkingFromTap");
--            getSoundManager():PlayWorldSound("PZ_DrinkingFromTap", self.character:getCurrentSquare(), 0, 2, 0.8, true);
        end
    end
end

function ISTakeWaterAction:stop()
    local used = self:getJobDelta() * self.waterUnit
    if used >= 1 then
		if isClient() then
			local obj = self.waterObject
			local args = {x=obj:getX(), y=obj:getY(), z=obj:getZ(), units=used}
			sendClientCommand(self.character, 'object', 'takeWater', args)
		else
			self.waterObject:useWater(used)
		end
	end
    ISBaseTimedAction.stop(self);
    if self.item ~= nil then
		self.item:setBeingFilled(false)
        self.item:setJobDelta(0.0);
        if self.waterObject:getProperties():Is(IsoFlagType.taintedWater) then
            self.item:setTaintedWater(true);
        end
     end
end

function ISTakeWaterAction:perform()
    if self.item ~= nil then
		self.item:setBeingFilled(false)
        self.item:getContainer():setDrawDirty(true);
        self.item:setJobDelta(0.0);
        if self.waterObject:getProperties():Is(IsoFlagType.taintedWater) then
            self.item:setTaintedWater(true);
        end
        --Without this setUsedDelta call, the final tick never goes through.
        -- the item's UsedDelta value is set at like .99
        --This means that the option to fill that container never goes away.
--		if self.item:getUsedDelta() > 0.91 then
--			self.item:setUsedDelta(1.0);
--		end
    else
		local thirst = self.character:getStats():getThirst() - (self.waterUnit / 10)
        self.character:getStats():setThirst(math.max(thirst, 0.0));
        if self.waterObject:getProperties():Is(IsoFlagType.taintedWater) then
            self.character:getBodyDamage():setPoisonLevel(self.character:getBodyDamage():getPoisonLevel() + self.waterUnit);
        end
    end

	if isClient() then
		local obj = self.waterObject
		local args = {x=obj:getX(), y=obj:getY(), z=obj:getZ(), units=self.waterUnit}
		sendClientCommand(self.character, 'object', 'takeWater', args)
	else
		self.waterObject:useWater(self.waterUnit)
	end

    -- needed to remove from queue / start next.
	ISBaseTimedAction.perform(self);
end

function ISTakeWaterAction:new (character, item, waterUnit, waterObject, time)
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.character = character;
	o.item = item;
	o.stopOnWalk = true;
	o.stopOnRun = true;
	if item ~= nil then
		o.maxTime = math.max(6, waterUnit) * 7
		o.startUsedDelta = item:getUsedDelta()
		o.endUsedDelta = math.min(item:getUsedDelta() + waterUnit * item:getUseDelta(), 1.0)
	else
		o.maxTime = time
    end
	o.waterUnit = waterUnit;
	o.waterObject = waterObject;
	return o
end
