-- Edward`s-Modding --

AnimationPartKRO = {};

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

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

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

function AnimationPartKRO:onLoad(vehicle)	
	self.cover_left = AnimationPartKRO.cover_left
	self.cover_right = AnimationPartKRO.cover_right
	self.CoverLEFT = false
	self.CoverRIGHT = false
	
end;

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

function AnimationPartKRO:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
	if keyStatus > 0 then
		if actionName == 'cover_left' then
			if self.CoverLEFT then
				self:cover_left(false)
			else 
				self:cover_left(true)
			end
		end
		if actionName == 'cover_right' then
			if self.CoverRIGHT then
				self:cover_right(false)
			else 
				self:cover_right(true)
			end
		end
	end
end

function AnimationPartKRO:cover_left(CoverLEFT, noEventSend)
	cover_leftEvent.sendEvent(self, CoverLEFT, noEventSend)
	self.CoverLEFT = CoverLEFT;
		if self.CoverLEFT then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("cover_left", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("cover_left", -1, nil, true)
			end
		end;
end;


function AnimationPartKRO:cover_right(CoverRIGHT, noEventSend)
	cover_rightEvent.sendEvent(self, CoverRIGHT, noEventSend)
	self.CoverRIGHT = CoverRIGHT;
		if self.CoverRIGHT then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("cover_right", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("cover_right", -1, nil, true)
			end
		end;
end;

--- Event ---

cover_leftEvent = {};
cover_leftEvent_mt = Class(cover_leftEvent, Event);
InitEventClass(cover_leftEvent, "cover_leftEvent");

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

cover_rightEvent = {};
cover_rightEvent_mt = Class(cover_rightEvent, Event);
InitEventClass(cover_rightEvent, "cover_rightEvent");

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