-- SoSi-Modding --

SoSiHornScript = {};

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

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

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

function SoSiHornScript:onLoad(vehicle)	
	self.horn_s = SoSiHornScript.horn_s
	self.horn_l = SoSiHornScript.horn_l
	self.horn_p = SoSiHornScript.horn_p
	self.Horn_s = false
	self.Horn_l = false
	self.Horn_p = false
	
end;

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

function SoSiHornScript:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
	if keyStatus > 0 then
		if actionName == 'horn_s' then
			if self.Horn_s then
				self:horn_s(false)
			else 
				self:horn_s(true)
			end
		end
		if actionName == 'horn_l' then
			if self.Horn_l then
				self:horn_l(false)
			else 
				self:horn_l(true)
			end
		end
		if actionName == 'horn_p' then
			if self.Horn_p then
				self:horn_p(false)
			else 
				self:horn_p(true)
			end
		end
	end
end

function SoSiHornScript:horn_s(Horn_s, noEventSend)
	horn_sEvent.sendEvent(self, Horn_s, noEventSend)
	self.Horn_s = Horn_s;
		if self.Horn_s then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("horn_s", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("horn_s", -1, nil, true)
			end
		end;
end;

function SoSiHornScript:horn_l(Horn_l, noEventSend)
	horn_lEvent.sendEvent(self, Horn_l, noEventSend)
	self.Horn_l = Horn_l;
		if self.Horn_l then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("horn_l", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("horn_l", -1, nil, true)
			end
		end;
end;

function SoSiHornScript:horn_p(Horn_p, noEventSend)
	horn_pEvent.sendEvent(self, Horn_p, noEventSend)
	self.Horn_p = Horn_p;
		if self.Horn_p then
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("horn_p", 1, nil, true)
			end
		else
			for name,_ in pairs(self.spec_animatedVehicle.animations) do
				self:playAnimation("horn_p", -1, nil, true)
			end
		end;
end;

--- Event ---

horn_sEvent = {};
horn_sEvent_mt = Class(horn_sEvent, Event);
InitEventClass(horn_sEvent, "horn_sEvent");

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


horn_lEvent = {};
horn_lEvent_mt = Class(horn_lEvent, Event);
InitEventClass(horn_lEvent, "horn_lEvent");

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


horn_pEvent = {};
horn_pEvent_mt = Class(horn_pEvent, Event);
InitEventClass(horn_pEvent, "horn_pEvent");

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