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

require "TimedActions/ISBaseTimedAction"

ISFertilizeAction = ISBaseTimedAction:derive("ISFertilizeAction");

function ISFertilizeAction:isValid()
	return true;
end

function ISFertilizeAction:update()
	self.item:setJobDelta(self:getJobDelta());
end

function ISFertilizeAction:start()
	self.item:setJobType(getText("ContextMenu_Fertilize"));
	self.item:setJobDelta(0.0);
end

function ISFertilizeAction:stop()
    ISBaseTimedAction.stop(self);
    self.item:setJobDelta(0.0);
end

function ISFertilizeAction:perform()
	self.item:getContainer():setDrawDirty(true);
	self.item:setJobDelta(0.0);
	if isClient() then
		local sq = self.plant:getSquare()
		local args = { x = sq:getX(), y = sq:getY(), z = sq:getZ() }
		sendClientCommand(self.character, 'farming', 'fertilize', args)
	else
		basicFarming.fertilize(self.item, self.plant)
	end
	-- MP shouldn't do this directly
	self.item:Use()
	self.character:getInventory():Remove("FertilizerEmpty")
    -- needed to remove from queue / start next.
	ISBaseTimedAction.perform(self);
end

function ISFertilizeAction:new(character, item, plant, time)
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.character = character;
	o.item = item;
    o.plant = plant;
	o.stopOnWalk = true;
	o.stopOnRun = true;
	o.maxTime = time;
	return o
end
