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

require "TimedActions/ISBaseTimedAction"

ISPlaceTrap = ISBaseTimedAction:derive("ISPlaceTrap");

function ISPlaceTrap:isValid()
	return self.character:getInventory():contains(self.weapon);
end

function ISPlaceTrap:update()
	self.weapon:setJobDelta(self:getJobDelta());
end

function ISPlaceTrap:start()
	self.weapon:setJobType(getText("ContextMenu_TrapPlace", self.weapon:getName()));
	self.weapon:setJobDelta(0.0);
end

function ISPlaceTrap:stop()
	self.weapon:setJobDelta(0.0);
    ISBaseTimedAction.stop(self);
end

function ISPlaceTrap:perform()
	self.weapon:setJobDelta(0.0);

    local trap = IsoTrap.new(self.weapon, self.square:getCell(), self.square);
    self.square:AddTileObject(trap);
    if isClient() then
        self.square:syncIsoTrap(self.weapon, self.weapon:getSensorRange() > 0, false);
    end

	if self.weapon == self.character:getPrimaryHandItem() then
		self.character:setPrimaryHandItem(nil)
	end
	if self.weapon == self.character:getSecondaryHandItem() then
		self.character:setSecondaryHandItem(nil)
	end
    self.character:getInventory():Remove(self.weapon);
    -- needed to remove from queue / start next.
	ISBaseTimedAction.perform(self);
end

function ISPlaceTrap:new(character, weapon, time)
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.character = character;
    o.square = character:getCurrentSquare();
	o.weapon = weapon;
	o.stopOnWalk = true;
	o.stopOnRun = true;
	o.maxTime = time;
	return o;
end
