package font

import (
	"tetris/engine/graphics"
)

// TileFont represents a tile-based font where each character maps
// to a tile location (X, Y) in a tileset or texture atlas.
//
// The character-to-tile mapping is defined in the Charmap.
// If CaseInsensitive is true, uppercase characters ('A'–'Z')
// are automatically converted to lowercase before lookup.
type TileFont struct {
	Charmap map[rune]byte

	// CaseInsensitive forces all characters to lowercase before lookup.
	CaseInsensitive bool
}

// GetTile returns the Tile corresponding to the given character.
//
// If CaseInsensitive is enabled, uppercase ASCII characters are converted
// to lowercase before lookup. If the character is not found in the Charmap,
// GetTile returns nil.
//
// The resulting Tile assumes a fixed tile size of 8x8 pixels
func (f TileFont) GetTile(char rune) *graphics.Tile {
	// convert to lowercase
	if f.CaseInsensitive && char >= 'A' && char <= 'Z' {
		char = char + 0x20
	}

	if offset, found := f.Charmap[char]; found {
		return &graphics.Tile{
			Size: 8,
			X:    offset & 0xF,
			Y:    offset >> 4,
		}
	}
	return nil
}
