local F = starlit.item.food
local lib = starlit.mod.lib

function F.impact(stack,f)
	if stack and not f then f = stack:get_definition()._starlit.food end
	local impact = f.impact and starlit.type.impact.clone(f.impact) or starlit.type.impact{}

	if impact.taste then
		local aff = impact.taste > 0 and 'good' or 'bad'
		table.insert(impact, {'taste', aff, {'morale', impact.taste}})
	end

	if stack then
		impact = impact:effective(stack)
	end

	return impact
end

local function foodTip(stack, f)
	local impact = F.impact(stack,f)
	local props = impact:propTable()

	if f.mass then
		table.insert(props, {
			title='Mass', affinity='info';
			desc = lib.math.si('g', f.mass, nil, nil, 2);
		})
	end

	return starlit.ui.tooltip {
		title = f.name;
		desc = f.desc;
		props = props;
		color = f.color;
	};
end

F.foreach('starlit:gen-food', {}, function(id, f)
	core.register_item(id, {
		type = f.itemType or 'craft';
		inventory_image = f.tex;
		short_description = f.name;
		description = foodTip(nil, f);
		on_use = function(st, luser)
			local user = starlit.activeUsers[luser:get_player_name()]
			st:take_item(1)
			local imp = F.impact(st,f)
			imp:apply(user)
			user:suitSound 'starlit-success' -- FIXME need proper eating sound
			return st
		end;
		_starlit = {
			food = f;
			recover = f.recover;
			mass = f.mass;
		};
	})
end)

starlit.item.seed.foreach('starlit:gen-seed', {}, function(id, s)
	core.register_item(id, {
		type = 'none';
		inventory_image = s.tex;
		short_description = s.name;
		description = starlit.ui.tooltip {
			title = s.name;
			color = s.color;
			desc = s.desc;
		};
		on_place = function()
			-- FIXME sow
		end;
		_starlit = {
			seed = s;
			mass = s.mass;
		};
	})
end)