local S = core.get_translator(core.get_current_modname())

--                                          ____________________________
--_________________________________________/    Variables & Functions    \_________

local flower_effect = {
	[ "mcl_flowers:allium" ] = "fire_resistance",
	[ "mcl_flowers:azure_bluet" ] = "blindness",
	[ "mcl_flowers:lily_of_the_valley" ] = "poison",
	[ "mcl_flowers:blue_orchid" ] = "saturation",
	[ "mcl_flowers:dandelion" ] = "saturation",
	[ "mcl_flowers:cornflower" ] = "jump",
	[ "mcl_flowers:oxeye_daisy" ] = "regeneration",
	[ "mcl_flowers:poppy" ] = "night_vision",
	[ "mcl_flowers:wither_rose" ] = "withering",
	[ "mcl_flowers:tulip_orange" ] = "weakness",
	[ "mcl_flowers:tulip_pink" ] = "weakness",
	[ "mcl_flowers:tulip_red" ] = "weakness",
	[ "mcl_flowers:tulip_white" ] = "weakness",
	[ "mcl_flowers:eyeblossom" ] = "nausea",
	[ "mcl_flowers:eyeblossom_open" ] = "blindness",
}

local effects = {
	[ "fire_resistance" ] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect("fire_resistance", placer, 1, 4)
	end,

	[ "blindness" ] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect("blindness", placer, 1, 8)
	end,

	[ "poison" ] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect_by_level("poison", placer, 1, 12)
	end,

	[ "saturation" ] = function(itemstack, placer, pointed_thing, player)
		mcl_potions.give_effect_by_level("saturation", placer, 1, 0.5)
	end,

	["jump"] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect_by_level("leaping", placer, 1, 6)
	end,

	["regeneration"] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect_by_level("regeneration", placer, 1, 8)
	end,

	["withering"] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect_by_level("withering", placer, 1, 8)
	end,

	["weakness"] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect_by_level("weakness", placer, 1, 9)
	end,

	["night_vision"] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect("night_vision", placer, 1, 5)
	end,

	["nausea"] = function(itemstack, placer, pointed_thing)
		mcl_potions.give_effect("nausea", placer, 1, 8)
	end,
}

local function get_random_effect()
	local keys = {}
	for k, _ in pairs(effects) do
		table.insert(keys, k)
	end
	return effects[keys[math.random(#keys)]]
end

core.register_on_craft(function(itemstack, _, old_craft_grid, _)
	if itemstack:get_name() ~= "mcl_sus_stew:stew" then return end
	for f,e in pairs(flower_effect) do
		for _,it in pairs(old_craft_grid) do
			if it:get_name() == f then
				itemstack:get_meta():set_string("effect",e)
				return itemstack
			end
		end
	end
end)

core.register_craftitem("mcl_sus_stew:stew",{
	description = S("Suspicious Stew"),
	inventory_image = "sus_stew.png",
	stack_max = 1,
	groups = { food = 2, eatable = 6, can_eat_when_full = 1, not_in_creative_inventory=1,},
	_mcl_saturation = 7.2,
	_mcl_eat_replace_with = "mcl_core:bowl",
	_mcl_eat_effect = function (itemstack, placer, pointed_thing)
		local e = itemstack:get_meta():get_string("effect")
		local f = effects[e]
		if not f then
			f = get_random_effect()
		end
		f(itemstack, placer, pointed_thing)
	end,
})

--compat with old (mcl5) sus_stew
core.register_alias("mcl_sus_stew:poison_stew", "mcl_sus_stew:stew")
core.register_alias("mcl_sus_stew:hunger_stew", "mcl_sus_stew:stew")
core.register_alias("mcl_sus_stew:jump_boost_stew", "mcl_sus_stew:stew")
core.register_alias("mcl_sus_stew:regneration_stew", "mcl_sus_stew:stew")
core.register_alias("mcl_sus_stew:night_vision_stew", "mcl_sus_stew:stew")

--										 ______________
--_________________________________________/	Crafts	\________________________________

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:allium"},
})

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:lily_of_the_valley"},
})

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:blue_orchid"},
})

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:dandelion"} ,
})

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:cornflower"},
})

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:oxeye_daisy"},
})

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:poppy"},
})

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:eyeblossom"},
})

core.register_craft({
	type = "shapeless",
	output = "mcl_sus_stew:stew",
	recipe = {"mcl_mushrooms:mushroom_red", "mcl_mushrooms:mushroom_brown", "mcl_core:bowl", "mcl_flowers:eyeblossom_open"},
})
