-- TODO: accept model part for built-in UV management, automatic texture size
local mt={}
--- @class UVManager
UVManager = {
	step=vec(0,0),
	offset=vec(0,0),
	positions={},
	part=nil,
	dimensions=nil
}
mt.__index=UVManager
--- @return UVManager
--- @param step? Vector2 A vector representing the distance between UVs
--- @param offset? Vector2 A vector represnting the starting point for UVs, or nil
--- @param positions? table A dictionary of names and offset vectors
--- @param part? ModelPart Model part to manage
function UVManager.new(step, offset, positions, part)
	local t={}
	if step ~= nil then t.step=step end
	if offset ~= nil then t.offset=offset end
	if positions ~= nil then t.positions=positions end

	if part ~= nil then
		UVManager.setPart(t, part)
	end

	t=setmetatable(t, mt)
	return t
end

--- @param part ModelPart Model part to manage
function UVManager.setPart(self, part)
	self.part=part
	self.dimensions=part:getTextureSize()
end


---Get real UV from vec(x, y) or string representing preset
---@param input Vector2|string UV to get
function UVManager.getUV(self, input)
	local vect={}
	local stp=self.step
	local offset=self.offset
	assert(type(input)=="string" or type(input) == "Vector2")
	if type(input) == "string" then
		if self.positions[input] == nil then return nil end
		vect=self.positions[input]
	elseif type(input) == "Vector2" then
		-- vect=vectors.of(input)
		vect=input
	end
	local u=offset.x+(vect.x*stp.x)
	local v=offset.y+(vect.y*stp.y)
	if self.dimensions ~= nil then
		-- TODO override for my specific texture, replace this with matrix stuff
		-- (get rid of division once you figure out how setUVMatrix works)
		return vec(u/(self.dimensions.x/2), v/(self.dimensions.y/2))
	else
		return util.UV{u, v}
	end
end

function UVManager.setUV(self, input)
	if self.part == nil then return false end
	self.part:setUV(self:getUV(input))
end

return UVManager