ChopperExtension = {};

function ChopperExtension.prerequisitesPresent(specializations)
	return SpecializationUtil.hasSpecialization(Combine, specializations)
end;

function ChopperExtension.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", ChopperExtension)
    SpecializationUtil.registerEventListener(vehicleType, "onLoad", ChopperExtension)
	SpecializationUtil.registerEventListener(vehicleType, "onDelete", ChopperExtension)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", ChopperExtension)
	SpecializationUtil.registerEventListener(vehicleType, "onTurnedOn", ChopperExtension)
	SpecializationUtil.registerEventListener(vehicleType, "onTurnedOff", ChopperExtension)
end

function ChopperExtension.registerOverwrittenFunctions(vehicleType)
    SpecializationUtil.registerOverwrittenFunction(vehicleType, "setIsSwathActive", ChopperExtension.setIsSwathActive)
end

function ChopperExtension.registerFunctions(vehicleType)
	SpecializationUtil.registerFunction(vehicleType, "playerTriggerChopperCheck", ChopperExtension.playerTriggerChopperCheck)
end

function ChopperExtension:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
    if self.isClient then
        local spec = self.spec_combine
        self:clearActionEventsTable(spec.actionEvents)
        if isActiveForInputIgnoreSelection and spec.swath.isAvailable and spec.chopper.isAvailable then
			if spec.actionEvents[InputAction.TOGGLE_CHOPPER] ~= nil then
				spec.actionEvents[InputAction.TOGGLE_CHOPPER] = nil
			end
        end
    end
end

function ChopperExtension:onLoad(savegame)
      local schema = Vehicle.xmlSchema
      schema:setXMLSpecializationType("ChopperExtension")

    schema:register(XMLValueType.NODE_INDEX, "vehicle.chopper#triggerNode", "TriggerNode")
	self.playerTriggerChopper = self.xmlFile:getValue("vehicle.chopper#triggerNode", nil, self.components, self.i3dMappings)
	if self.playerTriggerChopper ~= nil then
            addTrigger(self.playerTriggerChopper, "playerTriggerChopperCheck", self)
    end
	
	self.playerInChopperTrigger = false
end

function ChopperExtension:onDelete()
	   if self.playerTriggerChopper ~= nil then
        removeTrigger(self.playerTriggerChopper)
       end
end

function ChopperExtension:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
	if self.playerInChopperTrigger then
		self:raiseActive()
	end;
end

function ChopperExtension:onTurnedOn()
	if self.playerInChopperTrigger then
		local actionEvent = self.chopperActionEvents[InputAction.TOGGLE_CHOPPER]
		if actionEvent ~= nil and actionEvent.actionEventId ~= nil then
			g_inputBinding:setActionEventActive(actionEvent.actionEventId, false)
		end
	end
end

function ChopperExtension:onTurnedOff()
	if self.playerInChopperTrigger then
		local actionEvent = self.chopperActionEvents[InputAction.TOGGLE_CHOPPER]
		if actionEvent ~= nil and actionEvent.actionEventId ~= nil then
			g_inputBinding:setActionEventActive(actionEvent.actionEventId, true)
		end
	end
end

function ChopperExtension:playerTriggerChopperCheck(triggerId, otherId, onEnter, onLeave, onStay)
    if onEnter and g_currentMission.controlPlayer and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
		local spec = self.spec_combine
		self.chopperActionEvents = {};
		self:clearActionEventsTable(self.chopperActionEvents)
		if spec.swath.isAvailable and spec.chopper.isAvailable then
			local _, actionEventId = self:addActionEvent(self.chopperActionEvents, InputAction.TOGGLE_CHOPPER, self, Combine.actionEventToggleChopper, false, true, false, true, nil);
			g_inputBinding:setActionEventActive(actionEventId, not self:getIsTurnedOn())
			ChopperExtension.updateToggleStrawText(self)
		end
		self:raiseActive()
		self.playerInChopperTrigger = true
    elseif onLeave and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
		if self.chopperActionEvents[InputAction.TOGGLE_CHOPPER] ~= nil then
			self:removeActionEvent(self.chopperActionEvents, InputAction.TOGGLE_CHOPPER);
		end
		self.playerInChopperTrigger = false
    end;
end;

function ChopperExtension.updateToggleStrawText(self)
    local spec = self.spec_combine
    local actionEvent = self.chopperActionEvents[InputAction.TOGGLE_CHOPPER]
    if actionEvent ~= nil and actionEvent.actionEventId ~= nil then
        local text
        if spec.isSwathActive then
            text = g_i18n:getText("action_disableStrawSwath")
        else
            text = g_i18n:getText("action_enableStrawSwath")
        end
        g_inputBinding:setActionEventText(actionEvent.actionEventId, text)
    end
end

function ChopperExtension:setIsSwathActive(superfunc, isSwathActive, noEventSend, force)
    local spec = self.spec_combine
    if isSwathActive ~= spec.isSwathActive or force then
        CombineStrawEnableEvent.sendEvent(self, isSwathActive, noEventSend)
        spec.isSwathActive = isSwathActive
        local anim = spec.chopper.animName
        if self.playAnimation ~= nil and anim ~= nil then
            local dir = 1
            if isSwathActive then
                dir = -1
            end
            self:playAnimation(anim, dir, self:getAnimationTime(anim), true)
            if force then
                AnimatedVehicle.updateAnimationByName(self, anim, 9999999)
            end
        end
		if self.playerInChopperTrigger then
			ChopperExtension.updateToggleStrawText(self)
		end
    end
end