--***********************************************************
--**                    THE INDIE STONE                    **
--***********************************************************

require "TimedActions/ISBaseTimedAction"

ISInsertLightSourceFuelAction = ISBaseTimedAction:derive("ISInsertLightSourceFuelAction");

function ISInsertLightSourceFuelAction:isValid()
	return self.character:getInventory():contains(self.fuel)
end

function ISInsertLightSourceFuelAction:start()
	self.fuel:setJobType("Insert")
	self.fuel:setJobDelta(0.0)
end

function ISInsertLightSourceFuelAction:update()
	self.fuel:setJobDelta(self:getJobDelta())
end

function ISInsertLightSourceFuelAction:stop()
	self.fuel:setJobDelta(0.0)
	ISBaseTimedAction.stop(self)
end

function ISInsertLightSourceFuelAction:perform()
	self.fuel:setJobDelta(0.0)
	if isClient() then
		local sq = self.lightSource:getSquare()
		local args = { x = sq:getX(), y = sq:getY(), z = sq:getZ() }
		args.fuel = self.fuel:getFullType()
		args.usedDelta = self.fuel:getUsedDelta()
		for i=0,self.character:getInventory():getItems():size()-1 do
			if self.character:getInventory():getItems():get(i) == self.fuel then
				args.item = i
				break
			end
		end
		sendClientCommand(self.character, 'object', 'insertFuel', args)
	else
		self.lightSource:insertNewFuel(self.fuel, self.character)
	end
	-- needed to remove from queue / start next.
	ISBaseTimedAction.perform(self)
end

function ISInsertLightSourceFuelAction:new(character, lightSource, fuel, time)
	local o = ISBaseTimedAction.new(self, character)
	o.stopOnWalk = true
	o.stopOnRun = true
	o.maxTime = time
	o.lightSource = lightSource
	o.fuel = fuel
	return o
end
