-- Fredzaza --

Coloutil = {};
Coloutil.modDirectory = g_currentModDirectory;

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

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

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

function Coloutil:onLoad(vehicle)	
	self.Sangle = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.sangle#index"));
	self.Lock = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.Lock#index"));
	self.Unlock = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.Unlock#index"));
	
	self.animcol = Coloutil.animcol
	self.collision = false
	if self.Sangle ~= nil then
		setVisibility(self.Lock, false);
		setVisibility(self.Unlock, false);
		setVisibility(self.Sangle, false);
	end;
end;

function Coloutil:onDelete() 
end;

function Coloutil:onReadStream(streamId, connection)
    local collision = streamReadBool(streamId)
    self:animcol(collision, true)
end;
  
function Coloutil:onWriteStream(streamId, connection)
    streamWriteBool(streamId, self.collision)
end;
function Coloutil:mouseEvent(posX, posY, isDown, isUp, button)
end;

function Coloutil:keyEvent(unicode, sym, modifier, isDown)    
end;

function Coloutil:onUpdate(dt, vehicle)
	if self.Sangle ~= nil then
		local spec = self.spec_tensionBelts
		if spec.areBeltsFasten then
			setVisibility(self.Lock, true);	
			setVisibility(self.Unlock, false);		
		else
			setVisibility(self.Unlock, true);
			setVisibility(self.Lock, false);
		end;
	end;
end;

function Coloutil:onUpdateTick() 
end;	
function Coloutil:onDeactivate()

end;       
function Coloutil:onDraw()  
end;

function Coloutil:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
	if keyStatus > 0 then
		if actionName == 'col_outils' then
			if self.collision then
				self:animcol(false)
			else 
				self:animcol(true)
			end
		end
	end
end

function Coloutil:animcol(collision, noEventSend)
	AnimcolEvent.sendEvent(self, collision, noEventSend)
	self.collision = collision;
		if self.collision then
			if self.Sangle ~= nil then
				setVisibility(self.Sangle, true);
			end
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("colisionoutils", 1, nil, true)
			end
		else
			if self.Sangle ~= nil then
				setVisibility(self.Sangle, false);
			end
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("colisionoutils", -1, nil, true)
			end
		end;
end;

--- Event ---

AnimcolEvent = {};
AnimcolEvent_mt = Class(AnimcolEvent, Event);
InitEventClass(AnimcolEvent, "AnimcolEvent");

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