-- SurfDuck-Modding --

AnimationPartSDM = {};

function AnimationPartSDM.prerequisitesPresent(specializations)
      return true;
end;
function AnimationPartSDM.initSpecialization()
end
function AnimationPartSDM.registerOverwrittenFunctions(vehicleType)
end
function AnimationPartSDM.registerFunctions(vehicleType)	
end
function AnimationPartSDM.registerEvents(vehicleType)
end
function AnimationPartSDM:registerActionEventsPlayer()
end
function AnimationPartSDM:registerActionEventsMenu()
end

function AnimationPartSDM:onRegisterActionEvents(isSelected, isOnActiveVehicle)
	if isOnActiveVehicle then
		if self.AnimationPartSDM == nil then 
			self.AnimationPartSDM = {}
		else	
			self:clearActionEventsTable( self.AnimationPartSDM )
		end 
		for _,actionName in pairs({"ridelle_ar","ridelle_G","ridelle_D"}) do
			local _, eventName = self:addActionEvent(self.AnimationPartSDM, InputAction[actionName], self, AnimationPartSDM.actionCallback, true, true, false, true, nil);
	
			if isSelected then
				g_inputBinding.events[eventName].displayPriority = 1
			end
		end
	end
end

function AnimationPartSDM.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", AnimationPartSDM)
	SpecializationUtil.registerEventListener(vehicleType, "onDelete", AnimationPartSDM)
    SpecializationUtil.registerEventListener(vehicleType, "onReadStream", AnimationPartSDM)
    SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", AnimationPartSDM)
    --SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", AnimationPartSDM)
    --SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", AnimationPartSDM)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", AnimationPartSDM)
	SpecializationUtil.registerEventListener(vehicleType, "onDeactivate", AnimationPartSDM)
	SpecializationUtil.registerEventListener(vehicleType, "onDraw", AnimationPartSDM)
	SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", AnimationPartSDM)
end

function AnimationPartSDM:onLoad(vehicle)	
	self.ridelle_ar = AnimationPartSDM.ridelle_ar
	self.ridelle_G = AnimationPartSDM.ridelle_G
	self.ridelle_D = AnimationPartSDM.ridelle_D
	self.RidelleAR = false
	self.RidelleG = false
	self.RidelleD = false
	
end;

function AnimationPartSDM:onDelete() 
end;
function AnimationPartSDM:onReadStream(streamId, connection)   
end;  
function AnimationPartSDM:onWriteStream(streamId, connection)  
end;
function AnimationPartSDM:mouseEvent(posX, posY, isDown, isUp, button)
end;
function AnimationPartSDM:keyEvent(unicode, sym, modifier, isDown)    
end;
function AnimationPartSDM:onUpdate(dt, vehicle)		
end;
function AnimationPartSDM:onUpdateTick() 
end;	
function AnimationPartSDM:onDeactivate()	
end;       
function AnimationPartSDM:onDraw()  
end;

function AnimationPartSDM:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
	if keyStatus > 0 then
		if actionName == 'ridelle_ar' then
			if self.RidelleAR then
				self:ridelle_ar(false)
			else 
				self:ridelle_ar(true)
			end
		end
		if actionName == 'ridelle_G' then
			if self.RidelleG then
				self:ridelle_G(false)
			else 
				self:ridelle_G(true)
			end
		end
		if actionName == 'ridelle_D' then
			if self.RidelleD then
				self:ridelle_D(false)
			else 
				self:ridelle_D(true)
			end
		end
	end
end

function AnimationPartSDM:ridelle_ar(RidelleAR, noEventSend)
	ridelle_arEvent.sendEvent(self, RidelleAR, noEventSend)
	self.RidelleAR = RidelleAR;
		if self.RidelleAR then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("ridelle_ar", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("ridelle_ar", -1, nil, true)
			end
		end;
end;


function AnimationPartSDM:ridelle_G(RidelleG, noEventSend)
	ridelle_GEvent.sendEvent(self, RidelleG, noEventSend)
	self.RidelleG = RidelleG;
		if self.RidelleG then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("ridelle_G", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("ridelle_G", -1, nil, true)
			end
		end;
end;

function AnimationPartSDM:ridelle_D(RidelleD, noEventSend)
	ridelle_DEvent.sendEvent(self, RidelleD, noEventSend)
	self.RidelleD = RidelleD;
		if self.RidelleD then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("ridelle_D", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("ridelle_D", -1, nil, true)
			end
		end;
end;

--- Event ---

ridelle_arEvent = {};
ridelle_arEvent_mt = Class(ridelle_arEvent, Event);
InitEventClass(ridelle_arEvent, "ridelle_arEvent");

function ridelle_arEvent:emptyNew()
    local self = Event:new(ridelle_arEvent_mt);
    return self;
end;
function ridelle_arEvent:new(object, RidelleAR)
	local self = ridelle_arEvent:emptyNew()
	self.object = object;
	self.RidelleAR = RidelleAR;
	return self;
end;
function ridelle_arEvent:readStream(streamId, connection)
	self.object = NetworkUtil.readNodeObject(streamId);
	self.RidelleAR = streamReadBool(streamId);
	self:run(connection);
end;
function ridelle_arEvent:writeStream(streamId, connection)
	NetworkUtil.writeNodeObject(streamId, self.object);
	streamWriteBool(streamId, self.RidelleAR);
end;
function ridelle_arEvent:run(connection)
	if not connection:getIsServer() then
		g_server:broadcastEvent(self, false, connection, self.object);
	end;
	if self.object ~= nil then
		self.object:ridelle_ar(self.RidelleAR, true);
	end;
end;
function ridelle_arEvent.sendEvent(vehicle, RidelleAR, noEventSend)
	if RidelleAR ~= vehicle.RidelleAR then
		if noEventSend == nil or noEventSend == false then
			if g_server ~= nil then
				g_server:broadcastEvent(ridelle_arEvent:new(vehicle, RidelleAR), nil, nil, vehicle);
			else
				g_client:getServerConnection():sendEvent(ridelle_arEvent:new(vehicle, RidelleAR));
			end;
		end;
	end;
end;

ridelle_GEvent = {};
ridelle_GEvent_mt = Class(ridelle_GEvent, Event);
InitEventClass(ridelle_GEvent, "ridelle_GEvent");

function ridelle_GEvent:emptyNew()
    local self = Event:new(ridelle_GEvent_mt);
    return self;
end;
function ridelle_GEvent:new(object, RidelleG)
	local self = ridelle_GEvent:emptyNew()
	self.object = object;
	self.RidelleG = RidelleG;
	return self;
end;
function ridelle_GEvent:readStream(streamId, connection)
	self.object = NetworkUtil.readNodeObject(streamId);
	self.RidelleG = streamReadBool(streamId);
	self:run(connection);
end;
function ridelle_GEvent:writeStream(streamId, connection)
	NetworkUtil.writeNodeObject(streamId, self.object);
	streamWriteBool(streamId, self.RidelleG);
end;
function ridelle_GEvent:run(connection)
	if not connection:getIsServer() then
		g_server:broadcastEvent(self, false, connection, self.object);
	end;
	if self.object ~= nil then
		self.object:ridelle_G(self.RidelleG, true);
	end;
end;
function ridelle_GEvent.sendEvent(vehicle, RidelleG, noEventSend)
	if RidelleG ~= vehicle.RidelleG then
		if noEventSend == nil or noEventSend == false then
			if g_server ~= nil then
				g_server:broadcastEvent(ridelle_GEvent:new(vehicle, RidelleG), nil, nil, vehicle);
			else
				g_client:getServerConnection():sendEvent(ridelle_GEvent:new(vehicle, RidelleG));
			end;
		end;
	end;
end;


ridelle_DEvent = {};
ridelle_DEvent_mt = Class(ridelle_DEvent, Event);
InitEventClass(ridelle_DEvent, "ridelle_DEvent");

function ridelle_DEvent:emptyNew()
    local self = Event:new(ridelle_DEvent_mt);
    return self;
end;
function ridelle_DEvent:new(object, RidelleD)
	local self = ridelle_DEvent:emptyNew()
	self.object = object;
	self.RidelleD = RidelleD;
	return self;
end;
function ridelle_DEvent:readStream(streamId, connection)
	self.object = NetworkUtil.readNodeObject(streamId);
	self.RidelleD = streamReadBool(streamId);
	self:run(connection);
end;
function ridelle_DEvent:writeStream(streamId, connection)
	NetworkUtil.writeNodeObject(streamId, self.object);
	streamWriteBool(streamId, self.RidelleD);
end;
function ridelle_DEvent:run(connection)
	if not connection:getIsServer() then
		g_server:broadcastEvent(self, false, connection, self.object);
	end;
	if self.object ~= nil then
		self.object:ridelle_D(self.RidelleD, true);
	end;
end;
function ridelle_DEvent.sendEvent(vehicle, RidelleD, noEventSend)
	if RidelleD ~= vehicle.RidelleD then
		if noEventSend == nil or noEventSend == false then
			if g_server ~= nil then
				g_server:broadcastEvent(ridelle_DEvent:new(vehicle, RidelleD), nil, nil, vehicle);
			else
				g_client:getServerConnection():sendEvent(ridelle_DEvent:new(vehicle, RidelleD));
			end;
		end;
	end;
end;