-- Contact: RealisticDamageSystem@gmail.com
-- THANKS TO W33ZL (POWER TOOLS MOD) FOR LETTING ME USE HIS MENU!
-- Date 2.12.2021


-- THANKS YOU FOR THE REGISTER.LUA IAN!

source(g_currentModDirectory.."events/SettingsChangeEvent.lua")
registerRealisticDamageSystem = {}

registerRealisticDamageSystem.ModName = g_currentModName

registerRealisticDamageSystem.CostsState = 9;
registerRealisticDamageSystem.CostsSetting = 1.0;
registerRealisticDamageSystem.LengthState = 9;
registerRealisticDamageSystem.LengthSetting = 1.0;
registerRealisticDamageSystem.IntervallState = 9;
registerRealisticDamageSystem.IntervallSetting = 1.0;
registerRealisticDamageSystem.OpeningState = 4;
registerRealisticDamageSystem.OpeningSetting = 7;
registerRealisticDamageSystem.ClosingState = 3;
registerRealisticDamageSystem.ClosingSetting = 21;


g_specializationManager:addSpecialization("RealisticDamageSystem", "RealisticDamageSystem", g_currentModDirectory.."RealisticDamageSystem.lua", "")

function registerRealisticDamageSystem.registerSpecialization()
	local specName = "RealisticDamageSystem"

	for vehicleType, vehicle in pairs(g_vehicleTypeManager.types) do

		if vehicle ~= nil and vehicleType ~= "locomotive" and vehicleType ~= "ConveyorBelt" and vehicleType ~= "pickupConveyorBelt" and vehicleType ~= "woodCrusherTrailermotorized" and vehicleType ~= "baleWrapper" and vehicleType ~= "craneTrailer" then

			local ismotorized = false;
			local hasNotRDS = true;

			for name, spec in pairs(vehicle.specializationsByName) do
				if name == "motorized" then
					ismotorized = true;
				elseif name == "RealisticDamageSystem" then
					hasNotRDS = false;
				end
			end
			if hasNotRDS and ismotorized then
				print("  adding RealisticDamageSystem to vehicleType '"..tostring(vehicleType).."'")

				local specObject = g_specializationManager:getSpecializationObjectByName(specName);

				vehicle.specializationsByName[specName] = specObject;
				table.insert(vehicle.specializationNames, specName);
				table.insert(vehicle.specializations, specObject);
			end
		end
	end
end

TypeManager.finalizeTypes = Utils.appendedFunction(TypeManager.finalizeTypes, registerRealisticDamageSystem.registerSpecialization)



function init()
	InGameMenuGameSettingsFrame.onFrameOpen = Utils.appendedFunction(InGameMenuGameSettingsFrame.onFrameOpen, registerRealisticDamageSystem.installGUI)
    InGameMenuGameSettingsFrame.updateGameSettings = Utils.appendedFunction(InGameMenuGameSettingsFrame.updateGameSettings, registerRealisticDamageSystem.updateRealisticDamageSystemGui)	
end


function registerRealisticDamageSystem:installGUI()
	if not self.initRDSGuiDone then
		self.RDSCosts = self.checkDirt:clone()
		self.RDSCosts.target = registerRealisticDamageSystem
		self.RDSCosts.id = "RDSCosts"
		self.RDSCosts:setCallback("onClickCallback", "onRealisticDamageSystemCostsChanged")


		self.RDSCosts.elements[4]:setText(g_i18n:getText("setting_RealisticDamageSystem_Costs"))
		self.RDSCosts.elements[6]:setText(g_i18n:getText("explanation_RealisticDamageSystem_Costs"))
		
		local title = TextElement.new()
		title:applyProfile("settingsMenuSubtitle", true)
		title:setText(g_i18n:getText("ModName"))

		self.boxLayout:addElement(title)
		self.boxLayout:addElement(self.RDSCosts)

		self.RDSCosts:setTexts({"20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%", "110%", "120%", "130%", "140%", "150%", "160%", "170%", "180%", "190%", "200%"})
		
		
		
		
		self.RDSLength = self.checkDirt:clone()
		self.RDSLength.target = registerRealisticDamageSystem
		self.RDSLength.id = "RDSLength"
		self.RDSLength:setCallback("onClickCallback", "onRealisticDamageSystemLengthChanged")


		self.RDSLength.elements[4]:setText(g_i18n:getText("setting_RealisticDamageSystem_Length"))
		self.RDSLength.elements[6]:setText(g_i18n:getText("explanation_RealisticDamageSystem_Length"))
		
		self.RDSLength:setTexts({"20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%", "110%", "120%", "130%", "140%", "150%", "160%", "170%", "180%", "190%", "200%"})

		self.boxLayout:addElement(self.RDSLength)
		
		
		
		
		
		self.RDSIntervall = self.checkDirt:clone()
		self.RDSIntervall.target = registerRealisticDamageSystem
		self.RDSIntervall.id = "RDSIntervall"
		self.RDSIntervall:setCallback("onClickCallback", "onRealisticDamageSystemIntervallChanged")


		self.RDSIntervall.elements[4]:setText(g_i18n:getText("setting_RealisticDamageSystem_Intervall"))
		self.RDSIntervall.elements[6]:setText(g_i18n:getText("explanation_RealisticDamageSystem_Intervall"))
		
		self.RDSIntervall:setTexts({"20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%", "110%", "120%", "130%", "140%", "150%", "160%", "170%", "180%", "190%", "200%"})

		self.boxLayout:addElement(self.RDSIntervall)
		
		
		
		
		
		
		self.RDSOpeningHour = self.checkDirt:clone()
		self.RDSOpeningHour.target = registerRealisticDamageSystem
		self.RDSOpeningHour.id = "RDSOpeningHour"
		self.RDSOpeningHour:setCallback("onClickCallback", "onRealisticDamageSystemOpeningHourChanged")


		self.RDSOpeningHour.elements[4]:setText(g_i18n:getText("setting_RealisticDamageSystem_OpeningHour"))
		self.RDSOpeningHour.elements[6]:setText(g_i18n:getText("explanation_RealisticDamageSystem_OpeningHour"))
		
		self.RDSOpeningHour:setTexts({g_i18n:getText("selection_RDS_4oClock"), g_i18n:getText("selection_RDS_5oClock"), g_i18n:getText("selection_RDS_6oClock"), g_i18n:getText("selection_RDS_7oClock"), g_i18n:getText("selection_RDS_8oClock"), g_i18n:getText("selection_RDS_9oClock")})

		self.boxLayout:addElement(self.RDSOpeningHour)
		
		
		
		
		
		
		self.RDSClosingHour = self.checkDirt:clone()
		self.RDSClosingHour.target = registerRealisticDamageSystem
		self.RDSClosingHour.id = "RDSClosingHour"
		self.RDSClosingHour:setCallback("onClickCallback", "onRealisticDamageSystemClosingHourChanged")


		self.RDSClosingHour.elements[4]:setText(g_i18n:getText("setting_RealisticDamageSystem_ClosingHour"))
		self.RDSClosingHour.elements[6]:setText(g_i18n:getText("explanation_RealisticDamageSystem_ClosingHour"))
		
		self.RDSClosingHour:setTexts({g_i18n:getText("selection_RDS_19oClock"), g_i18n:getText("selection_RDS_20oClock"), g_i18n:getText("selection_RDS_21oClock"), g_i18n:getText("selection_RDS_22oClock"), g_i18n:getText("selection_RDS_23oClock")})

		self.boxLayout:addElement(self.RDSClosingHour)
		
		
		
		self.initRDSGuiDone = true
		
		
		
		
		self.RDSCosts:setState(registerRealisticDamageSystem.CostsState)
		self.RDSLength:setState(registerRealisticDamageSystem.LengthState)
		self.RDSIntervall:setState(registerRealisticDamageSystem.IntervallState)
		self.RDSOpeningHour:setState(registerRealisticDamageSystem.OpeningState)
		self.RDSClosingHour:setState(registerRealisticDamageSystem.ClosingState)
	end
end

function registerRealisticDamageSystem.updateRealisticDamageSystemGui(self)
    if self.initRDSGuiDone and self.RDSCosts ~= nil then
        self.RDSCosts:setState(registerRealisticDamageSystem.CostsState)
    end
	if self.initRDSGuiDone and self.RDSLength ~= nil then
        self.RDSLength:setState(registerRealisticDamageSystem.LengthState)
    end
	if self.initRDSGuiDone and self.RDSIntervall ~= nil then
        self.RDSIntervall:setState(registerRealisticDamageSystem.IntervallState)
    end
	if self.initRDSGuiDone and self.RDSOpeningHour ~= nil then
        self.RDSOpeningHour:setState(registerRealisticDamageSystem.OpeningState)
    end
	if self.initRDSGuiDone and self.RDSClosingHour ~= nil then
        self.RDSClosingHour:setState(registerRealisticDamageSystem.ClosingState)
    end
end

function registerRealisticDamageSystem:onRealisticDamageSystemCostsChanged(state)
	registerRealisticDamageSystem.CostsState = state;
	
	if state == 1 then
		registerRealisticDamageSystem.CostsSetting = 0.2
	elseif state == 2 then
		registerRealisticDamageSystem.CostsSetting = 0.3
	elseif state == 3 then
		registerRealisticDamageSystem.CostsSetting = 0.4
	elseif state == 4 then
		registerRealisticDamageSystem.CostsSetting = 0.5
	elseif state == 5 then
		registerRealisticDamageSystem.CostsSetting = 0.6
	elseif state == 6 then
		registerRealisticDamageSystem.CostsSetting = 0.7
	elseif state == 7 then
		registerRealisticDamageSystem.CostsSetting = 0.8
	elseif state == 8 then
		registerRealisticDamageSystem.CostsSetting = 0.9
	elseif state == 9 then
		registerRealisticDamageSystem.CostsSetting = 1.0
	elseif state == 10 then
		registerRealisticDamageSystem.CostsSetting = 1.1
	elseif state == 11 then
		registerRealisticDamageSystem.CostsSetting = 1.2
	elseif state == 12 then
		registerRealisticDamageSystem.CostsSetting = 1.3
	elseif state == 13 then
		registerRealisticDamageSystem.CostsSetting = 1.4
	elseif state == 14 then
		registerRealisticDamageSystem.CostsSetting = 1.5
	elseif state == 15 then
		registerRealisticDamageSystem.CostsSetting = 1.6
	elseif state == 16 then
		registerRealisticDamageSystem.CostsSetting = 1.7
	elseif state == 17 then
		registerRealisticDamageSystem.CostsSetting = 1.8
	elseif state == 18 then
		registerRealisticDamageSystem.CostsSetting = 1.9
	elseif state == 19 then
		registerRealisticDamageSystem.CostsSetting = 2.0
	end
	
	g_client:getServerConnection():sendEvent(SettingsChangeEvent.new(registerRealisticDamageSystem.CostsState, registerRealisticDamageSystem.CostsSetting, registerRealisticDamageSystem.LengthState, registerRealisticDamageSystem.LengthSetting, registerRealisticDamageSystem.IntervallState, registerRealisticDamageSystem.IntervallSetting, registerRealisticDamageSystem.OpeningState, registerRealisticDamageSystem.OpeningSetting, registerRealisticDamageSystem.ClosingState, registerRealisticDamageSystem.ClosingSetting))
	
	g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingCostsChanged"):format(registerRealisticDamageSystem.CostsSetting * 100), "", 2500)
end



function registerRealisticDamageSystem:onRealisticDamageSystemLengthChanged(state)
	registerRealisticDamageSystem.LengthState = state;
	
	if state == 1 then
		registerRealisticDamageSystem.LengthSetting = 0.2
	elseif state == 2 then
		registerRealisticDamageSystem.LengthSetting = 0.3
	elseif state == 3 then
		registerRealisticDamageSystem.LengthSetting = 0.4
	elseif state == 4 then
		registerRealisticDamageSystem.LengthSetting = 0.5
	elseif state == 5 then
		registerRealisticDamageSystem.LengthSetting = 0.6
	elseif state == 6 then
		registerRealisticDamageSystem.LengthSetting = 0.7
	elseif state == 7 then
		registerRealisticDamageSystem.LengthSetting = 0.8
	elseif state == 8 then
		registerRealisticDamageSystem.LengthSetting = 0.9
	elseif state == 9 then
		registerRealisticDamageSystem.LengthSetting = 1.0
	elseif state == 10 then
		registerRealisticDamageSystem.LengthSetting = 1.1
	elseif state == 11 then
		registerRealisticDamageSystem.LengthSetting = 1.2
	elseif state == 12 then
		registerRealisticDamageSystem.LengthSetting = 1.3
	elseif state == 13 then
		registerRealisticDamageSystem.LengthSetting = 1.4
	elseif state == 14 then
		registerRealisticDamageSystem.LengthSetting = 1.5
	elseif state == 15 then
		registerRealisticDamageSystem.LengthSetting = 1.6
	elseif state == 16 then
		registerRealisticDamageSystem.LengthSetting = 1.7
	elseif state == 17 then
		registerRealisticDamageSystem.LengthSetting = 1.8
	elseif state == 18 then
		registerRealisticDamageSystem.LengthSetting = 1.9
	elseif state == 19 then
		registerRealisticDamageSystem.LengthSetting = 2.0
	end
	
	g_client:getServerConnection():sendEvent(SettingsChangeEvent.new(registerRealisticDamageSystem.CostsState, registerRealisticDamageSystem.CostsSetting, registerRealisticDamageSystem.LengthState, registerRealisticDamageSystem.LengthSetting, registerRealisticDamageSystem.IntervallState, registerRealisticDamageSystem.IntervallSetting, registerRealisticDamageSystem.OpeningState, registerRealisticDamageSystem.OpeningSetting, registerRealisticDamageSystem.ClosingState, registerRealisticDamageSystem.ClosingSetting))
	
	g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingLengthChanged"):format(registerRealisticDamageSystem.LengthSetting * 100), "", 2500)
end



function registerRealisticDamageSystem:onRealisticDamageSystemIntervallChanged(state)
	registerRealisticDamageSystem.IntervallState = state;
	
	if state == 1 then
		registerRealisticDamageSystem.IntervallSetting = 0.2
	elseif state == 2 then
		registerRealisticDamageSystem.IntervallSetting = 0.3
	elseif state == 3 then
		registerRealisticDamageSystem.IntervallSetting = 0.4
	elseif state == 4 then
		registerRealisticDamageSystem.IntervallSetting = 0.5
	elseif state == 5 then
		registerRealisticDamageSystem.IntervallSetting = 0.6
	elseif state == 6 then
		registerRealisticDamageSystem.IntervallSetting = 0.7
	elseif state == 7 then
		registerRealisticDamageSystem.IntervallSetting = 0.8
	elseif state == 8 then
		registerRealisticDamageSystem.IntervallSetting = 0.9
	elseif state == 9 then
		registerRealisticDamageSystem.IntervallSetting = 1.0
	elseif state == 10 then
		registerRealisticDamageSystem.IntervallSetting = 1.1
	elseif state == 11 then
		registerRealisticDamageSystem.IntervallSetting = 1.2
	elseif state == 12 then
		registerRealisticDamageSystem.IntervallSetting = 1.3
	elseif state == 13 then
		registerRealisticDamageSystem.IntervallSetting = 1.4
	elseif state == 14 then
		registerRealisticDamageSystem.IntervallSetting = 1.5
	elseif state == 15 then
		registerRealisticDamageSystem.IntervallSetting = 1.6
	elseif state == 16 then
		registerRealisticDamageSystem.IntervallSetting = 1.7
	elseif state == 17 then
		registerRealisticDamageSystem.IntervallSetting = 1.8
	elseif state == 18 then
		registerRealisticDamageSystem.IntervallSetting = 1.9
	elseif state == 19 then
		registerRealisticDamageSystem.IntervallSetting = 2.0
	end
	
	g_client:getServerConnection():sendEvent(SettingsChangeEvent.new(registerRealisticDamageSystem.CostsState, registerRealisticDamageSystem.CostsSetting, registerRealisticDamageSystem.LengthState, registerRealisticDamageSystem.LengthSetting, registerRealisticDamageSystem.IntervallState, registerRealisticDamageSystem.IntervallSetting, registerRealisticDamageSystem.OpeningState, registerRealisticDamageSystem.OpeningSetting, registerRealisticDamageSystem.ClosingState, registerRealisticDamageSystem.ClosingSetting))
	
	g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingIntervallChanged"):format(registerRealisticDamageSystem.IntervallSetting * 100), "", 2500)
end



function registerRealisticDamageSystem:onRealisticDamageSystemOpeningHourChanged(state)
	registerRealisticDamageSystem.OpeningState = state;
	
	if state == 1 then
		registerRealisticDamageSystem.OpeningSetting = 4
	elseif state == 2 then
		registerRealisticDamageSystem.OpeningSetting = 5
	elseif state == 3 then
		registerRealisticDamageSystem.OpeningSetting = 6
	elseif state == 4 then
		registerRealisticDamageSystem.OpeningSetting = 7
	elseif state == 5 then
		registerRealisticDamageSystem.OpeningSetting = 8
	elseif state == 6 then
		registerRealisticDamageSystem.OpeningSetting = 9
	end
	
	g_client:getServerConnection():sendEvent(SettingsChangeEvent.new(registerRealisticDamageSystem.CostsState, registerRealisticDamageSystem.CostsSetting, registerRealisticDamageSystem.LengthState, registerRealisticDamageSystem.LengthSetting, registerRealisticDamageSystem.IntervallState, registerRealisticDamageSystem.IntervallSetting, registerRealisticDamageSystem.OpeningState, registerRealisticDamageSystem.OpeningSetting, registerRealisticDamageSystem.ClosingState, registerRealisticDamageSystem.ClosingSetting))
	
	g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingOpeningHourChanged"):format(registerRealisticDamageSystem.OpeningSetting), "", 2500)
end





function registerRealisticDamageSystem:onRealisticDamageSystemClosingHourChanged(state)
	registerRealisticDamageSystem.ClosingState = state;
	
	if state == 1 then
		registerRealisticDamageSystem.ClosingSetting = 19
	elseif state == 2 then
		registerRealisticDamageSystem.ClosingSetting = 20
	elseif state == 3 then
		registerRealisticDamageSystem.ClosingSetting = 21
	elseif state == 4 then
		registerRealisticDamageSystem.ClosingSetting = 22
	elseif state == 5 then
		registerRealisticDamageSystem.ClosingSetting = 23
	end
	
	g_client:getServerConnection():sendEvent(SettingsChangeEvent.new(registerRealisticDamageSystem.CostsState, registerRealisticDamageSystem.CostsSetting, registerRealisticDamageSystem.LengthState, registerRealisticDamageSystem.LengthSetting, registerRealisticDamageSystem.IntervallState, registerRealisticDamageSystem.IntervallSetting, registerRealisticDamageSystem.OpeningState, registerRealisticDamageSystem.OpeningSetting, registerRealisticDamageSystem.ClosingState, registerRealisticDamageSystem.ClosingSetting))
	
	g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingClosingHourChanged"):format(registerRealisticDamageSystem.ClosingSetting), "", 2500)
end







function registerRealisticDamageSystem.SettingsChanged(CostsState, CostsSetting, LengthState, LengthSetting, IntervallState, IntervallSetting, OpeningState, OpeningSetting, ClosingState, ClosingSetting)
	if registerRealisticDamageSystem.CostsState ~= CostsState then
		g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingCostsChanged"):format(CostsSetting * 100), "", 2500)
	end
	if registerRealisticDamageSystem.LengthState ~= LengthState then
		g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingLengthChanged"):format(LengthSetting * 100), "", 2500)
	end
	if registerRealisticDamageSystem.IntervallState ~= IntervallState then
		g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingIntervallChanged"):format(IntervallSetting * 100), "", 2500)
	end
	if registerRealisticDamageSystem.OpeningState ~= OpeningState then
		g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingOpeningHourChanged"):format(OpeningSetting), "", 2500)
	end
	if registerRealisticDamageSystem.ClosingState ~= ClosingState then
		g_currentMission:addGameNotification(g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("ModName"), g_i18n.modEnvironments[registerRealisticDamageSystem.ModName]:getText("SettingClosingHourChanged"):format(ClosingSetting), "", 2500)
	end

	registerRealisticDamageSystem.CostsState = CostsState
	registerRealisticDamageSystem.CostsSetting = CostsSetting
	registerRealisticDamageSystem.LengthState = LengthState
	registerRealisticDamageSystem.LengthSetting = LengthSetting
	registerRealisticDamageSystem.IntervallState = IntervallState
	registerRealisticDamageSystem.IntervallSetting = IntervallSetting
	registerRealisticDamageSystem.OpeningState = OpeningState
	registerRealisticDamageSystem.OpeningSetting = OpeningSetting
	registerRealisticDamageSystem.ClosingState = ClosingState
	registerRealisticDamageSystem.ClosingSetting = ClosingSetting
end



init()




-- make localizations available
local i18nTable = getfenv(0).g_i18n
for l18nId,l18nText in pairs(g_i18n.texts) do
  i18nTable:setText(l18nId, l18nText)
end