-- Contact: SlipDestroysFields@web.de
-- Date 14.08.2022

-- #LICENSE#
	-- This mod contains adapted parts of FS17_GearboxAddon by Mogli12 and is licensed under GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
	-- License file can be found in following GitHub repository: https://github.com/Frvetz/FS22_SlipDestroysFields
-- #LICENSE#

SettingsChangeEvent = {}
local SettingsChangeEvent_mt = Class(SettingsChangeEvent, Event)
InitEventClass(SettingsChangeEvent, "SettingsChangeEvent")

function SettingsChangeEvent.emptyNew()
	local event = Event.new(SettingsChangeEvent_mt)
	


	return event
end

function SettingsChangeEvent.new(CostsState, CostsSetting, LengthState, LengthSetting, IntervallState, IntervallSetting, OpeningState, OpeningSetting, ClosingState, ClosingSetting)
    local event = SettingsChangeEvent.emptyNew()
	    event.CostsState = CostsState
        event.CostsSetting = CostsSetting
        event.LengthState = LengthState
        event.LengthSetting = LengthSetting
        event.IntervallState = IntervallState
        event.IntervallSetting = IntervallSetting
        event.OpeningState = OpeningState
        event.OpeningSetting = OpeningSetting
        event.ClosingState = ClosingState
        event.ClosingSetting = ClosingSetting
	return event
end

function SettingsChangeEvent:readStream(streamId, connection)
    self.CostsState = streamReadInt32(streamId)
    self.CostsSetting = streamReadFloat32(streamId)
    self.LengthState = streamReadInt32(streamId)
    self.LengthSetting = streamReadFloat32(streamId)
    self.IntervallState = streamReadInt32(streamId)
    self.IntervallSetting = streamReadFloat32(streamId)
    self.OpeningState = streamReadInt32(streamId)
    self.OpeningSetting = streamReadInt32(streamId)
    self.ClosingState = streamReadInt32(streamId)
    self.ClosingSetting = streamReadInt32(streamId)
	
	self:run(connection)
end

function SettingsChangeEvent:writeStream(streamId, connection)
	streamWriteInt32(streamId, self.CostsState)
	streamWriteFloat32(streamId, self.CostsSetting)
	streamWriteInt32(streamId, self.LengthState)
	streamWriteFloat32(streamId, self.LengthSetting)
	streamWriteInt32(streamId, self.IntervallState)
	streamWriteFloat32(streamId, self.IntervallSetting)
	streamWriteInt32(streamId, self.OpeningState)
	streamWriteInt32(streamId, self.OpeningSetting)
	streamWriteInt32(streamId, self.ClosingState)
	streamWriteInt32(streamId, self.ClosingSetting)
end

function SettingsChangeEvent:run(connection)
	registerRealisticDamageSystem.SettingsChanged(self.CostsState, self.CostsSetting, self.LengthState, self.LengthSetting, self.IntervallState, self.IntervallSetting, self.OpeningState, self.OpeningSetting, self.ClosingState, self.ClosingSetting)
	if not connection:getIsServer() then
		g_server:broadcastEvent(SettingsChangeEvent.new(self.CostsState, self.CostsSetting, self.LengthState, self.LengthSetting, self.IntervallState, self.IntervallSetting, self.OpeningState, self.OpeningSetting, self.ClosingState, self.ClosingSetting))
	end
end