package main

import (
	"fmt"
	"os"
	"runtime"

	"github.com/gen2brain/raylib-go/raylib"
)

// Thai consonants (44 characters)
var thaiConsonants = []struct {
	character string
	name      string
}{
	{"ก", "Ko Kai"}, {"ข", "Kho Khai"}, {"ฃ", "Kho Khuat"}, {"ค", "Kho Khwai"}, {"ฅ", "Kho Khon"},
	{"ฆ", "Kho Rakhang"}, {"ง", "Ngo Ngai"}, {"จ", "Cho Chan"}, {"ฉ", "Cho Ching"}, {"ช", "Cho Chang"},
	{"ซ", "So So"}, {"ฌ", "Cho Che"}, {"ญ", "Yo Ying"}, {"ฎ", "Do Dek"}, {"ฏ", "To Tao"},
	{"ฐ", "To Thong"}, {"ฑ", "To Thung"}, {"ฒ", "To Thahan"}, {"ณ", "No Nen"}, {"ด", "Do Dek"},
	{"ต", "To Tao"}, {"ถ", "To Thung"}, {"ท", "To Thahan"}, {"ธ", "Tho Thong"}, {"น", "No Nu"},
	{"บ", "Bo Baimai"}, {"ป", "Po Pla"}, {"ผ", "Pho Phung"}, {"ฝ", "Fo Fa"}, {"พ", "Pho Phan"},
	{"ฟ", "Fo Fan"}, {"ภ", "Pho Samphao"}, {"ม", "Mo Ma"}, {"ย", "Yo Yak"}, {"ร", "Ro Rua"},
	{"ล", "Lo Ling"}, {"ว", "Wo Waen"}, {"ศ", "So Sala"}, {"ษ", "So Ruezi"}, {"ส", "So Suan"},
	{"ห", "Ho Hip"}, {"ฬ", "Lo Chula"}, {"อ", "O Ang"}, {"ฮ", "Ho Nokhuk"},
}

// Thai vowels (32 characters)
var thaiVowels = []struct {
	character string
	name      string
}{
	{"ะ", "Sara A"}, {"ั", "Sara Aa"}, {"า", "Sara Aa"}, {"ำ", "Sara Am"}, {"ิ", "Sara I"},
	{"ี", "Sara Ii"}, {"ึ", "Sara Ue"}, {"ื", "Sara Uee"}, {"ุ", "Sara U"}, {"ู", "Sara Uu"},
	{"เ", "Sara E"}, {"แ", "Sara Ae"}, {"โ", "Sara O"}, {"ใ", "Sara Ai Mai Malai"}, {"ไ", "Sara Ai Mai Muang"},
	{"ๅ", "Sara Ue"}, {"ๆ", "Sara Uee"},
}

// Thai numbers
var thaiNumbers = []struct {
	character string
	name      string
}{
	{"๐", "Zero"}, {"๑", "One"}, {"๒", "Two"}, {"๓", "Three"}, {"๔", "Four"},
	{"๕", "Five"}, {"๖", "Six"}, {"๗", "Seven"}, {"๘", "Eight"}, {"๙", "Nine"},
}

func main() {
	// Initialize raylib for font rendering
	runtime.LockOSThread()
	rl.InitWindow(1, 1, "Thai Generator")
	defer rl.CloseWindow()

	// Create directory for images
	if err := os.MkdirAll("images", 0755); err != nil {
		panic(err)
	}

	// Load a font that supports Thai
	font := rl.GetFontDefault()
	
	fontPaths := []string{
		"/System/Library/Fonts/Ayuthaya.ttc",
		"/System/Library/Fonts/Thonburi.ttc",
		"/Library/Fonts/Ayuthaya.ttc",
		"/usr/share/fonts/truetype/noto/NotoSansThai-Regular.ttf",
		"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
	}
	
	for _, path := range fontPaths {
		if _, err := os.Stat(path); err == nil {
			font = rl.LoadFont(path)
			if font != (rl.Font{}) {
				break
			}
		}
	}

	if font == (rl.Font{}) {
		font = rl.GetFontDefault()
		fmt.Println("Using default font")
	} else {
		fmt.Println("Loaded Thai font")
	}

	// All Thai characters
	allThai := append(thaiConsonants, thaiVowels...)
	allThai = append(allThai, thaiNumbers...)

	// Create image for each character
	for _, t := range allThai {
		// Create a render texture
		texture := rl.LoadRenderTexture(256, 256)
		
		rl.BeginTextureMode(texture)
			rl.ClearBackground(rl.White)
		
		// Draw the Thai character
		charWidth := rl.MeasureText(t.character, 128)
		charX := int32((256 - charWidth) / 2)
		charY := int32(80)
		rl.DrawText(t.character, charX, charY, 128, rl.Black)
		
		// Draw name at the bottom
		nameWidth := rl.MeasureText(t.name, 24)
		nameX := int32((256 - nameWidth) / 2)
		nameY := int32(200)
		rl.DrawText(t.name, nameX, nameY, 24, rl.Blue)
		
		rl.EndTextureMode()
		
		// Convert render texture to image
		img := rl.LoadImageFromTexture(texture.Texture)
		
		// Save as PNG
		filename := "images/" + t.character + "_" + t.name + ".png"
		rl.ExportImage(*img, filename)
		
		// Cleanup
		rl.UnloadImage(img)
		rl.UnloadRenderTexture(texture)
		
		fmt.Println("Generated:", filename)
	}

	fmt.Println("Finished generating all Thai images!")
}
