package d2fonts

import (
	"crypto/sha256"
	"fmt"

	"github.com/d2lang/d2/d2renderers/internal/fontface"
)

type bundledFaceSpec struct {
	font   Font
	digest [sha256.Size]byte
}

var bundledFaceSpecs = []bundledFaceSpec{
	{Font{Family: SourceSansPro, Style: FONT_STYLE_REGULAR}, [sha256.Size]byte{0xa4, 0xc0, 0x7a, 0x3a, 0x09, 0xed, 0xc7, 0x1a, 0x05, 0x3d, 0x3e, 0xd7, 0x8d, 0x69, 0xff, 0x1a, 0xd4, 0x2f, 0x2f, 0x1f, 0x6b, 0x9f, 0x11, 0x09, 0x0b, 0xde, 0x7c, 0xf6, 0x18, 0x35, 0x5c, 0x78}},
	{Font{Family: SourceSansPro, Style: FONT_STYLE_BOLD}, [sha256.Size]byte{0xf2, 0x05, 0xfb, 0x64, 0xaf, 0x91, 0x7c, 0x37, 0xb6, 0x0b, 0x64, 0xa9, 0x5e, 0x0e, 0xac, 0x3a, 0x9e, 0x5b, 0xb9, 0x84, 0x9f, 0xa0, 0x7c, 0x2f, 0x2c, 0x2a, 0x04, 0x63, 0xd5, 0x55, 0x89, 0xe8}},
	{Font{Family: SourceSansPro, Style: FONT_STYLE_SEMIBOLD}, [sha256.Size]byte{0x72, 0xbf, 0x9a, 0xa2, 0xe7, 0x8b, 0x8c, 0x78, 0x44, 0x4a, 0xe7, 0x4b, 0x6b, 0xe0, 0x94, 0x11, 0xd5, 0xfb, 0x5b, 0xf0, 0xeb, 0x9b, 0xcd, 0xfd, 0x47, 0x08, 0x2c, 0xc5, 0xd9, 0xb5, 0x36, 0xdf}},
	{Font{Family: SourceSansPro, Style: FONT_STYLE_ITALIC}, [sha256.Size]byte{0xbd, 0x77, 0x18, 0x1e, 0x59, 0xa8, 0xf0, 0xf3, 0xea, 0x2b, 0xae, 0x01, 0xba, 0x13, 0x10, 0x2e, 0x4c, 0xd8, 0xf3, 0xb4, 0xce, 0x3a, 0x7f, 0xa2, 0x9b, 0xe4, 0xbd, 0xa9, 0xb3, 0xc9, 0xfa, 0x9c}},
	{Font{Family: SourceCodePro, Style: FONT_STYLE_REGULAR}, [sha256.Size]byte{0xf1, 0x44, 0x13, 0x7f, 0x55, 0x78, 0x05, 0xc7, 0x32, 0x7f, 0xc4, 0xb1, 0x4d, 0x1d, 0x73, 0x0f, 0x6e, 0x18, 0x22, 0xe0, 0x12, 0x41, 0x70, 0x25, 0x1f, 0xf1, 0xbc, 0xd7, 0x23, 0xa6, 0x93, 0xf1}},
	{Font{Family: SourceCodePro, Style: FONT_STYLE_BOLD}, [sha256.Size]byte{0xc2, 0xaa, 0xbd, 0x38, 0x30, 0xbd, 0x19, 0x42, 0x06, 0x70, 0xa3, 0x95, 0xc4, 0xdf, 0x30, 0x59, 0x6e, 0x95, 0x0c, 0x53, 0xf4, 0x61, 0x64, 0x45, 0x10, 0x7a, 0x11, 0x40, 0x46, 0xb9, 0xe6, 0x18}},
	{Font{Family: SourceCodePro, Style: FONT_STYLE_SEMIBOLD}, [sha256.Size]byte{0x1b, 0x61, 0xc9, 0x80, 0xd7, 0x5d, 0xe8, 0x21, 0x62, 0x28, 0xa2, 0x60, 0x3b, 0xca, 0xbe, 0x51, 0x39, 0x40, 0x13, 0x45, 0x2d, 0xf2, 0x6a, 0x04, 0xc9, 0x93, 0x26, 0xbb, 0xc0, 0x35, 0xc5, 0xa5}},
	{Font{Family: SourceCodePro, Style: FONT_STYLE_ITALIC}, [sha256.Size]byte{0xc3, 0x3a, 0xca, 0x4e, 0x40, 0x83, 0x60, 0x61, 0xac, 0xb3, 0x4f, 0x47, 0x65, 0x3d, 0xb9, 0x00, 0xf1, 0x03, 0x0c, 0x82, 0x31, 0x0a, 0x2e, 0xd0, 0x8a, 0x6d, 0xcb, 0xf3, 0xce, 0xc3, 0x37, 0x05}},
	{Font{Family: HandDrawn, Style: FONT_STYLE_REGULAR}, [sha256.Size]byte{0x3d, 0x2e, 0x49, 0x38, 0xc4, 0x8d, 0xca, 0x04, 0x84, 0xbf, 0xf9, 0xb7, 0x46, 0xd0, 0x93, 0x50, 0x48, 0x54, 0x80, 0x72, 0xeb, 0xb1, 0xa8, 0x9b, 0xc3, 0x93, 0x3a, 0x65, 0x54, 0x0e, 0x7b, 0x98}},
	{Font{Family: HandDrawn, Style: FONT_STYLE_BOLD}, [sha256.Size]byte{0x92, 0x47, 0x8c, 0xf4, 0xc0, 0x74, 0x57, 0x18, 0x1b, 0x46, 0xea, 0x7d, 0x6a, 0x55, 0x35, 0xaf, 0x98, 0x4c, 0x9f, 0x05, 0xfc, 0xcb, 0xef, 0xc8, 0xa7, 0xea, 0x34, 0x3e, 0xc6, 0x30, 0xd7, 0x11}},
}

func registerBundledFontFaces() error {
	for _, spec := range bundledFaceSpecs {
		data, ok := FontFaces.Lookup(spec.font)
		if !ok || len(data) == 0 {
			return fmt.Errorf("bundled font %s/%s is not loaded", spec.font.Family, spec.font.Style)
		}
		// FontFaces is a public, mutable registry. Registration retains only
		// authenticated metadata; the parser registry takes a private copy lazily
		// if this face is actually used by the raster pipeline.
		if err := fontface.RegisterBundledFace(data, spec.digest); err != nil {
			return fmt.Errorf("register bundled font %s/%s: %w", spec.font.Family, spec.font.Style, err)
		}
	}
	return nil
}
