-- Dada-Modding --

AnimationPartDDM = {};

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

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

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

function AnimationPartDDM:onLoad(vehicle)	
	self.porte = AnimationPartDDM.porte
	self.patin_G = AnimationPartDDM.patin_G
	self.patin_D = AnimationPartDDM.patin_D
	self.Porte = false
	self.PatinG = false
	self.PatinD = false
	
end;

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

function AnimationPartDDM:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
	if keyStatus > 0 then
		if actionName == 'porte' then
			if self.Porte then
				self:porte(false)
			else 
				self:porte(true)
			end
		end
		if actionName == 'patin_G' then
			if self.PatinG then
				self:patin_G(false)
			else 
				self:patin_G(true)
			end
		end
		if actionName == 'patin_D' then
			if self.PatinD then
				self:patin_D(false)
			else 
				self:patin_D(true)
			end
		end
	end
end

function AnimationPartDDM:porte(Porte, noEventSend)
	porteEvent.sendEvent(self, Porte, noEventSend)
	self.Porte = Porte;
		if self.Porte then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("porte", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("porte", -1, nil, true)
			end
		end;
end;


function AnimationPartDDM:patin_G(PatinG, noEventSend)
	patin_GEvent.sendEvent(self, PatinG, noEventSend)
	self.PatinG = PatinG;
		if self.PatinG then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("patin_G", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("patin_G", -1, nil, true)
			end
		end;
end;

function AnimationPartDDM:patin_D(PatinD, noEventSend)
	patin_DEvent.sendEvent(self, PatinD, noEventSend)
	self.PatinD = PatinD;
		if self.PatinD then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("patin_D", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("patin_D", -1, nil, true)
			end
		end;
end;

--- Event ---

porteEvent = {};
porteEvent_mt = Class(porteEvent, Event);
InitEventClass(porteEvent, "porteEvent");

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

patin_GEvent = {};
patin_GEvent_mt = Class(patin_GEvent, Event);
InitEventClass(patin_GEvent, "patin_GEvent");

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


patin_DEvent = {};
patin_DEvent_mt = Class(patin_DEvent, Event);
InitEventClass(patin_DEvent, "patin_DEvent");

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