-- FS-Version: 	FS19
-- Title:   	FillTypeMover 
-- author:  	Farmer_Schubi
-- date:    	26.04.2019
-- Version: 	1.0.0.0

-- Copyright (©) by Farmer_Schubi

FillTypeMover = {};

local modDesc = loadXMLFile("modDesc", g_currentModDirectory .. "modDesc.xml");
local updateMs = 60000;
FillTypeMover.version = getXMLString(modDesc, "modDesc.version");
FillTypeMover.modDirectory = g_currentModDirectory;
local i = 0;
local modDescKey = string.format("modDesc.storeItems(%d).storeItem", i)
if hasXMLProperty(modDesc, modDescKey) then
	local modxmlname = Utils.getNoNil(getXMLString(modDesc, modDescKey .. "#xmlFilename"), "Unknown");
	print("modDescKey: " .. modDescKey .. "modxmlname:  " .. modxmlname);
	
	local modxml = loadXMLFile("modxml", g_currentModDirectory .. modxmlname);
	if hasXMLProperty(modxml, "placeable.storeData.name") then
		FillTypeMover.storename = getXMLString(modxml, "placeable.storeData.name");
	else
		FillTypeMover.storename = "";
	end;
	print("storename: " .. FillTypeMover.storename);
	
	if hasXMLProperty(modxml, "placeable.litersPerMin") then
		FillTypeMover.litersPerMin = getXMLInt(modxml, "placeable.litersPerMin");
	else
		FillTypeMover.litersPerMin = 100;
	end;
	print("litersPerMin: " .. FillTypeMover.litersPerMin);
	
	local capacityPerFillTypeKey = string.format("placeable.storages(%d).storage", i)	
	FillTypeMover.capacityPerFillType = Utils.getNoNil(getXMLInt(modxml, capacityPerFillTypeKey .. "#capacityPerFillType"), "Unknown");
	if FillTypeMover.capacityPerFillType == "Unknown" then
		FillTypeMover.capacityPerFillType = 500000;
	end;
	print("capacityPerFillType: " .. FillTypeMover.capacityPerFillType);
	
	if hasXMLProperty(modxml, "placeable.filltypename_in") then
		FillTypeMover.filltypename_in = utf8ToUpper(getXMLString(modxml, "placeable.filltypename_in"));
	else
		FillTypeMover.filltypename_in = utf8ToUpper("grass_windrow");
	end;	
	print("filltypename_in: " .. FillTypeMover.filltypename_in);
	
	if hasXMLProperty(modxml, "placeable.filltypename_out") then
		FillTypeMover.filltypename_out = utf8ToUpper(getXMLString(modxml, "placeable.filltypename_out"));
	else
		FillTypeMover.filltypename_out = utf8ToUpper("drygrass_windrow");
	end;	
	print("filltypename_out: " .. FillTypeMover.filltypename_out);	
end

addModEventListener(FillTypeMover);

function FillTypeMover:loadMap()

print("############################################################");
print("--- FillTypeMover " .. self.version);
print("############################################################");
end

--function FillTypeMover:keyEvent(unicode, sym, modifier, isDown)

--end;

--function FillTypeMover:mouseEvent(posX, posY, isDown, isUp, button)
--end;


function FillTypeMover:deleteMap()
end;

function FillTypeMover:update(dt)
	local plus = 0;
	updateMs = updateMs + (dt * g_currentMission.loadingScreen.missionInfo.timeScale);
	if updateMs >= 60000   then
		updateMs = updateMs - 60000;
--		print("--- FillTypeMover dt:" .. dt .. updateMs);
		for a=1, #g_currentMission.placeables do
			if g_currentMission.placeables[a] ~= nil then
				local object = g_currentMission.placeables[a];
				if object ~= nil and object.nodeId ~= nil then
					if object.storages ~= nil and type(object.storages) == "table" and object.loadingStation ~= nil and object.loadingStation.stationName ~= nil  and object.loadingStation.stationName == FillTypeMover.storename then
--						print("nodeId "  .. object.nodeId .. "FarmId " .. object.storages[1].ownerFarmId ..  "stationName " .. Utils.getNoNil(object.loadingStation.stationName, "Unknown"));
--						DebugUtil.printTableRecursively(object.storages[1].fillTypes,".",0,1);
						for fillType_in,isAccepted in pairs(object.storages[1].fillTypes) do
							local filltypename_in = g_fillTypeManager.indexToName[fillType_in];
							if filltypename_in == FillTypeMover.filltypename_in then
								if object.storages[1].fillLevels[fillType_in] > 0 then
									for fillType_out,isAccepted in pairs(object.storages[1].fillTypes) do
										local filltypename_out = g_fillTypeManager.indexToName[fillType_out];
										if filltypename_out == FillTypeMover.filltypename_out then
										    if object.storages[1].fillLevels[fillType_in] > FillTypeMover.litersPerMin then
												plus = FillTypeMover.litersPerMin;
											else
												plus =  object.storages[1].fillLevels[fillType_in];
											end;
--											print("plus: " .. plus);
											out = object.storages[1].fillLevels[fillType_out] + plus;
											if out <= FillTypeMover.capacityPerFillType then
												object.storages[1].fillLevels[fillType_out] = object.storages[1].fillLevels[fillType_out] + plus;
												object.storages[1].fillLevels[fillType_in] = object.storages[1].fillLevels[fillType_in] - plus;
												print("Storename Skript 2: " .. FillTypeMover.storename .. " fillType_in " .. filltypename_in .. " fillLevels " .. math.floor(object.storages[1].fillLevels[fillType_in]) .. "  fillType_out " .. filltypename_out .. " fillLevels " .. math.floor(object.storages[1].fillLevels[fillType_out]));
											end;
										end;
									end;	
								end;
							end;
						end;
					end;
				end;
			end;
		end;
	end;
end; 

function FillTypeMover:draw()

end