-- [ʞ] fab.lua
--  ~ lexi hale <lexi@hale.su>
--  🄯 EUPL1.2
--  ? fabrication spec class
--    a type.fab supports two operators:
--
--    + used for compounding recipes. that is,
--			a+b = compose a new spec from the spec parts a and b.
--      this is used e.g. for creating tier-based
--      fabspecs.
--
--    * used for determining quantities. that is,
--			f*x = spec to make x instances of f
--
--    new fab fields must be defined in starlit.type.fab.fields.
--    this maps a name to fn(a,b,n) -> quant, where a is the first
--    argument, b is a compounding amount, and n is a quantity of
--    items to produce. fields that are unnamed will be underwritten

local function fQuant(a,b,n) return ((a or 0)+(b or 0))*n end
local function fFac  (a,b,n)
	if a == nil and b == nil then return nil end
	local f if a == nil or b == nil then
		f = a or b
	else
		f = (a or 1)*(b or 1)
	end
	return f*n
end
local function fReq  (a,b,n) return a or b         end
local function fFlag (a,b,n) return a and b        end
local function fSize (a,b,n) return math.max(a,b)  end

local F = string.format
local lib = starlit.mod.lib

local fields = {
	-- fabrication eligibility will be determined by which kinds
	-- of input a particular fabricator can introduce. e.g. a
	-- printer with a  but no cache can only print items whose
	-- recipe only names elements as ingredients
	element = {
		name = {"element", "elements"};
		string = function(x, n, long)
			local el = starlit.world.material.element.db[x]
			return lib.math.si('g', n) .. ' ' .. ((not long and el.sym) or el.name)
		end;
		image = function(x, n)
			return string.format('starlit-element-%s.png', x)
		end;
		op = fQuant;
	};
	metal ={
		name = {"metal", "metals"};
		string = function(x, n)
			local met = starlit.world.material.metal.db[x]
			return lib.math.si('g', n) .. ' ' .. met.name
		end;
		image = function(x, n)
			local met = starlit.world.material.metal.db[x]
			return ItemStack(met.form.ingot):get_definition().inventory_image
		end;
		op = fQuant;
	};
	liquid = {
		name = {"liquid", "liquids"};
		string = function(x, n)
			local liq = starlit.world.material.liquid.db[x]
			return lib.math.si('L', n) .. ' ' .. liq.name
		end;
		op = fQuant;
	};
	gas = {
		name = {"gas", "gasses"};
		string = function(x, n)
			local gas = starlit.world.material.gas.db[x]
			return lib.math.si('g', n) .. ' ' .. gas.name
		end;
		op = fQuant;
	};
-- 	crystal = {
-- 		op = fQuant;
-- 	};
	item = {
		name = {"item", "items"};
		string = function(x, n)
			local i = minetest.registered_items[x]
			return tostring(n) .. 'x ' .. i.short_description
		end;
	};

	-- factors

	cost = {op=fFac}; -- units vary
	time = {op=fFac}; -- (s)
		-- print: base printing time
	size = {op=fSize};
		-- printBay: size of the printer bay necessary to produce the item
	req  = {op=fReq};
	flag = {op=fFlag}; -- means that can be used to produce the item & misc flags
		-- print: allow production with a printer
		-- smelt: allow production with a smelter
	-- all else defaults to underwrite
}

local order = {
	'element', 'metal', 'liquid', 'gas', 'item'
}

local lib = starlit.mod.lib

local fab fab = lib.class {
	__name = 'starlit:fab';
	
	fields = fields;
	order = order;
	construct = function(q) return q end;
	__index = {
		elementalize = function(self)
			local e = fab {element = self.element or {}}
			for _, kind in pairs {'metal', 'gas', 'liquid'} do
				for m,mass in pairs(self[kind] or {}) do
					local mc = starlit.world.material[kind][m].composition
					e = e + mc:elementalize()*mass
				end
			end
			return e
		end;

		elementSeq = function(self)
			local el = {}
			local em = self.element
			local s = 0
			local eldb = starlit.world.material.element.db
			for k in pairs(em) do table.insert(el, k) s=s+eldb[k].n end
			table.sort(el, function(a,b)
				return eldb[a].n > eldb[b].n
			end)
			return el, em, s
		end;

		formula = function(self)
			local ts,f=0
			if self.element then
				f = {}
				local el, em, s = self:elementSeq()
				local eldb = starlit.world.material.element.db
				for i, e in ipairs(el) do
					local sym, n = eldb[e].sym, em[e]
					if n > 0 then
						table.insert(f, string.format("%s%s",
							sym, n>1 and lib.str.nIdx(n) or ''))
					end
				end
				f = table.concat(f)
				ts = ts + s
			end

			local sub = {}
			for _, w in pairs {'metal', 'gas', 'liquid'} do
				if self[w] then
					local mdb = starlit.world.material[w].db
					for k, amt in pairs(self[w]) do
						local mf, s = mdb[k].composition:formula()
						if amt > 0 then table.insert(sub, {
							f = string.format("(%s)%s",mf,
								lib.str.nIdx(amt));
							s = s;
						}) end
						ts = ts + s*amt
					end
				end
			end
			table.sort(sub, function(a,b) return a.s > b.s end)
			local fml = {}
			for i, v in ipairs(sub) do fml[i] = v.f end
			if f then table.insert(fml, f) end
			fml = table.concat(fml, ' + ')

			return fml, ts
		end;

		visualize = function(self)
			local all = {}
			for i,o in ipairs(order) do
				local t = {}
				if self[o] then
					for mat,amt in pairs(self[o]) do
						local v = {}
						v.id = mat
						v.n = amt
						if fields[o].string then
							v.label = fields[o].string(mat,amt,true)
						end
						if fields[o].image then
							v.img = fields[o].image(mat,amt)
						end
						table.insert(t,v)
					end
				end
				if fields[o].sort then
					table.sort(t, function(a,b) return fields[o].sort(a.id, b.id) end)
				end
				if next(t) then table.insert(all, {
					id=o, list=t;
					header=fields[o].name[t[2] and 2 or 1];
				}) end
			end
			return all
		end;
	};

	__tostring = function(self)
		local t = {}
		for i,o in ipairs(order) do
			if self[o] and fields[o].string then
				for mat,amt in pairs(self[o]) do
					if amt > 0 then
						table.insert(t, fields[o].string(mat, amt))
					end
				end
			end
		end
		return table.concat(t, ", ")
	end;

	__add = function(a,b)
		local new = fab {}
		for cat, vals in pairs(a) do
			new[cat] = lib.tbl.copy(vals)
		end
		for cat, vals in pairs(b) do
			if not new[cat] then
				new[cat] = lib.tbl.copy(vals)
			else
				local f = fields[cat].op
				for k,v in pairs(vals) do
					local n = f(new[cat][k], v, 1)
					new[cat][k] = n > 0 and n or nil
				end
			end
		end
		return new
	end;

	__mul = function(x,n)
		local new = fab {}
		for cat, vals in pairs(x) do
			new[cat] = {}
			local f = fields[cat].op
			for k,v in pairs(vals) do
				local num = f(v,nil,n)
				new[cat][k] = num > 0 and num or nil
			end
		end
		return new
	end;

	__div = function(x,n)
		return x * (1/n)
	end;
}

starlit.type.fab = fab
