-- FS-Version: 	FS19
-- Title:   	SiloDisplay 
-- author:  	Farmer_Schubi
-- date:    	26.04.2019
-- Version: 	1.0.2.0

-- Copyright (©) by Farmer_Schubi

-- 28.04.2019 erweitert um mehrere FillTypes pro Rohstoff und einen zweiten Rohstoff


local Silo = {} 
Silo.__index = Silo 

function Silo.new()
  local self = setmetatable({}, Silo)
  self.modxmlname 			= ""
  self.ModDirectory         = ""
  self.storename  			= ""
  self.message    			= true
  self.fillTypes            = {}
  return self
end

function Silo.get(self)
  return self
end

function Silo.set_modxmlname(self, modxmlname)
  self.modxmlname = modxmlname
end

function Silo.get_modxmlname(self)
  return self.modxmlname
end

function Silo.set_storename(self, storename)
  self.storename = storename 
end

function Silo.set_ModDirectory(self, ModDirectory)
  self.ModDirectory = ModDirectory
end

function Silo.set_message(self, message_str)
	if message_str == "TRUE" then
		self.message = true
	else
		self.message = false	
	end
end

function Silo.add_fillType(self, name)
    local fillType     = {}
	fillType.name      = name
	fillType.search    = true
	fillType.display   = DigitalDisplay:new()
    table.insert(self.fillTypes, fillType)
end


SiloDisplay = {};

local modDesc = loadXMLFile("modDesc", g_currentModDirectory .. "modDesc.xml");
local updateMs = 60000;
SiloDisplay.version = getXMLString(modDesc, "modDesc.version");
SiloDisplay.modDirectory = g_currentModDirectory;


SiloDisplay.SiloDisp = {}
local i = 0
while true do
	local storageKey = string.format("modDesc.storeItems(0).storeItem(%d)", i)
	if not hasXMLProperty(modDesc, storageKey) then
		break
	end
	local modxmlname1 = Utils.getNoNil(getXMLString(modDesc, storageKey .. "#xmlFilename"), "Unknown");
	
	local modxml = loadXMLFile("modxml", g_currentModDirectory .. modxmlname1)
	if hasXMLProperty(modxml, "placeable.SiloDisplay") then
		local SiloDisp = Silo.new("")		
		SiloDisp:set_modxmlname(modxmlname1)
		SiloDisp:set_ModDirectory(g_currentModDirectory)
		if hasXMLProperty(modxml, "placeable.storeData.name") then
			SiloDisp:set_storename(getXMLString(modxml, "placeable.storeData.name"))
		end
		if hasXMLProperty(modxml, "placeable.Message") then
			SiloDisp:set_message(utf8ToUpper(getXMLString(modxml, "placeable.Message")))
		end
	
    	local d = 0
		while true do
			local displayKey = string.format("placeable.Displays(0).Display(%d)", d)
			if not hasXMLProperty(modxml, displayKey .. "#fillType") then
				break
			else
				SiloDisp:add_fillType(utf8ToUpper(getXMLString(modxml, displayKey .. "#fillType")))
			end	
			d = d + 1			
		end		
		table.insert(SiloDisplay.SiloDisp, SiloDisp)
	end;			
	i = i + 1;
end

for i,SiloDisp in ipairs(SiloDisplay.SiloDisp) do
	local h = Silo.get(SiloDisp)
    if h.message then	
		for f,fillType in pairs(h.fillTypes) do
			print("FillType.name " .. fillType.name)
		end
	end
end

local h = Silo.get(SiloDisp)

addModEventListener(SiloDisplay);

function SiloDisplay:loadMap()

print("############################################################");
print("--- SiloDisplay 1.0.2.0");
print("############################################################");
end

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

--end;

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


function SiloDisplay:deleteMap()
end;

function SiloDisplay:update(dt)
	local plus = 0;
	updateMs = updateMs + (dt * g_currentMission.loadingScreen.missionInfo.timeScale);
	if updateMs >= 10000   then
		updateMs = updateMs - 10000;
		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
					for zz,SiloDisp in ipairs(SiloDisplay.SiloDisp) do
						local ftm = Silo.get(SiloDisp)

						if object.storages ~= nil and type(object.storages) == "table" and object.loadingStation ~= nil and object.loadingStation.stationName ~= nil and object.loadingStation.stationName == ftm.storename then
--            				print("Silodisplay  " .. object.loadingStation.stationName .. "    " .. ftm.storename)
					    	local d = 0
					    	local d1 = 0
							for f,fillType in pairs(ftm.fillTypes) do
								if fillType.display.rootNode == nil and fillType.search then
								   fillType.search  = false	
								   local childId3 = getChild(object.nodeId, "anzeige")
--								   print("Silodisplay " .. fillType.name .. " " .. childId3)
								   if childId3 ~= 0 then
									  local childId = getChild(childId3, "displays")
									  if childId ~= 0 then
									     d1 = d + 1
    									 local weightDisplayKey = string.format("weightDisplay%d", d1)
--									     print("weightDisplayKey " .. fillType.name .. " / " .. weightDisplayKey)
										 local childId2 = getChild(childId, weightDisplayKey)
										 if childId2 ~= 0 then
											local childName = getName(childId2)
											if childName == weightDisplayKey then
											   local modxml = loadXMLFile("modxml", ftm.ModDirectory .. ftm.modxmlname)
									   		   local displayKey = string.format("placeable.Displays(0).Display(%d)", d)
--											   print("displayKey " .. fillType.name .. " / " .. displayKey)
											   if not fillType.display:load(object.nodeId, modxml, displayKey) then
												  fillType.display = nil
											   else
												  fillType.display:setValue(0)
											   end							  
											end
										 end
									  end						
								   end						
								end       
								d = d + 1	 								
							end 							

--							print("filltypename_in_stor "  .. filltypename_in_stor .. "ftm.in1 " .. ftm.in1 );
							for fillType_in_stor,isAccepted in pairs(object.storages[1].fillTypes) do
								local filltypename_in_stor = g_fillTypeManager.indexToName[fillType_in_stor];					
								for f,fillType in pairs(ftm.fillTypes) do
									if filltypename_in_stor == fillType.name then	
									   if fillType.display ~= nil then
										  fillType.display:setValue(object.storages[1].fillLevels[fillType_in_stor])
									   end									
									end
								end					
     						end
							
						end
					end
				end;
			end;
		end;
	end;
end; 

function SiloDisplay:draw()

end
