local world = starlit.world
local lib = starlit.mod.lib

local function dropCat(a,b)
	local function strdrop(s)
		if type(s) == 'string' then
			return {max_items = 1; items = {{items={s}}}}
		else return s end
	end
	a = strdrop(a)
	b = strdrop(b)
	return {
		max_items = a.max_items + b.max_items;
		items = lib.tbl.append(a.items, b.items);
	}
end

local function simpleDrop(rarity, what)
	return {
		max_items = 1;
		items = {
			{rarity = rarity, items = {what}};
		};
	};
end

local function biomeGrass(biomes, seed)
	return function(def)
		world.ecology.plants.link(def.id, {
			stages = {{
				tex = def.tex;
				drop = simpleDrop(4, 'starlit_eco:fiber');
			}};
			decoration = {
				deco_type = 'simple';
				place_on = 'starlit:greengraze';
				biomes = biomes;
				noise_params = {
					offset = def.ofs;
					spread = vector.new(10,10,10);
					seed = seed;
					octaves = 3;
				};
			}
		})
	end
end

do gg = biomeGrass({'starlit:steppe', 'starlit:forest'}, 0x5e8fa0)
	gg {
		id = 'starlit_eco:greengraze_low';
		name = 'Low Greengraze';
		tex = 'starlit-eco-plant-grass-sprig.png';
		ofs = -.3;
	}
	gg {
		id = 'starlit_eco:greengraze_high';
		name = 'High Greengraze';
		tex = 'starlit-eco-plant-grass-high.png';
		ofs = -.5;
	}
	gg {
		id = 'starlit_eco:greengraze_tall';
		name = 'Tall Greengraze';
		tex = 'starlit-eco-plant-grass-tall.png';
		ofs = -.7;
	}
end

local function stalkPlant(def)
	local function stage(s, drops, swap)
		return {
			tex = lib.image(string.format('starlit-eco-plant-stalk%s.png',s)):colorize(def.color);
			drop = drops;
			swap = swap;
		}
	end
	local function plantMatter(opts)
		local dps = {
			seed = def.seed;
			fiber = def.fiber;
			leaf = def.leaf and def.leaf.drop or nil;
			berry = def.berries and def.berries.drop or nil;
		}
		local t = {max_items=0, items={}}
		for k,v in pairs(opts) do
			if dps[v] then t = dropCat(dps[v], t) end
		end
		return t
	end

	local fg
	local stages = {
		stage('-grow-1', '');
		stage('-grow-2', plantMatter{'seed'});
		stage('-grow-3', plantMatter{'seed','fiber'});
		stage('', plantMatter{'seed','seed','fiber'});
	};
	if def.leaf then
		local ps = stage('', plantMatter{'seed','seed','seed','fiber','leaf'})
		ps.tex = ps.tex .. lib.image('starlit-eco-plant-stalk-petals.png'):shift(def.leaf.color)
		table.insert(stages, ps)
	end
	if def.berries then
		local ps = lib.image.clone(stages[#stages])
		ps.tex = ps.tex:blit(lib.image('starlit-eco-plant-stalk-berries.png'):shift(def.berries.color))
		ps.drop = def.berries.drop;
		ps.swap = #stages
		table.insert(stages, ps)
	end

	if def.biolum then for i,v in ipairs(stages) do
		v.biolum = math.floor(def.biolum * (i/#stages))
	end end

	world.ecology.plants.link(def.id, {
		name = def.name;
		stages = stages;
		decoration = def.decoration;
		meshOpt = 3;
	})
end


function stalkPlantAuto(def)
	local id = def.id
	local id_berries = def.id .. '_berry'
	local id_seed = def.id .. '_seed'

	local p = lib.tbl.proto({}, def)
	if def.berries then
		local bdef = lib.tbl.defaults({
			name = def.name .. ' Berry';
			tex = lib.image('starlit-eco-plant-berry-bunch.png'):shift(def.berries.color or def.color):render();
			color = def.color;
		}, def.berries)
		bdef.name = bdef.name
		starlit.item.food.link(id_berries, bdef)
		p.berries = {
			color = def.berries.color;
			drop = id_berries;
		}
	end

	if def.seed then
		local sdef = lib.tbl.defaults({
			name = def.name .. ' Seed';
			tex = lib.image('starlit-eco-plant-seeds.png'):shift(def.seed.color or def.color):render();
			color = def.color;
			grow = {kind = 'plant', id = id};
		}, def.seed)

		starlit.item.seed.link(id_seed, sdef)
		p.seed = id_seed
	end

	return stalkPlant(p)
end

stalkPlantAuto {
	id = 'starlit_eco:moondrop';
	name = "Moondrop";
	fiber = simpleDrop(2, 'starlit_eco:fiber');
	seed = {};
	color = lib.color(.5, .7, 1);
	biolum = 5;
	leaf = {
		color = lib.color(.6, .8, .8);
		drop = simpleDrop(2, 'starlit_eco:moondrop_petal');
	};
	berries = {
		desc = "The fruits of the moondrop are not very nutritious, but their peculiar sweet-sour flavor profile makes them one of Farthest Shadow's great delicacies";
		color = lib.color(.5,0,.1);
		mass = 1;
		impact = starlit.type.impact {
			nutrition = 10;
			hydration = 0.05;
			taste = 1*60;
		};
	};
	decoration = {
		place_on = 'starlit:greengraze';
		fill_ratio = 0.03;
		biomes = {'starlit:steppe', 'starlit:forest'};
		y_min = 10;
		y_max = 100;
	};
}

stalkPlantAuto {
	id = 'starlit_eco:dustrose';
	name = "Dust Rose";
	fiber = simpleDrop(2, 'starlit_eco:fiber');
	seed = {};
	color = lib.color(.9, .8, .3);
	leaf = {
		color = lib.color(.7, .4, .8);
		drop = simpleDrop(2, 'starlit_eco:dustrose_petal');
	};
	decoration = {
		place_on = 'starlit:greengraze';
		fill_ratio = 0.03;
		biomes = {'starlit:forest'};
		y_min = 0;
		y_max = 50;
	};
}

stalkPlantAuto {
	id = 'starlit_eco:harrowstalk';
	name = "Harrowstalk";
	fiber = simpleDrop(2, 'starlit_eco:fiber');
	seed = {};
	color = lib.color(.3, .2, .1);
	decoration = {
		place_on = 'starlit:sand';
		fill_ratio = 0.03;
		biomes = {'starlit:desert'};
		y_min = 0;
		y_max = 400;
	};
}
