ChopperConfigurations = {};

function ChopperConfigurations.initSpecialization()
    local schema = Vehicle.xmlSchema
    schema:setXMLSpecializationType("ChopperConfigurations")
	
	if g_configurationManager.configurations["chopper"] == nil then
		g_configurationManager:addConfigurationType("chopper", g_i18n:getText("configuration_choppa"), nil, nil, nil, nil, ConfigurationUtil.SELECTOR_MULTIOPTION)
	end
	
	ObjectChangeUtil.registerObjectChangeXMLPaths(schema, "vehicle.chopperConfigurations.chopperConfiguration(?)")
	schema:register(XMLValueType.BOOL, "vehicle.chopperConfigurations.chopperConfiguration(?).swath#available", "yes or no")
	schema:register(XMLValueType.BOOL, "vehicle.chopperConfigurations.chopperConfiguration(?).swath#isDefaultActive", "yes or no")
	schema:register(XMLValueType.INT, "vehicle.chopperConfigurations.chopperConfiguration(?).swath#workAreaIndex", "indexArea", 0)
	schema:register(XMLValueType.BOOL, "vehicle.chopperConfigurations.chopperConfiguration(?).chopper#available", "yes or no")
	schema:register(XMLValueType.INT, "vehicle.chopperConfigurations.chopperConfiguration(?).chopper#workAreaIndex", "work area", 0)
	schema:register(XMLValueType.STRING, "vehicle.chopperConfigurations.chopperConfiguration(?).chopper#animName", "Animation name")
	schema:register(XMLValueType.FLOAT, "vehicle.chopperConfigurations.chopperConfiguration(?).chopper#animSpeedScale", "Speed scale", 1)
	schema:register(XMLValueType.FLOAT, "vehicle.chopperConfigurations.chopperConfiguration(?).processing#toggleTime", "processingTime", 1)
	
	schema:setXMLSpecializationType()
end;

function ChopperConfigurations.prerequisitesPresent(specializations)
	return SpecializationUtil.hasSpecialization(Combine, specializations)
end;

function ChopperConfigurations.registerEventListeners(vehicleType)	
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", ChopperConfigurations)
	SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", ChopperConfigurations)
end

function ChopperConfigurations:onLoad(savegame)
    local spec = self.spec_combine
	ObjectChangeUtil.updateObjectChanges(self.xmlFile, "vehicle.chopperConfigurations.chopperConfiguration", self.configurations["chopper"], self.components, self);
    spec.workAreaParameters = {}
    spec.workAreaParameters.lastRealArea = 0
    spec.workAreaParameters.lastArea = 0
    spec.workAreaParameters.litersToDrop = 0
    spec.workAreaParameters.droppedLiters = 0
    spec.workAreaParameters.isChopperEffectEnabled = 0
    spec.workAreaParameters.isStrawEffectEnabled = 0
    spec.workAreaParameters.effectDensity = 0.2
    spec.workAreaParameters.effectDensitySent = 0.2
end;

function ChopperConfigurations:onPostLoad(savegame)
	ChopperConfigurations.loadChopperConfigurations(self, self.xmlFile, self.configurations["chopper"], self.spec_combine);
end;

function ChopperConfigurations:loadChopperConfigurations(xmlFile, configId, entry)
	local key = string.format("vehicle.chopperConfigurations.chopperConfiguration(%d)", configId-1)
    local spec = self.spec_combine

    entry.swath = {}
    entry.swath.isAvailable = self.xmlFile:getValue(key .. ".swath#available",false)
    local isDefaultActive = self.xmlFile:getValue(key .. ".swath#isDefaultActive", entry.swath.isAvailable)
    if entry.swath.isAvailable then
        entry.swath.workAreaIndex = self.xmlFile:getValue(key .. ".swath#workAreaIndex")
        if entry.swath.workAreaIndex == nil then
            entry.swath.isAvailable = false
            g_logManager:xmlWarning(self.configFileName, "Missing 'swath#workAreaIndex' for combine swath function!")
        end
        entry.warningTime = 0
    end

    entry.chopper = {}
    entry.chopper.isAvailable = self.xmlFile:getValue(key .. ".chopper#available",false)
    if entry.chopper.isAvailable then
        entry.chopper.workAreaIndex = self.xmlFile:getValue(key .. ".chopper#workAreaIndex")
        if entry.chopper.workAreaIndex == nil then
            entry.chopper.isAvailable = false
            g_logManager:xmlWarning(self.configFileName, "Missing 'chopper#workAreaIndex' for combine chopper function!")
        end
        entry.chopper.animName = self.xmlFile:getValue(key .. ".chopper#animName")
        entry.chopper.animSpeedScale = self.xmlFile:getValue(key .. ".chopper#animSpeedScale")
    end
    entry.isSwathActive = isDefaultActive
	
    entry.processing = {}
    local toggleTime = xmlFile:getValue(key .. ".processing#toggleTime")
    if toggleTime == nil and entry.chopper.animName ~= nil then
        toggleTime = self:getAnimationDurection(entry.chopper.animName)
        if toggleTime ~= nil then
            toggleTime = toggleTime / 1000
        end
    end
    entry.processing.toggleTime = Utils.getNoNil(toggleTime, 0) * 1000
	
    local inputBuffer = {}
    local slotDuration = 300
    local slotCount = MathUtil.clamp(math.ceil(entry.processing.toggleTime / slotDuration), 2, 20)
    inputBuffer.slotCount = slotCount
    inputBuffer.slotDuration = math.ceil( entry.processing.toggleTime / inputBuffer.slotCount )
    inputBuffer.fillIndex = 1
    inputBuffer.dropIndex = inputBuffer.fillIndex + 1
    inputBuffer.slotTimer = inputBuffer.slotDuration
    inputBuffer.activeTimeout = inputBuffer.slotDuration * (inputBuffer.slotCount + 2)
    inputBuffer.activeTimer = inputBuffer.activeTimeout
    inputBuffer.buffer = {}
    for _=1, inputBuffer.slotCount do
        table.insert(inputBuffer.buffer, {realArea = 0, area = 0, liters = 0, inputLiters = 0, strawRatio = 0, effectDensity = 0.2} )
    end
    entry.processing.inputBuffer = inputBuffer




end
function ChopperConfigurations:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
    if spec.isFilling ~= spec.sentIsFilling or spec.chopperPSenabled ~= spec.sentChopperPSenabled or spec.strawPSenabled ~= spec.sentStrawPSenabled or math.abs(spec.workAreaParameters.effectDensity - spec.workAreaParameters.effectDensitySent) > 0.05 then
        self:raiseDirtyFlags(spec.effectDirtyFlag)

        spec.sentIsFilling = spec.isFilling
        spec.sentChopperPSenabled = spec.chopperPSenabled
        spec.sentStrawPSenabled = spec.strawPSenabled
        spec.workAreaParameters.effectDensitySent = spec.workAreaParameters.effectDensity
    end
end