require "TimedActions/ISBaseTimedAction"

ISRemoveSheetRope = ISBaseTimedAction:derive("ISRemoveSheetRope");

function ISRemoveSheetRope:isValid()
    if IsoWindowFrame.isWindowFrame(self.window) then
        return IsoWindowFrame.haveSheetRope(self.window)
    end
    return self.window and self.window:haveSheetRope()
end

function ISRemoveSheetRope:update()
end

function ISRemoveSheetRope:start()
end

function ISRemoveSheetRope:stop()
    ISBaseTimedAction.stop(self);
end

function ISRemoveSheetRope:perform()
    if isClient() then
        local obj = self.window
        local index = obj:getObjectIndex()
        local args = { x=obj:getX(), y=obj:getY(), z=obj:getZ(), index=index }
        sendClientCommand(self.character, 'object', 'removeSheetRope', args)
    elseif IsoWindowFrame.isWindowFrame(self.window) then
        IsoWindowFrame.removeSheetRope(self.window, self.character)
    else
        self.window:removeSheetRope(self.character)
    end
    -- needed to remove from queue / start next.
    ISBaseTimedAction.perform(self);
end

function ISRemoveSheetRope:new(character, window)
    local o = {}
    setmetatable(o, self)
    self.__index = self
    o.character = character;
    o.stopOnWalk = true;
    o.stopOnRun = true;
    o.maxTime = 20;
    -- custom fields
    o.window = window;
    return o;
end
