--[[
	PushableVehicleFix
	Specialization to fix some issues for the pushable vehicle!
	
	@author:    Ifko[nator]
	@date:      09.02.2021
	@version:	1.0

	History:	v1.0 @09.02.2021 - initial implementation in FS 19
]]

PushableVehicleFix = {};

--## MotorizedFix
function PushableVehicleFix:onLoad(savegame)
	local specMotorized = self.spec_motorized
	
	specMotorized.alwaysTurnedOn = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.motorized#alwaysTurnedOn"), false);
end;

Motorized.onLoad = Utils.appendedFunction(Motorized.onLoad, PushableVehicleFix.onLoad);

function PushableVehicleFix:onRegisterActionEvents(superFunc, isActiveForInput, isActiveForInputIgnoreSelection)
	if self.isClient then
		local specMotorized = self.spec_motorized;

		self:clearActionEventsTable(specMotorized.actionEvents);

		if isActiveForInputIgnoreSelection and not specMotorized.alwaysTurnedOn then
			local _, actionEventId = self:addActionEvent(specMotorized.actionEvents, InputAction.TOGGLE_MOTOR_STATE, self, Motorized.actionEventToggleMotorState, false, true, false, true, nil);

			g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_VERY_HIGH);
			g_inputBinding:setActionEventText(actionEventId, specMotorized.turnOnText);
		end;
	end;
end;

Motorized.onRegisterActionEvents = Utils.overwrittenFunction(Motorized.onRegisterActionEvents, PushableVehicleFix.onRegisterActionEvents);

function PushableVehicleFix:onEnterVehicle(superFunc, isControlling)
	local specMotorized = self.spec_motorized;
	
	if g_currentMission.missionInfo.automaticMotorStartEnabled and self:getCanMotorRun() or specMotorized.alwaysTurnedOn then
		self:startMotor(true);
	end;

	local samples = specMotorized.samples;

	if samples.compressedAir ~= nil then
		g_soundManager:playSample(samples.compressedAir, math.random(500, 2000));
	end;
end;

Motorized.onEnterVehicle = Utils.overwrittenFunction(Motorized.onEnterVehicle, PushableVehicleFix.onEnterVehicle);