------------------------------------------------------------------------------------------------------------------------------
--// Classes


local event = {};

function event.new(t)
	local evnts = {};
	local self = setmetatable({},{
		__index = t;
		__newindex = function(tt, k, v)
			if (t[k] ~= v) then
				t[k] = v;
				if (type(evnts[k]) == "function") then
					evnts[k](v);
				end;
			end;
		end;
		__metatable = "The metatable is locked.";
	});
	
	function self:connect(k, f)
		evnts[k] = f;
	end;
	
	return self;
end;

local settings = {};

function settings.new(fontModule, attached, child)
	local self = setmetatable({}, {__index = settings});
	
	settings.child = child;
	settings.attached = attached;
	
	-- place data in new format for easy access
	self.information = fontModule.font.information;
	self.atlases = fontModule.atlases;
	self.styles = fontModule.font.styles;
	
	-- sort from least to greatest
	sort(self.information.sizes, function(a, b) return a > b; end);
	
	-- establish some settings variables
	self.style = self.information.styles[1];
	self.size = child.TextSize;
	
	-- failsafes
	for styleName, style in next, self.styles do
		-- characters that DNE
		for sizeName, size in next, style do
			setmetatable(size.characters, {
				__index = function(t, k)
					local k = tostring(k);
					local v = rawget(t, k)
					if (not v) then
						warn(k, "is not a valid character. Replaced with, \"" .. char(REPLACE) .. "\"");
						return rawget(t, tostring(REPLACE));
					end;
					return v;
				end;
			})
		end;
		-- sizes that DNE
		setmetatable(style, {
			__index = function(t, k)
				local k = tostring(k);
				local v = rawget(t, k);
				if (not v) then
					local closest = getClosestNumber(k, self.information.sizes);
					self.size = closest;
					child.TextSize = closest;
					warn(k, "is not a valid size. Using the closest size,", closest);
					return rawget(t, tostring(closest));
				end;
				return v;
			end;
		});
	end;
	-- styles that DNE
	setmetatable(self.styles, {
		__index = function(t, k)
			local v = rawget(t, k);
			if (not v) then 
				local nstyle = self.information.styles[1];
				self.style = nstyle;
				attached.Style = nstyle;
				warn(k, "is not a valid style. Using first style found", nstyle);
				return rawget(t, nstyle);
			end;
			return v;
		end;
	});
	
	return self;
end;

function settings:preload()
	for _, atlas in next, self.atlases do
		content:Preload(atlas);
	end;
end;
