local gem = sorcery.lib.image('default_diamond_block.png')
local amethyst = gem:multiply(sorcery.lib.color(sorcery.data.gems.amethyst.tone))
local emerald = gem:multiply(sorcery.lib.color(sorcery.data.gems.emerald.tone))
local hitbox = {
	type = 'fixed';
	fixed = {
		-0.5, -0.5, -0.4;
		0.5, 1.5, 0.4;
	};
}

local update_inv = function(pos)
	minetest.get_node_timer(pos):start(60)
end

minetest.register_node('sorcery:harvester', {
	description = 'Harvester';
	drawtype = 'mesh';
	paramtype = 'light';
	paramtype2 = 'facedir';
	mesh = 'sorcery-harvester.obj';
	after_dig_node = sorcery.lib.node.purge_container;
	groups = { cracky = 2; oddly_breakable_by_hand = 1; sorcery_magitech = 1; };
	sunlight_propagates = true;
	selection_box = hitbox;
	collision_box = hitbox;
	tiles = {
		amethyst:render();
		'default_copper_block.png';
		'default_stone.png';
		'default_gold_block.png';
		'default_tin_block.png';
		emerald:render();
	};

	on_timer = function(pos,elapse)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		if inv:is_empty('charge') then return false end

		local put_in_hopper = sorcery.lib.node.discharger(pos)
		local discharge = function(item,idx)
			inv:set_stack('charge',put_in_hopper(item))
		end
		
		local ley = sorcery.ley.estimate(pos)
		local charged = false
		for i=1,inv:get_size('charge') do
			local curve = function(x) return ((x*10)^2)/10 end
			local item = inv:get_stack('charge',i)
			if minetest.get_item_group(item:get_name(), 'sorcery_wand') ~= 0 then
				local mese = 1
				if sorcery.wands.util.getproto(item).gem == 'mese' then
					mese = 1.5 end
				local repair_per_tick = (65536 / 80) * curve(ley.force) * mese
				local spell = item:get_meta():get_string('sorcery_wand_spell')
				if spell == '' then goto skip end
				local aff = sorcery.data.spells[spell].leytype
				if aff == ley.aff[1] or aff == ley.aff[2] then
					repair_per_tick = repair_per_tick * 2 end
				item:set_wear(math.max(0,item:get_wear() - repair_per_tick * (elapse / 60)))
				if item:get_wear() == 0 then item = put_in_hopper(item) end
			else 
				local e = sorcery.enchant.get(item)
				if #e.spells == 0 then goto skip end -- item is not magical
				local mat = sorcery.enchant.getsubj(item)
				if e.energy < mat.data.maxenergy then
					local energy_per_tick = (100 * curve(ley.force)) + ((mat.data.energysource or 0)*2)
					e.energy = math.min(e.energy + energy_per_tick, mat.data.maxenergy)
					sorcery.enchant.set(item,e,true)
				else discharge(item,i) goto skip end -- already fully charged
			end
			-- print('repair cycle! repairing item'..item:get_name()..' by',repair_per_tick * (elapse / 60))
			inv:set_stack('charge',i,item)
			charged = true
		::skip::end

		return charged
	end;

	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		inv:set_size('charge', 3)
		meta:set_string('infotext','Harvester')
		meta:set_string('formspec', [[
			size[8,5]
			list[context;charge;2.5,0;3,1;]
			list[current_player;main;0,1.3;8,4;]
			listring[]
		]])
	end;
	on_metadata_inventory_put = update_inv;
	on_metadata_inventory_move = update_inv;

	_sorcery = {
		recipe = {
			note = "Standalone recharger for wands, amulets, and enchanted tools that draws on the ambient force from the local leylines";
		};
	};
})

minetest.register_craftitem('sorcery:harvester_receptacle', {
	description = 'Harvester Receptacle';
	inventory_image = 'sorcery_harvester_receptacle.png';
})

minetest.register_craft {
	output = 'sorcery:harvester_receptacle';
	recipe = {
		{'','default:stone','default:copper_ingot'};
		{'','default:gold_ingot',''};
		{'stairs:slab_stone','stairs:slab_stone','stairs:slab_stone'};
	};
}

minetest.register_craftitem('sorcery:accumulator', {
	description = 'Accumulator';
	inventory_image = 'sorcery_accumulator.png';
	_sorcery = {
		recipe = {
			info = 'Captures and channels ambient ley current';
		};
	};
})

minetest.register_craft {
	output = 'sorcery:accumulator';
	recipe = {
		{'default:tin_ingot', 'default:tin_ingot', 'default:tin_ingot'};
		{'sorcery:gem_amethyst','sorcery:gem_emerald','sorcery:gem_amethyst'};
		{'default:copper_ingot', 'default:copper_ingot', 'default:copper_ingot'};
	};
}
minetest.register_craft {
	output = 'sorcery:harvester';
	recipe = {
		{'sorcery:accumulator'};
		{'default:gold_ingot'};
		{'sorcery:harvester_receptacle'};
	};
}
