-- 
-- caplight Function
-- version: 1.0
-- date: 11.12.2015
-- author: FS15: speedy77
-- date: 22.12.2017
-- author: FS17: speedy77

agroTruck = {};

function agroTruck.prerequisitesPresent(specializations)
    return true;
end;

function agroTruck:load(savegame)
	self.setagroTruck = SpecializationUtil.callSpecializationsFunction("setagroTruck");
	self.cl = {};
	self.cl.rl = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.agroTruck#realLight"));
	self.cl.co = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.agroTruck#corona"));
	self.cl.turnOn = false;
	self.cl.isOn = false;
	self.cl.active = false;
	setVisibility( self.cl.rl, false );
	setVisibility( self.cl.co, false );
	
	agroTruckSoundFile = Utils.getFilename("Shared/blinktick.wav", self.baseDirectory);
	self.agroTruckSoundId = createSample("agroTruckSound");
	loadSample(self.agroTruckSoundId, agroTruckSoundFile, false);
	self.agroTruckSoundPlaying = false;
	
end;

function agroTruck:delete()
end;

function agroTruck:readStream(streamId, connection)
	local state = streamReadBool(streamId);
	self:setagroTruck(state, true);
end;

function agroTruck:writeStream(streamId, connection)
	streamWriteBool(streamId, self.cl.turnOn);
end;

function agroTruck:readUpdateStream(streamId, timestamp, connection)
end;

function agroTruck:writeUpdateStream(streamId, connection, dirtyMask)
end;

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

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

function agroTruck:update(dt)
	if self:getIsActive() then
		if self.isClient and self:getIsActiveForInput(false) and not self:hasInputConflictWithSelection() then
			if InputBinding.hasEvent(InputBinding.SWITHagroTruck) then
				self:setagroTruck(not self.cl.turnOn);
				playSample(self.agroTruckSoundId,1,0.5,0);
			end;
		end;
	end;
	
	local realLightsActive = false;
	realLightsActive = self:getIsActiveForLights();
	
	if (self.cl.turnOn == true and self.cl.isOn == false) or (self.cl.active ~= realLightsActive) then
		if self.cl.rl ~= nil then
			if realLightsActive then
				setVisibility( self.cl.rl, true );
			else
				setVisibility( self.cl.rl, false );
			end
		end;
		if self.cl.co ~= nil then
			setVisibility( self.cl.co, true );			
		end;
		self.cl.active = realLightsActive;
		self.cl.isOn = true;
	elseif (self.cl.turnOn == false and self.cl.isOn == true) then
		if self.cl.rl ~= nil then
			setVisibility( self.cl.rl, false );
		end;
		if self.cl.be ~= nil then
			setVisibility( self.cl.be, false );
		end;
		if self.cl.co ~= nil then
			setVisibility( self.cl.co, false );		
		end;
		self.cl.isOn = false;
	end;
end;

function agroTruck:updateTick(dt)

end;

function agroTruck:draw()
	if g_currentMission.controlledVehicle ~= nil then
		g_currentMission:addHelpButtonText(g_i18n:getText("agroTruckONOFF"), InputBinding.SWITHagroTruck);
	end;
end;

function agroTruck:onLeave()

end;

function agroTruck:onEnter()

end;

function agroTruck:setagroTruck(state, noEventSend)
	SetagroTruckEvent.sendEvent(self, state, noEventSend);
	self.cl.turnOn = state;
end;
	
	
--
SetagroTruckEvent = {};
SetagroTruckEvent_mt = Class(SetagroTruckEvent, Event);

InitEventClass(SetagroTruckEvent, "SetagroTruckEvent");

function SetagroTruckEvent:emptyNew()
    local self = Event:new(SetagroTruckEvent_mt);
    self.className="SetagroTruckEvent";
    return self;
end;

function SetagroTruckEvent:new(object, state)
    local self = SetagroTruckEvent:emptyNew()
    self.object = object;
	self.state = state;
    return self;
end;

function SetagroTruckEvent:readStream(streamId, connection)
    local id = streamReadInt32(streamId);
    self.object = networkGetObject(id);
	self.state = streamReadBool(streamId);
    self:run(connection);
end;

function SetagroTruckEvent:writeStream(streamId, connection)
    streamWriteInt32(streamId, networkGetObjectId(self.object));
	streamWriteBool(streamId, self.state);
end;

function SetagroTruckEvent:run(connection)
    self.object:setagroTruck(self.state, true);
    if not connection:getIsServer() then
        g_server:broadcastEvent(SetagroTruckEvent:new(self.object, self.state), nil, connection, self.object);
    end;
end;

function SetagroTruckEvent.sendEvent(vehicle, state, noEventSend)
	if noEventSend == nil or noEventSend == false then
		if g_server ~= nil then
			g_server:broadcastEvent(SetagroTruckEvent:new(vehicle, state), nil, nil, vehicle);
		else
			g_client:getServerConnection():sendEvent(SetagroTruckEvent:new(vehicle, state));
		end;
	end;
end;

