-- sd modding , titi --

AnimationPart = {};

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

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

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

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

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

function AnimationPart:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
	if keyStatus > 0 then
		if actionName == 'col' then
			if self.RidelleAR then
				self:col(false)
			else 
				self:col(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 AnimationPart:col(RidelleAR, noEventSend)
	colEvent.sendEvent(self, RidelleAR, noEventSend)
	self.RidelleAR = RidelleAR;
		if self.RidelleAR then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("col", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("col", -1, nil, true)
			end
		end;
end;



--- Event ---

colEvent = {};
colEvent_mt = Class(colEvent, Event);
InitEventClass(colEvent, "colEvent");

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


