-- This file is part of FS25_Through_The_Years.
--
-- FS25_Through_The_Years is free software: you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- FS25_Through_The_Years is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
-- details.
--
-- You should have received a copy of the GNU General Public License along with
-- FS25_Through_The_Years. If not, see <https://www.gnu.org/licenses/>.
--
-- This file contains adapted parts of FS25_SlipDestroysFields by frvetz and is licensed under
-- GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007.

source(g_currentModDirectory .. "type_control_options.lua")

LoadSettingsForClientEvent = {}
local LoadSettingsForClientEvent_mt = Class(LoadSettingsForClientEvent, Event)
InitEventClass(LoadSettingsForClientEvent, "LoadSettingsForClientEvent")

function LoadSettingsForClientEvent.emptyNew()
	local event = Event.new(LoadSettingsForClientEvent_mt)

	event.typeControlOptions = TypeControlOptions.getDefaultOptions()

	return event
end

function LoadSettingsForClientEvent.new(baseYear, baseYearState, filterNoYearState, vehicleShopExpirationState,
										typeControlOptions)
    local event = LoadSettingsForClientEvent.emptyNew()

	event.baseYear = baseYear
	event.baseYearState = baseYearState
	event.filterNoYearState = filterNoYearState
	event.vehicleShopExpirationState = vehicleShopExpirationState
	event.typeControlOptions = typeControlOptions

	return event
end

function LoadSettingsForClientEvent:readStream(streamId, connection)
    self.baseYear = streamReadInt32(streamId)
	self.baseYearState = streamReadInt32(streamId)
	self.filterNoYearState = streamReadInt32(streamId)
	self.vehicleShopExpirationState = streamReadInt32(streamId)

	for category, _ in TypeControlOptions.pairsByKeys(self.typeControlOptions) do
		self.typeControlOptions[category].state = streamReadInt32(streamId)
	end

	self:run(connection)
end

function LoadSettingsForClientEvent:writeStream(streamId, connection)
	streamWriteInt32(streamId, self.baseYear)
	streamWriteInt32(streamId, self.baseYearState)
	streamWriteInt32(streamId, self.filterNoYearState)
	streamWriteInt32(streamId, self.vehicleShopExpirationState)

	for category, _ in TypeControlOptions.pairsByKeys(self.typeControlOptions) do
		streamWriteInt32(streamId, self.typeControlOptions[category].state)
	end
end

function LoadSettingsForClientEvent:run(connection)
	ThroughTheYears.SettingsChanged(
		self.baseYear, self.baseYearState, self.filterNoYearState, self.vehicleShopExpirationState,
		self.typeControlOptions)
	if g_server ~= nil then
		g_server:broadcastEvent(LoadSettingsForClientEvent.new(
			self.baseYear, self.baseYearState, self.filterNoYearState, self.vehicleShopExpirationState,
			self.typeControlOptions))
	end
end
