ZENON = {};
ZENON.directory = g_currentModDirectory

ZENON.tabela = {
	zenek1 = "wheat",
	zenek2 = "grass",
	zenek3 = "canola",
	zenek4 = "barley",
	zenek5 = "maize",
	zenek6 = "decoFoliage",
	zenek7 = "potato",
	zenek8 = "sugarBeet",
	zenek9 = "sunflower",
	zenek10 = "soybean",
	zenek11 = "oilseedRadish",
	zenek12 = "poplar",
	zenek13 = "bush01",
	zenek14 = "oat",
	zenek15 = "sugarCane",
	zenek16 = "cotton"
};

function ZENON:loadMap(name)
	local image = Utils.getFilename("textures/pitchfork_normal.png", ZENON.directory)
	local name = "ZENON"
	local price = 666
	
	for i=1, table.getn(g_storeManager.items) do
		g_storeManager.items[i].imageFilename = image
		g_storeManager.items[i].name = name
		g_storeManager.items[i].price = price
		for w=1, table.getn(g_storeManager.items[i].functions) do
			g_storeManager.items[i].functions[w] = name
		end
	end
	
	for x=1, table.getn(g_brandManager.indexToBrand) do
		g_brandManager.indexToBrand[x].imageShopOverview = image
		g_brandManager.indexToBrand[x].image = image
		g_brandManager.indexToBrand[x].title = name
	end
	
	for _, c in pairs(g_storeManager.categories) do
		c.image = image
		c.title = name
	end
	
	local zenekMaterial = Utils.getFilename("materialHolders/plane_materialHolder.i3d", ZENON.directory)
	self.zenekMaterial = loadI3DFile(zenekMaterial, false, true, false)
	
	local zenekNode = getChildAt(self.zenekMaterial, 0);
	self.zenekMat = getMaterial(zenekNode, 0)
	
	local zenekSound = Utils.getFilename("sounds/fill.ogg", ZENON.directory);
	self.zenekSound = createSample("zenosław");
	loadSample(self.zenekSound, zenekSound, false);
	
	
    local path = Utils.getFilename("materialHolders/linkToRoot.i3d", ZENON.directory)
    local rootNode = loadI3DFile(path, false, false, false)
    if rootNode == 0 then
        return false
    end
	
    self.lightningNode = rootNode
    link(getRootNode(), rootNode)
    setVisibility(rootNode, true)
    setWorldTranslation(rootNode, 0, 1800, 0)
	
	self:loadZenon(getRootNode())
	
	local zenekuprawyMaterial = Utils.getFilename("materialHolders/fruit_materialHolder.i3d", ZENON.directory)
	self.zenekuprawyMaterial = loadI3DFile(zenekuprawyMaterial, false, true, false)
	
	local zenekuprawyNode = getChildAt(self.zenekuprawyMaterial, 0);
	self.zenekuprawyMat = getMaterial(zenekuprawyNode, 0)
end;

function ZENON:deleteMap() 
    if self.zenekMaterial ~= nil then
        delete(self.zenekMaterial)
        self.zenekMaterial = nil
    end
    if self.zenekuprawyMat ~= nil then
        delete(self.zenekuprawyMat)
        self.zenekuprawyMat = nil
    end
end; 

function ZENON:update(dt)
	if self.zenekmaterialy == nil then
		for _, sss in pairs(ZENON.tabela) do
			local terrainDetail = getTerrainDetailByName(g_currentMission.terrainRootNode, sss)
			for i = 0, getNumOfChildren(terrainDetail) - 1 do
				local nodeId = getChildAt(terrainDetail, i)
				setMaterial(nodeId, self.zenekuprawyMat, 0)
			end
		end
		self.zenekmaterialy = 1
	end

	for f=1, table.getn(g_currentMission.vehicles) do
		if g_currentMission.vehicles[f].spec_washable ~= nil then
			local washSpec = g_currentMission.vehicles[f].spec_washable
			for t=1, table.getn(washSpec.washableNodes) do
				for _, node in pairs(washSpec.washableNodes[t].nodes) do
					if getMaterial(node, 0) ~= self.zenekMat then
						setMaterial(node, self.zenekMat, 0)
					end
				end
			end
		end
	end

	if not g_currentMission.player.isEntered then
		if self.zenekplejer == nil then
			playSample(self.zenekSound, 1000, 1000, 1000, 1000, 1000);
			self.zenekplejer = true
		end
	else
		if self.zenekplejer then
			stopSample(self.zenekSound, 1000, 1000);
			self.zenekplejer = nil 
		end
	end
end;

function ZENON:loadZenon(nodeId) 
    local nodeName = getName(nodeId)
	if getHasClassId(nodeId, ClassIds.SHAPE) then
		if getRigidBodyType(nodeId) ~= "NoRigidBody" or string.find(nodeName, "attachments") or string.find(nodeName, "LOD0Attachments") or string.find(nodeName, "LOD") or getClipDistance(nodeId) < 300 then
			setMaterial(nodeId, self.zenekMat, 0)
		end
	end

    for i = 0, getNumOfChildren(nodeId) - 1 do
        local childId = getChildAt(nodeId, i)
        if childId ~= 0 then
            self:loadZenon(childId)
        end
    end
end

addModEventListener(ZENON);