package speiger.src.coreengine.rendering.gui.font;

import java.util.List;

import speiger.src.collections.ints.maps.impl.hash.Int2ObjectOpenHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap;
import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.rendering.gui.font.glyth.Glyth;
import speiger.src.coreengine.rendering.gui.font.glyth.GlythData;
import speiger.src.coreengine.rendering.gui.font.providers.IFontProvider;

public class FontGroup {
	private static final int BOLD_FLAG = 1<<31;
	AssetLocation locations;
	List<IFontProvider> providers;
	Int2ObjectMap<Glyth> bakedGlyths = new Int2ObjectOpenHashMap<>();
	Int2ObjectMap<GlythData> dataGlyths = new Int2ObjectOpenHashMap<>();
	
	public FontGroup(AssetLocation locations, List<IFontProvider> providers) {
		this.locations = locations;
		this.providers = providers;
	}
	
	public GlythData data(int codepoint) {
		return dataGlyths.computeIfAbsent(codepoint, this::compute);
	}
	
	public Glyth glyth(int codepoint, boolean bold) {
		return bakedGlyths.computeIfAbsent(codepoint | (bold ? 0 : BOLD_FLAG), this::bake);
	}
	
	private GlythData compute(int codepoint) {
		for(int i = 0,m=providers.size();i<m;i++) {
			GlythData data = providers.get(i).glythData(codepoint);
			if(data != null) return data;
		}
		return null;
	}
	
	private Glyth bake(int codepoint) {
		boolean bold = (codepoint & BOLD_FLAG) != 0;
		codepoint &= ~BOLD_FLAG;
		for(int i = 0,m=providers.size();i<m;i++) {
			GlythData data = providers.get(i).glythData(codepoint);
			if(data != null) return data.bake(bold);
		}
		return null;
	}
}