BaleCounters = {
	SEND_NUM_BITS = 16,
	MOD_NAME = g_currentModName,
	SPEC_NAME = g_currentModName .. ".baleCounter",
	SPEC_TABLE_NAME = "spec_" .. g_currentModName .. ".baleCounter"
}

source(g_currentModDirectory .. "scripts/specializations/events/BaleCounterResetEvent.lua")
source(g_currentModDirectory .. "scripts/hud/BaleCounterHUDExtension.lua")
VehicleHUDExtension.registerHUDExtension(BaleCounters, BaleCounterHUDExtension)

function BaleCounters.prerequisitesPresent(specializations)
	return SpecializationUtil.hasSpecialization(Baler, specializations)
end

function BaleCounters.initSpecialization()
	local schema = Vehicle.xmlSchema

	schema:setXMLSpecializationType("BaleCounters")
	Dashboard.registerDashboardXMLPaths(schema, "vehicle.baleCounter.dashboards", "sessionCounter lifetimeCounter")
	schema:setXMLSpecializationType()

	local schemaSavegame = Vehicle.xmlSchemaSavegame
	local baseKey = "vehicles.vehicle(?)." .. BaleCounters.SPEC_NAME

	schemaSavegame:register(XMLValueType.INT, baseKey .. "#sessionCounter", "Session counter")
	schemaSavegame:register(XMLValueType.INT, baseKey .. "#lifetimeCounter", "Lifetime counter")
end

function BaleCounters.registerFunctions(vehicleType)
	SpecializationUtil.registerFunction(vehicleType, "doBaleCounterResets", BaleCounters.doBaleCounterResets)
end

function BaleCounters.registerOverwrittenFunctions(vehicleType)
	SpecializationUtil.registerOverwrittenFunction(vehicleType, "dropBale", BaleCounters.dropBale)
end

function BaleCounters.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", BaleCounters)
	SpecializationUtil.registerEventListener(vehicleType, "onReadStream", BaleCounters)
	SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", BaleCounters)
	SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", BaleCounters)
end

function BaleCounters:onLoad(savegame)
	local spec = self[BaleCounters.SPEC_TABLE_NAME]
	spec.sessionCounter = 0
	spec.lifetimeCounter = 0

	if savegame ~= nil and not savegame.resetVehicles then
		local key = savegame.key .. "." .. BaleCounters.SPEC_NAME
		spec.sessionCounter = savegame.xmlFile:getValue(key .. "#sessionCounter", spec.sessionCounter)
		spec.lifetimeCounter = savegame.xmlFile:getValue(key .. "#lifetimeCounter", spec.lifetimeCounter)
	end

	if self.loadDashboardsFromXML ~= nil then
		self:loadDashboardsFromXML(self.xmlFile, "vehicle.baleCounter.dashboards", {
			valueFunc = "sessionCounter",
			valueTypeToLoad = "sessionCounter",
			valueObject = spec
		})
		self:loadDashboardsFromXML(self.xmlFile, "vehicle.baleCounter.dashboards", {
			valueFunc = "lifetimeCounter",
			valueTypeToLoad = "lifetimeCounter",
			valueObject = spec
		})
	end
end

function BaleCounters:saveToXMLFile(xmlFile, key, usedModNames)
	local spec = self[BaleCounters.SPEC_TABLE_NAME]

	xmlFile:setValue(key .. "#sessionCounter", spec.sessionCounter)
	xmlFile:setValue(key .. "#lifetimeCounter", spec.lifetimeCounter)
end

function BaleCounters:onReadStream(streamId, connection)
	local spec = self[BaleCounters.SPEC_TABLE_NAME]
	spec.sessionCounter = streamReadUIntN(streamId, BaleCounters.SEND_NUM_BITS)
	spec.lifetimeCounter = streamReadUIntN(streamId, BaleCounters.SEND_NUM_BITS)
end

function BaleCounters:onWriteStream(streamId, connection)
	local spec = self[BaleCounters.SPEC_TABLE_NAME]

	streamWriteUIntN(streamId, spec.sessionCounter, BaleCounters.SEND_NUM_BITS)
	streamWriteUIntN(streamId, spec.lifetimeCounter, BaleCounters.SEND_NUM_BITS)
end

function BaleCounters:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
	if self.isClient then
		local spec = self[BaleCounters.SPEC_TABLE_NAME]

		self:clearActionEventsTable(spec.actionEvents)

		if isActiveForInputIgnoreSelection then
			local _, actionEventId = self:addPoweredActionEvent(spec.actionEvents, InputAction.BALE_COUNTER_RESETT, self, BaleCounters.actionEventResetCounter, false, true, false, true, nil)

			g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_HIGH)
		end
	end
end

function BaleCounters:actionEventResetCounter(actionName, inputValue, callbackState, isAnalog)
	self:doBaleCounterResets()
end

function BaleCounters:doBaleCounterResets(noEventSend)
	local spec = self[BaleCounters.SPEC_TABLE_NAME]
	spec.sessionCounter = 0

	BaleCounterResetEvent.sendEvent(self, noEventSend)
end

function BaleCounters:dropBale(superFunc, baleIndex)
	superFunc(self, baleIndex)

	local spec = self[BaleCounters.SPEC_TABLE_NAME]
	spec.sessionCounter = spec.sessionCounter + 1
	spec.lifetimeCounter = spec.lifetimeCounter + 1
end
