-- IndustrialTest
-- Copyright (C) 2024 mrkubax10

-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

local S=minetest.get_translator("industrialtest")

local rotaryMacerator={}
rotaryMacerator.opPower=60

rotaryMacerator.getFormspec=function(pos)
	local meta=minetest.get_meta(pos)
	local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100
	local maxSrcTime=meta:get_float("maxSrcTime")
	local srcPercent=maxSrcTime>0 and meta:get_float("srcTime")/maxSrcTime*100 or 0
	local rpm=meta:get_int("rpm")
	local formspec
	if industrialtest.mtgAvailable then
		formspec={
			"list[context;src;3.8,1.8;1,1]",
			"list[context;modifier;4.9,1.8;1,1]",
			(powerPercent>0 and "image[3.8,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]"
			 or "image[3.8,2.8;1,1;industrialtest_gui_electricity_bg.png]"),
			"list[context;powerStorage;3.8,3.9;1,1]",
			(srcPercent>0 and "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[lowpart:"..srcPercent..":gui_furnace_arrow_fg.png^[transformR270]"
			 or "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[transformR270]"),
			"list[context;dst;6,2.8;1,1;]",
			"list[context;upgrades;9,0.9;1,4]",
			"label[0.5,2.8;Speed: "..rpm.."]",
			"listring[context;src]",
			"listring[context;modifier]",
			"listring[context;powerStorage]",
			"listring[context;dst]",
			"listring[context;upgrades]"
		}
	elseif industrialtest.mclAvailable then
		formspec={
			"list[context;src;3.8,1.8;1,1]",
			mcl_formspec.get_itemslot_bg(3.8,1.8,1,1),
			"list[context;modifier;4.9,1.8;1,1]",
			mcl_formspec.get_itemslot_bg(4.9,1.8,1,1),
			(powerPercent>0 and "image[3.8,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]"
			 or "image[3.8,2.8;1,1;industrialtest_gui_electricity_bg.png]"),
			"list[context;powerStorage;3.8,3.9;1,1]",
			mcl_formspec.get_itemslot_bg(3.8,3.9,1,1),
			(srcPercent>0 and "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[lowpart:"..srcPercent..":gui_furnace_arrow_fg.png^[transformR270]"
			 or "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[transformR270]"),
			"list[context;dst;6,2.8;1,1;]",
			mcl_formspec.get_itemslot_bg(6,2.8,1,1),
			"list[context;upgrades;9,0.9;1,4]",
			mcl_formspec.get_itemslot_bg(9,0.9,1,4),
			"label[0.5,2.8;Speed: "..rpm.."]",
			"listring[context;src]",
			"listring[context;modifier]",
			"listring[context;powerStorage]",
			"listring[context;dst]",
			"listring[context;upgrades]"
		}
	end
	return table.concat(formspec,"")
end

rotaryMacerator.onConstruct=function(pos,meta,inv)
	inv:set_size("src",1)
	inv:set_size("modifier",1)
	inv:set_size("powerStorage",1)
	inv:set_size("dst",1)
	inv:set_size("upgrades",4)
	meta:set_int("rpm",0)
	meta:set_float("srcTime",0)
	meta:set_float("maxSrcTime",0)
end

rotaryMacerator.onTimer=function(pos,elapsed,meta,inv)
	local shouldRerunTimer=false
	local shouldUpdateFormspec=false
	local srcSlot=inv:get_stack("src",1)
	local modifierSlot=inv:get_stack("modifier",1)
	local dstSlot=inv:get_stack("dst",1)
	local powerAmount=meta:get_int("industrialtest.powerAmount")
	local rpm=meta:get_int("rpm")

	shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv)

	if rpm>0 then
		meta:set_int("rpm",math.max(rpm-1000,0))
		shouldRerunTimer=shouldRerunTimer or rpm>0
		shouldUpdateFormspec=true
	end

	if powerAmount>=rotaryMacerator.opPower and not srcSlot:is_empty() then
		local result=industrialtest.api.getMaceratorRecipeResult(srcSlot:get_name())
		if result then
			meta:set_float("srcTime",0)
			meta:set_float("maxSrcTime",result.time)
			minetest.swap_node(pos,{
				name="industrialtest:rotary_macerator_active",
				param2=minetest.get_node(pos).param2
			})
			minetest.get_node_timer(pos):start(industrialtest.updateDelay)
			return false,shouldUpdateFormspec
		end
	end
	
	return shouldRerunTimer,shouldUpdateFormspec
end

rotaryMacerator.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,count)
	if toList=="dst" then
		return 0
	end
	return count
end

rotaryMacerator.allowMetadataInventoryPut=function(pos,listname,index,stack)
	if listname=="dst" then
		return 0
	end
	return stack:get_count()
end

rotaryMacerator.onMetadataInventoryMove=function(pos)
	minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end

rotaryMacerator.onMetadataInventoryPut=function(pos)
	minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end

rotaryMacerator.onMetadataInventoryTake=function(pos,listname)
	if listname=="dst" then
		minetest.get_node_timer(pos):start(industrialtest.updateDelay)
	end
end

rotaryMacerator.activeOnTimer=function(pos,elapsed,meta,inv)
	local srcSlot=inv:get_stack("src",1)
	local modifierSlot=inv:get_stack("modifier",1)
	local dstSlot=inv:get_stack("dst",1)
	local powerAmount=meta:get_int("industrialtest.powerAmount")
	local rpm=meta:get_int("rpm")

	industrialtest.internal.chargeFromPowerStorageItem(meta,inv)

	if srcSlot:is_empty() or powerAmount<rotaryMacerator.opPower then
		meta:set_float("srcTime",0)
		meta:set_float("maxSrcTime",0)
		minetest.swap_node(pos,{
			name="industrialtest:rotary_macerator",
			param2=minetest.get_node(pos).param2
		})
		minetest.get_node_timer(pos):start(industrialtest.updateDelay)
		return false,true
	end

	local result
	local modified=false
	if not modifierSlot:is_empty() then
		result=industrialtest.api.getRotaryMaceratorModifier(srcSlot:get_name(),modifierSlot:get_name())
	end
	if result then
		modified=true
	else
		result=industrialtest.api.getMaceratorRecipeResult(srcSlot:get_name())
	end
	local resultStack=ItemStack(result.output)
	if not dstSlot:item_fits(resultStack) then
		meta:set_float("srcTime",0)
		meta:set_float("maxSrcTime",0)
		minetest.swap_node(pos,{
			name="industrialtest:rotary_macerator",
			param2=minetest.get_node(pos).param2
		})
		minetest.get_node_timer(pos):start(industrialtest.updateDelay)
		return false,true
	end
	meta:set_float("maxSrcTime",result.time)

	local srcTime=meta:get_float("srcTime")+elapsed*(1+rpm/7500)
	if srcTime>=meta:get_float("maxSrcTime") then
		local speed=industrialtest.api.getMachineSpeed(meta)
		local multiplier=1
		if srcSlot:get_count()>=speed then
			multiplier=speed
		end
		resultStack:set_count(resultStack:get_count()*multiplier)
		inv:add_item("dst",resultStack)
		meta:set_float("srcTime",0)
		meta:set_float("maxSrcTime",0)
		srcSlot:set_count(srcSlot:get_count()-multiplier)
		inv:set_stack("src",1,srcSlot)
		meta:set_int("rpm",math.min(rpm+750,7500))
		if modified then
			local modifierMeta=modifierSlot:get_meta()
			local uses=result.uses
			if modifierMeta:contains("uses") then
				uses=modifierMeta:get_int("uses")
			end
			uses=math.max(uses-1,0)
			if uses==0 then
				if result.modifierLeftover then
					modifierSlot:set_name(result.modifierLeftover)
				else
					modifierSlot:take_item(1)
				end
				uses=result.uses
			end
			if not modifierSlot:is_empty() and not result.modifierLeftover then
				modifierMeta:set_int("uses",uses)
			end
			inv:set_stack("modifier",1,modifierSlot)
		end
	else
		meta:set_float("srcTime",srcTime)
	end

	return true,true
end

industrialtest.internal.registerMachine({
	name="rotary_macerator",
	displayName=S("Rotary Macerator"),
	capacity=industrialtest.api.lvPowerFlow*2,
	getFormspec=rotaryMacerator.getFormspec,
	flow=industrialtest.api.lvPowerFlow,
	ioConfig="iiiiii",
	requiresWrench=true,
	registerActiveVariant=true,
	sounds="metal",
	powerSlots={"powerStorage"},
	storageSlots={"src","modifier","powerStorage","dst","upgrades"},
	groups={
		_industrialtest_hasPowerInput=1
	},
	customKeys={
		tiles={
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png^industrialtest_macerator_front.png"
		},
		paramtype2="facedir",
		legacy_facedir_simple=true
	},
	activeCustomKeys={
		tiles={
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png",
			"industrialtest_advanced_machine_block.png^industrialtest_macerator_front_active.png"
		}
	},
	onConstruct=rotaryMacerator.onConstruct,
	onTimer=rotaryMacerator.onTimer,
	allowMetadataInventoryMove=rotaryMacerator.allowMetadataInventoryMove,
	allowMetadataInventoryPut=rotaryMacerator.allowMetadataInventoryPut,
	onMetadataInventoryPut=rotaryMacerator.onMetadataInventoryPut,
	onMetadataInventoryMove=rotaryMacerator.onMetadataInventoryMove,
	onMetadataInventoryTake=rotaryMacerator.onMetadataInventoryTake,
	activeOnTimer=rotaryMacerator.activeOnTimer
})
minetest.register_craft({
	type="shaped",
	output="industrialtest:rotary_macerator",
	recipe={
		{"industrialtest:refined_iron_ingot","industrialtest:refined_iron_ingot","industrialtest:refined_iron_ingot"},
		{"industrialtest:refined_iron_ingot","industrialtest:macerator","industrialtest:refined_iron_ingot"},
		{"industrialtest:refined_iron_ingot","industrialtest:advanced_machine_block","industrialtest:refined_iron_ingot"}
	}
})