local Action = require('Legacy.GameObjects.Action')
--local Bindings = require('States.ApplicationBindings')

local rng = RNG.FromTime()
local timeUntilTravelDrive = 15 -- temporary local setting

local MoveTo = Subclass("MoveTo", Action, function(self, target, range, useTravelDrive)
    self.target = target
    self.range = range
    self.useTravelDrive = useTravelDrive
end)

function MoveTo:clone()
    return MoveTo(self.target, self.range)
end

function MoveTo:getName()
    local typename = Config:getObjectInfo("object_types", self.target:getType())
    return format("MoveTo %s '%s'", typename, self.target:getName())
end

local function getTargetPos(e, target)
    local tp = target:getPos()
    local tr = target:getRadius()
    local tu = target:getUp()
    local er = e:getRadius()
    return tp - tu:muls(1.25 * tr + er)
end

function MoveTo:onUpdateActive(e, dt)
    -- Move to within the supplied range of the target object
    local tp = getTargetPos(e, self.target)

    -- Within range of the target object?
    if (e:getPos() - tp):length() <= self.range then
        -- MoveTo is complete, remove movement action from entity's Action queue
        e:popAction()
        e.travelDriveActive = false
        e.travelDriveTimer = 0

        if e == GameState.player.currentShip and GameState.player.autonavActive then
            GameState.player.autonavActive = false
        end

        return -- within range, so end flight
    end

    -- Use the "target" metaphor to store where this ship is moving to
    e:setTarget(self.target)

    if GameState.debug.instantJobs then
        local p = e:getPos()
        local dp = tp - p
        e:setPos(p + dp:normalize():scale(rng:getUniform() * min(dp:length(), dt * GameState.debug.jobSpeed)))
    else
        if self.useTravelDrive then
            if not e.travelDriveActive and e.travelDriveTimer >= timeUntilTravelDrive then
                e.travelDriveActive = true
            else
                e.travelDriveTimer = e.travelDriveTimer + dt
            end
        end

        local tf = self.target:getForward()
        local tu = self.target:getUp()
        self:flyToward(e, tp, -tf, tu)
    end
end

return MoveTo
