package alui

import (
	"opslag.de/schobers/allg5"
)

type Fonts struct {
	fonts map[string]*allg5.Font
}

type FontDescription struct {
	Path string
	Size int
	Name string
}

func NewFonts() *Fonts {
	return &Fonts{map[string]*allg5.Font{}}
}

func (f *Fonts) Destroy() {
	for _, font := range f.fonts {
		font.Destroy()
	}
	f.fonts = nil
}

func (f *Fonts) Load(path string, size int, name string) error {
	font, err := allg5.LoadTTFFont(path, size)
	if err != nil {
		return err
	}
	if old := f.fonts[name]; old != nil {
		old.Destroy()
	}
	f.fonts[name] = font
	return nil
}

func (f *Fonts) LoadFonts(descriptions ...FontDescription) error {
	for _, desc := range descriptions {
		err := f.Load(desc.Path, desc.Size, desc.Name)
		if err != nil {
			return err
		}
	}
	return nil
}

func (f *Fonts) Get(name string) *allg5.Font {
	if name == "" {
		return f.Get("default")
	}
	return f.fonts[name]
}