HelicopterMode = {}

function HelicopterMode.prerequisitesPresent(specializations)
    return true
end

function HelicopterMode.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onLoad", HelicopterMode)
    SpecializationUtil.registerEventListener(vehicleType, "onUpdate", HelicopterMode)
end

function HelicopterMode:onLoad(savegame)
    self.heli_isFlying = false
    self.heli_hasLifted = false
    self.heli_liftForce = 150
    self.heli_gravityForce = -150
    self.heli_forwardForce = 90-- növelt előre erő
    self.heli_hoverForce = 70

    self.setHelicopterInput = HelicopterMode.setHelicopterInput

    -- input változók inicializálása
    self.heli_inputW = false
    self.heli_inputS = false
    self.heli_inputA = false
    self.heli_inputD = false
    self.heli_inputLMB = false
    self.heli_inputRMB = false
end

function HelicopterMode:setHelicopterInput(w, s, a, d, lmb, rmb)
    self.heli_inputW = w
    self.heli_inputS = s
    self.heli_inputA = a
    self.heli_inputD = d
    self.heli_inputLMB = lmb
    self.heli_inputRMB = rmb
end

function HelicopterMode:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
    if self:getIsControlled() then
        if self.isClient and self:getIsActiveForInput(true) then
            local w = Input.isKeyPressed(Input.KEY_w)
            local s = Input.isKeyPressed(Input.KEY_s)
            local a = Input.isKeyPressed(Input.KEY_a)
            local d = Input.isKeyPressed(Input.KEY_d)
            local lmb = Input.isMouseButtonPressed(Input.MOUSE_BUTTON_LEFT)
            local rmb = Input.isMouseButtonPressed(Input.MOUSE_BUTTON_RIGHT)

            if self.lastInputW ~= w or self.lastInputS ~= s or self.lastInputA ~= a or self.lastInputD ~= d
                or self.lastInputLMB ~= lmb or self.lastInputRMB ~= rmb then

                self.lastInputW = w
                self.lastInputS = s
                self.lastInputA = a
                self.lastInputD = d
                self.lastInputLMB = lmb
                self.lastInputRMB = rmb

                g_client:getServerConnection():sendEvent(HelicopterInputEvent.new(self, w, s, a, d, lmb, rmb))
            end
        end

        if self.isServer then
            local comp = self.components[1].node

            if not self.heli_inputW and not self.heli_inputS and not self.heli_inputA and not self.heli_inputD
                and not self.heli_inputLMB and not self.heli_inputRMB then
                setLinearVelocity(comp, 0, 0, 0)
                setAngularVelocity(comp, 0, 0, 0)
                return
            end

            local upForce = 0

            if self.heli_inputLMB then
                upForce = self.heli_liftForce
                self.heli_hasLifted = true
            elseif self.heli_inputRMB then
                upForce = self.heli_gravityForce
            elseif self.heli_hasLifted then
                upForce = self.heli_hoverForce
            end

            local moveForward = 0
            if self.heli_inputW then
                moveForward = self.heli_forwardForce
            elseif self.heli_inputS then
                moveForward = -self.heli_forwardForce
            end

            local time = g_currentMission.time / 1000
            local wave = math.sin(time * 1.5) * 5

            -- pontosabb mozgási irány
            local x, y, z = localDirectionToWorld(self.components[1].node, 0, 0, 1)
            local fx = x * moveForward
            local fy = upForce + wave
            local fz = z * moveForward

            addForce(comp, fx, fy, fz, 0, 0, 0, true)

            self.heli_isFlying = upForce > 0

            local yawTorque = 0
            if self.heli_inputA then
                yawTorque = 600
            elseif self.heli_inputD then
                yawTorque = -600
            end
            if yawTorque ~= 0 then
                addTorque(comp, 0, yawTorque, 0, true)
            end

            local _, avy, _ = getAngularVelocity(comp)
            setAngularVelocity(comp, 0, avy, 0)
            setLinearDamping(comp, 0.8)
            setAngularDamping(comp, 1.0)

        end
    end
end
