local nature_common_filter = {
	gold = 1,
	crafting = 4,
}

local nature_rare_filter = {
	gold = 3,
	crafting = 2,
	potion = 1,
}

local village_filter = {
	gold = 2,	
	constructed_materials = 1,	
	food = 0.25,
	clothes = 0.05,
	combat = 0.01,
}

local village_chest = {
	gold = 2,
	food = 0.5,
	combat = 0.5,
	potion = 0.5,
}


local default_filter = {
	gold = 1,
	general = 1,
	combat = 1,
}

local default_amount = 3
local shop_min = 1
local shop_max = 1

local shelf_min = 1
local shelf_max = 2

local chest_min = 4
local chest_max = 8

local RULE_CONTAINERS = {
	categories = {
		librarian_shop = {
			min_amount = shop_min,
			max_amount = shop_max,
			filter = {

			},
		},
		chef_shop = {
			min_amount = shop_min,
			max_amount = shop_max,
			filter = {
				food = 1,
			},
		},
		armorer_shop = {
			min_amount = shop_min,
			max_amount = shop_max,
			filter = {
				armor = 1,
				clothes = 1,
			},
		},
		smith_shop = {
			min_amount = shop_min,
			max_amount = shop_max,
			filter = {
				melee = 1,
			},
		},
		wizard_shop = {
			min_amount = shop_min,
			max_amount = shop_max,
			filter = {
				magical = 1,
				jewlery = 1,
				wizard = 1,				
			},
		},
		merchant_shop = {
			min_amount = shop_min,
			max_amount = shop_max,
			filter = {
				general = 1,
				combat = 1,
				jewlery = 1,
				weapon = 1,
				clothes = 1,
				armor = 1,
				potion = 1,
				crafting = 1,
			},
		},
		alchemist_shop = {
			min_amount = shop_min,
			max_amount = shop_max,
			filter = {
				potion = 1,
			},
		},
		ranger_shop = {
			min_amount = shop_min,
			max_amount = shop_max,
			filter = {
				ranged = 1,				
			},
		},

		craft_wood = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_wood = 1,
			},
		},
		craft_leather = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_leather = 1,
			},
		},
		craft_stone = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_stone = 1,
			},
		},
		craft_bone = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_bone = 1,
			},
		},
		craft_dark_wood = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_dark_wood = 1,
			},
		},
		craft_copper = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_copper = 1,
			},
		},
		craft_obsidian = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_obsidian = 1,
			},
		},
		craft_iron = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_iron = 1,
			},
		},
		craft_cothra = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_cothra = 1,
			},
		},
		craft_fur = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_fur = 1,
			},
		},
		craft_silk = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_silk = 1,
			},
		},
		craft_starfibre = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_starfibre = 1,
			},
		},
		craft_dragonscale = {
			min_amount = 1,
			max_amount = 1,
			filter = {
				crafted_by_dragonscale = 1,
			},
		},

		--
		well = {
			max_amount = 4,
			filter = village_filter,
			rarity_factor = 0.5,
		},
		bucket = {
			max_amount = 1,
			filter = village_filter,
			rarity_factor = 0.5,
		},
		rope_coil = {
			max_amount = 1,
			filter = village_filter,
			rarity_factor = 0.5,
		},
		cart = {
			max_amount = 3,
			filter = village_filter,
			rarity_factor = 0.5,
		},
		crate = {
			max_amount = 2,
			filter = village_filter,
			rarity_factor = 0.5,
		},
		haybale = {
			max_amount = 1,
			filter = village_filter,
			rarity_factor = 0.5,
		},
		barrel = {
			max_amount = 3,
			filter = village_filter,
			rarity_factor = 0.5,
		},

		--house
		house_shelf = {
			max_amount = 2,
			filter = {
				gold = 2,
				food = 0.5,
			},
		},
		house_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				gold = 1,
				civilian = 4,
				constructed_materials = 1,
				food = 0.5,
				potion = 0.5,				
			},
		},

		--library
		book_shelf = {			
			max_amount = 2,
			filter = {
				scroll = 0.5,
				gold = 2,
			},
			rarity_factor = 0.75,
		},
		library_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				scroll = 2,
				gold = 1,
			},
		},


		--kitchen
		kitchen_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				food = 2,
				gold = 2,
			},
		},
		kitchen_bin = {
			min_amount = 3,
			max_amount = 5,
			filter = {				
				gold = 1,
				natural_materials = 3,
			},
			rarity_factor = 0.5,
		},
		food_crate = {
			max_amount = default_amount,
			filter = {
				food = 2,
				gold = 2,
			},
			rarity_factor = 0.75,
		},
		food_basket = {
			max_amount = default_amount,
			filter = {
				food = 2,
				gold = 2,
			},
		},


		--graveyard
		graveyard_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				combat = 1,
				potion = 1,
				gold = 3,
			},
		},
		small_urn = {
			max_amount = 1,
			filter = {
				gold = 1,
			},
			rarity_factor = 0.5,
		},
		big_urn = {
			min_amount = 4,
			max_amount = 5,
			filter = {
				gold = 1,
			},
			rarity_factor = 0.75,
		},
		bat = {
			max_amount = 1,
			filter = {
				gold = 1,
			},
			rarity_factor = 0.25,
		},
		zombie = {
			max_amount = 2,
			filter = {
				gold = 5,
				potion_materials = 2,
				food = 0.2,
				general = 0.02,
			},
			rarity_factor = 0.5,
		},
		skeleton = {
			max_amount = 2,
			filter = {
				gold = 3,
				potion_materials = 3,
				general = 0.02,
			},
			rarity_factor = 0.5,
		},
		skeleton_mage = {
			max_amount = 3,
			filter = {
				gold = 5,
				magic = 1,
				wizard = 1,
				general = 0.05,
			},
			rarity_factor = 0.75,
		},
		mummy = {
			max_amount = 3,
			filter = {
				gold = 5,
				elastic_materials = 1,
				general = 0.5,
			},
			rarity_factor = 0.75,
		},
		vampire = {
			min_amount = 3,
			max_amount = 5,
			filter = {
				gold = 2,
				combat = 1,
				crafting = 1,
			},
		},
		necromancer = {
			min_amount = 3,
			max_amount = 5,
			filter = {
				gold = 2,
				combat = 1,
				crafting = 1,
			},
		},

		--crypt
		crypt_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				combat = 0.25,
				potion = 1,
				gold = 3,
				natural_materials = 3,
			},
		},


		--armorer
		armorer_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				armor = 0.5,
				gold = 2,
				armor_materials = 3,
			},
		},
		armorer_cabinet = {
			max_amount = default_amount,
			filter = {
				armor_materials = 1,
				gold = 3,
			},
			rarity_factor = 0.75,
		},
		armor_rack = {
			max_amount = default_amount,
			filter = {
				armor = 1,				
				gold = 2,
			},
		},
		

		--smith
		smith_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				hard_materials = 2,
				melee = 1,
				gold = 2,
			},
		},
		smith_cabinet = {
			max_amount = default_amount,
			filter = {
				hard_materials = 2,
				gold = 2,
			},
			rarity_factor = 0.75,
		},
		weapon_rack = {
			max_amount = default_amount,
			filter = {
				melee = 1,
				gold = 1,
			},
		},
		tool_box = {
			max_amount = default_amount,
			filter = {
				hard_materials = 2,
			},
			rarity_factor = 0.75,
		},


		--wizard
		wizard_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				wizard = 1,
				magical = 1,
				jewlery = 1,
				gold = 2,
			},
		},
		robe_closet = {
			max_amount = default_amount,
			filter = {
				wizard = 1,
			},
			rarity_factor = 0.75,
		},
		staff_rack = {
			max_amount = default_amount,
			filter = {
				staff = 1,
			},
		},
		jewlery_box = {
			min_amount = 3,
			max_amount = 4,
			filter = {
				jewlery = 1,
				gold = 1,
			},
		},

		--merchant
		merchant_knapsack = {
			min_amount = 1,
			max_amount = 2,
			filter = {
				general = 1,
				food = 1,
			},
		},
		merchant_crate = {
			min_amount = 2,
			max_amount = 3,
			filter = {
				general = 1,
				combat = 1,
			},
			rarity_factor = 0.75,
		},
		merchant_wagon = {
			min_amount = 5,
			max_amount = 10,
			filter = {
				general = 0.25,
				combat = 0.5,
				crafting = 2,
				potion = 0.5,
				gold = 0.5,
			},
			rarity_factor = 0.75,
		},


		--alchemist
		alchemist_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				potion = 1,
				gold = 1,
				potion_materials = 1,
			},
		},
		potion_rack = {
			max_amount = default_amount,
			filter = {
				potion = 2,
				gold = 1,
			},
		},
		ingredient_crate = {
			max_amount = default_amount,
			filter = {
				gold = 1,
				potion_materials = 2,
			},
		},

		--ranger
		ranger_chest = {
			min_amount = chest_min,
			max_amount = chest_max,
			filter = {
				elastic_materials = 2,
				gold = 2,
				ranged = 1.5,
			},
		},
		writing_desk = {
			max_amount = default_amount,
			filter = {
				gold = 1,
				elastic_materials = 2,
			},
			rarity_factor = 0.75,
		},
		pen_box = {
			max_amount = default_amount,
			filter = {
				gold = 1,
				elastic_materials = 2,
			},
			rarity_factor = 0.75,
		},

		--insane
		mentalist = {
			max_amount = default_amount,
			filter = {
				gold = 2,
				potion_materials = 0.5,
				combat = 0.05,
				potion = 0.5,
			},
			rarity_factor = 0.25,
		},
		mentalist_monster = {
			max_amount = default_amount,
			filter = {
				natural_materials = 1,
				gold = 1,
			},
			rarity_factor = 0.25,
		},
		mentalist_chest = {
			max_amount = default_amount,
			filter = {
				combat = 0.25,
				gold = 1,
				crafting = 1,
			},
		},
		ritual_urn = {
			max_amount = default_amount,
			filter = {
				gold = 1,				
			},
			rarity_factor = 0.5,
		},
		mentalist_barrel = {
			max_amount = default_amount,
			filter = {
				food = 1,
			},
			rarity_factor = 0.25,
		},

		--animals
		animal_small = {
			max_amount = 1,
			filter = {
				natural_materials = 1,
				gold = 1,
			},
			rarity_factor = 0.25,
		},
		nest_small = {
			min_amount = 2,
			max_amount = 3,
			filter = {
				natural_materials = 2,
				gold = 1,
			},
			rarity_factor = 0.75,
		},

		animal_medium = {
			max_amount = 2,
			filter = {
				natural_materials = 1,
				gold = 1,
			},
			rarity_factor = 0.5,
		},
		nest_medium = {
			min_amount = 3,
			max_amount = 5,
			filter = {
				natural_materials = 2,
				gold = 1,
			},
			rarity_factor = 0.75,
		},
		
		animal_big = {
			min_amount = 2,
			max_amount = 3,
			filter = {
				natural_materials = 1,
				gold = 1,
			},
			rarity_factor = 0.75,
		},
		nest_big = {
			min_amount = 4,
			max_amount = 7,
			filter = {
				natural_materials = 2,
				gold = 1,
			},
			rarity_factor = 0.75,
		},

		--nature
		plant_potent = {
			max_amount = 1,
			filter = {
				potion_materials = 1,
				gold = 1,
			},
			rarity_factor = 0.25,
		},
		fruit_bush = {
			max_amount = default_amount,
			filter = {
				fruit = 1,
			},
			rarity_factor = 0.25,
		},
		tangled_roots = {
			max_amount = default_amount,
			filter = {
				natural_materials = 1,
			},
			rarity_factor = 0.25,
		},
		rock_loose = {
			max_amount = default_amount,
			filter = {
				gold = 1,
				general = 0.25,
			},
			rarity_factor = 0.25,
		}
	},
	generate = function(rules, def, level, index)
		if not def[level] then
			local items = {}
			local item_rules = rules:get_categories("item")
			for category, cat_def in pairs(item_rules) do
				if cat_def.filters then
					local amt_factor = 1
					if cat_def.level_peak then
						local diff = math.abs(level-cat_def.level_peak)
						amt_factor = 1-math.clamp(diff/(cat_def.level_range*0.5), 0, 1)
						--print(amount_factor, level, cat_def.level_peak, cat_def.level_range)
					end
					if amt_factor and amt_factor > 0 then
						assert(cat_def.filters, category)
						for filter, amt in pairs(def.filter) do
							if cat_def.filters[filter] then
								local final_chance = amt*amt_factor*(cat_def.level_peak_factor or 1)
								items[category] = final_chance								
								break
							end
						end
					end
				end
			end

			local min = def.min_amount or 1
			local max = math.max(min,def.max_amount or 1)
			local level_def = {
				min_picks = min,
				max_picks = max,
				items = items,
			}

			def[level] = level_def
		end

		return Container:new(def[level], level, index)
	end,
}

return RULE_CONTAINERS