--
-- Load support for MT game translation.
local S = minetest.get_translator("bucket")

-- Default Liquid Namespace
Liquid_name = {}
Liquid_name.water = {}
Liquid_name.lava = {}

-- Default water and lava
Liquid_name.water.source = "default:water_source"
Liquid_name.water.flowing = "default:water_flowing"
Liquid_name.water.river_source = "default:river_water_source"
Liquid_name.water.river_flowing = "default:river_water_flowing"
Liquid_name.lava.source = "default:lava_source"
Liquid_name.lava.flowing = "default:lava_flowing"

--bucket waret and lava
Liquid_name.water.bucket_source = "bucket:bucket_water_source"
Liquid_name.water.bucket_flowing = "bucket:bucket_water_flowing"
Liquid_name.lava.bucket_source = "bucket:bucket_lava_source"
Liquid_name.lava.bucket_flowing = "bucket:bucket_lava_flowing"
	--Bucket river water not used (not needed yet)
	--Liquid_name.water.bucket_river_source = "bucket:bucket_river_water_source"
	--Liquid_name.water.bucket_river_flowing = "bucket:bucket_river_water_flowing"

-- Liquid flowing range definitions (Default)
local bucket_water_range = 1
local bucket_lava_range = 1
-- load settingtypes
bucket_water_range = minetest.settings:get("bucket_water_flowing_range") or bucket_water_range
bucket_lava_range = minetest.settings:get("bucket_lava_flowing_range") or bucket_lava_range

-- Registred nodes according to aliases
local water_source_def = minetest.deserialize(minetest.serialize(minetest.registered_nodes[Liquid_name.water.source]))
local water_flowing_def = minetest.deserialize(minetest.serialize(minetest.registered_nodes[Liquid_name.water.flowing]))
local lava_source_def = minetest.deserialize(minetest.serialize(minetest.registered_nodes[Liquid_name.lava.source]))
local lava_flowing_def = minetest.deserialize(minetest.serialize(minetest.registered_nodes[Liquid_name.lava.flowing]))

-- NEW liquid definitions "bucket" liquid
--minetest.log(dump(minetest.registered_nodes[Liquid_name.water.source]))
minetest.register_node(Liquid_name.water.bucket_source, water_source_def)
minetest.override_item(Liquid_name.water.bucket_source, {
	description = S("Bucket Water Source"),
	liquid_alternative_flowing = Liquid_name.water.bucket_flowing,
	liquid_alternative_source = Liquid_name.water.bucket_source,
	liquid_renewable = false,
})
-- For dynamic_liquid support
if minetest.get_modpath("dynamic_liquid") then
	-- node is detroyed by lava, Lava source blocks will turn these blocks into steam.
	minetest.registered_nodes[Liquid_name.water.bucket_source].groups.dynamic_lava_source_destroys = 1
	-- node cools lava
	minetest.registered_nodes[Liquid_name.water.bucket_source].groups.dynamic_cools_lava_source = 1
	minetest.registered_nodes[Liquid_name.water.bucket_source].groups.dynamic_cools_lava_flowing = 1
end

minetest.register_node(Liquid_name.water.bucket_flowing, water_flowing_def)
minetest.override_item(Liquid_name.water.bucket_flowing, {
	description = S("Flowing Bucket Water"),
	liquid_alternative_flowing = Liquid_name.water.bucket_flowing,
	liquid_alternative_source = Liquid_name.water.bucket_source,
	liquid_range = bucket_water_range,
	liquid_renewable = false,
})

-- For dynamic_liquid support
if minetest.get_modpath("dynamic_liquid") then
			-- in dynamic_liquid is flowing water not cooling lava
	minetest.registered_nodes[Liquid_name.water.bucket_flowing].groups.cools_lava = 0
		-- node is detroyed by lava, Flowing lava will turn these blocks into steam
	minetest.registered_nodes[Liquid_name.water.bucket_flowing].groups.dynamic_lava_source_destroys = 1
	minetest.registered_nodes[Liquid_name.water.bucket_flowing].groups.dynamic_lava_flowing_destroys = 1

	-- gedining bucket water as dynamic liquid
	dynamic_liquid.liquid_abm(Liquid_name.water.bucket_source, Liquid_name.water.bucket_flowing, 1)

	-- this new funktion is needed, because setting goups is not enough
	-- not implemented in actrual version (12.12.2020) - musst be added, or use fork https://github.com/daretmavi/dynamic_liquid
	-- check if add_to_water_nodes is in the mod dynamic_liquid available. _G is a global table on lua that stores every global function in it.
	if _G["dynamic_liquid"]["add_to_water_nodes"] then
		dynamic_liquid.add_to_water_nodes(Liquid_name.water.bucket_source, Liquid_name.water.bucket_flowing)
	else
		local Dyn_liq_error = S("Mod: dynamic_liquid is not changed. add_to_water_nodes function is missing! Lava colling will not work the same ways, as the default water-lava interaction works.")
		minetest.log(Dyn_liq_error)
	end
end

minetest.register_node(Liquid_name.lava.bucket_source, lava_source_def)
minetest.override_item(Liquid_name.lava.bucket_source, {
	description = S("Bucket Lava Source"),
	liquid_alternative_flowing = Liquid_name.lava.bucket_flowing,
	liquid_alternative_source = Liquid_name.lava.bucket_source,
	liquid_renewable = false,
})

minetest.register_node(Liquid_name.lava.bucket_flowing, lava_flowing_def)
minetest.override_item(Liquid_name.lava.bucket_flowing, {
	description = S("Bucket Lava Source"),
	liquid_alternative_flowing = Liquid_name.lava.bucket_flowing,
	liquid_alternative_source = Liquid_name.lava.bucket_source,
	liquid_range = bucket_lava_range,
	liquid_renewable = false,
})


-- Change Bucket Water to normal "water_source" on contact with it
minetest.register_abm({
	label = "Water Source normalization",
	nodenames = {Liquid_name.water.bucket_source},
	neighbors = {Liquid_name.water.source,
				Liquid_name.water.flowing},
	interval = 1.0, -- Run every 1 second
	chance = 1, -- Select every 1 nod
	action = function(pos, node, active_object_count, active_object_count_wider)
			local pos = {x = pos.x, y = pos.y, z = pos.z}
			minetest.set_node(pos, {name = Liquid_name.water.source})
	end
})

--
minetest.register_abm({
	label = "Water Flowing normalization",
	nodenames = {Liquid_name.water.bucket_flowing},
	neighbors = {Liquid_name.water.source,
				Liquid_name.water.flowing},
	interval = 1.0, -- Run every 1 second
	chance = 1, -- Select every 1 nod
	action = function(pos, node, active_object_count, active_object_count_wider)
			local pos = {x = pos.x, y = pos.y, z = pos.z}
			minetest.set_node(pos, {name = Liquid_name.water.flowing})
	end
})

-- Change Bucket Water to normal "river_water_source" on contact with it
minetest.register_abm({
	label = "River Water Source normalization",
	nodenames = {Liquid_name.water.bucket_source},
	neighbors = {Liquid_name.water.river_source,
				Liquid_name.water.river_flowing},
	interval = 1.0, -- Run every 1 second
	chance = 1, -- Select every 1 nod
	action = function(pos, node, active_object_count, active_object_count_wider)
			local pos = {x = pos.x, y = pos.y, z = pos.z}
			minetest.set_node(pos, {name = Liquid_name.water.river_source})
	end
})

-- Change Bucket Lava to normal on contact with lava or water
minetest.register_abm({
	label = "Lava Source normalization",
	nodenames = {Liquid_name.lava.bucket_source},
	neighbors = {Liquid_name.lava.source,
				Liquid_name.lava.flowing,
				Liquid_name.water.source,
				Liquid_name.water.flowing,
				Liquid_name.water.river_source,
				Liquid_name.water.river_flowing,
				Liquid_name.water.bucket_source,
				Liquid_name.water.bucket_flowing},
	interval = 1.0, -- Run every 1 second
	chance = 1, -- Select every 1 nod
	action = function(pos, node, active_object_count, active_object_count_wider)
			local pos = {x = pos.x, y = pos.y, z = pos.z}
			minetest.set_node(pos, {name = Liquid_name.lava.source})
	end
})

-- Bucket flowing Lava to normal on contact with lava or water
minetest.register_abm({
	label = "Lava Flowing normalization",
	nodenames = {Liquid_name.lava.bucket_flowing},
	neighbors = {Liquid_name.lava.source,
				Liquid_name.lava.flowing,
				Liquid_name.water.source,
				Liquid_name.water.flowing,
				Liquid_name.water.river_source,
				Liquid_name.water.river_flowing,
				Liquid_name.water.bucket_source},
	interval = 1.0, -- Run every 1 second
	chance = 1, -- Select every 1 nod
	action = function(pos, node, active_object_count, active_object_count_wider)
			local pos = {x = pos.x, y = pos.y, z = pos.z}
			minetest.set_node(pos, {name = Liquid_name.lava.flowing})
	end
})
