-- Fredzaza --

Godet = {};

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

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

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

function Godet:onLoad(vehicle)	
	self.animcol = Godet.animcol
	self.collision = false

	self.racine2 = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.objet#index"));
	self.godetvisual = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.godetvisual#index"));
end;

function Godet:onDelete() 
end;

function Godet:onReadStream(streamId, connection)
    local collision = streamReadBool(streamId)
    self:animcol(collision, true)
end;
  
function Godet:onWriteStream(streamId, connection)
    streamWriteBool(streamId, self.collision)
end;

function Godet:mouseEvent(posX, posY, isDown, isUp, button)
end;

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

function Godet:onUpdate(dt, vehicle)
		local attacherVehicle = self:getAttacherVehicle()
		if attacherVehicle ~= nil then
			if attacherVehicle.godetvisualpelle ~= nil then
				link(attacherVehicle.godetvisualpelle, self.godetvisual)
			end
		else
			unlink(self.godetvisual)
			link(self.racine2, self.godetvisual)
        end
end;

function Godet:onUpdateTick() 
end;	
function Godet:onDeactivate()
	unlink(self.godetvisual)
	link(self.racine2, self.godetvisual)
end;       
function Godet:onDraw()  
end;

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

function Godet:animcol(collision, noEventSend)
	AnimcolEvent.sendEvent(self, collision, noEventSend)
	self.collision = collision;
		if self.collision then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("colisiongodet", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("colisiongodet", -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;