package main

import (
	"fmt"
	"os"
	"runtime"

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

// Korean Hangul Jamo - Choseong (Initial consonants)
var koreanChoseong = []struct {
	character string
	name      string
}{
	{"ㄱ", "Giyeok"}, {"ㄲ", "SsangGiyeok"}, {"ㄴ", "Nieun"}, {"ㄷ", "Dieut"}, {"ㄸ", "SsangDieut"},
	{"ㄹ", "Rieul"}, {"ㅁ", "Mieum"}, {"ㅂ", "Pieup"}, {"ㅃ", "SsangPieup"}, {"ㅅ", "Siot"},
	{"ㅆ", "SsangSiot"}, {"ㅇ", "Ieung"}, {"ㅈ", "Jieut"}, {"ㅉ", "SsangJieut"}, {"ㅊ", "Chieut"},
	{"ㅋ", "Kieuk"}, {"ㅌ", "Tieut"}, {"ㅍ", "Pieup"}, {"ㅎ", "Hieut"},
}

// Korean Hangul Jamo - Jungseong (Medial vowels)
var koreanJungseong = []struct {
	character string
	name      string
}{
	{"ㅏ", "A"}, {"ㅐ", "Ae"}, {"ㅑ", "Ya"}, {"ㅒ", "Yae"}, {"ㅓ", "Eo"},
	{"ㅔ", "E"}, {"ㅕ", "Yeo"}, {"ㅖ", "Ye"}, {"ㅗ", "O"}, {"ㅘ", "Wa"},
	{"ㅙ", "Wae"}, {"ㅚ", "Oe"}, {"ㅛ", "Yo"}, {"ㅜ", "U"}, {"ㅝ", "Wo"},
	{"ㅞ", "We"}, {"ㅟ", "Wi"}, {"ㅠ", "Yu"}, {"ㅡ", "Eu"}, {"ㅢ", "Ui"},
	{"ㅣ", "I"},
}

// Korean Hangul Jamo - Jongseong (Final consonants)
var koreanJongseong = []struct {
	character string
	name      string
}{
	{"", "None"}, {"ㄱ", "Giyeok"}, {"ㄲ", "SsangGiyeok"}, {"ㄱㅅ", "Giyeok-Siot"}, {"ㄴ", "Nieun"},
	{"ㄵ", "Nieun-Cieut"}, {"ㄶ", "Nieun-Hieut"}, {"ㄷ", "Dieut"}, {"ㄹ", "Rieul"}, {"ㄺ", "Rieul-Giyeok"},
	{"ㄻ", "Rieul-Mieum"}, {"ㄼ", "Rieul-Pieup"}, {"ㄽ", "Rieul-Siot"}, {"ㄾ", "Rieul-Tieut"}, {"ㄿ", "Rieul-Phup"},
	{"ㅀ", "Rieul-Hieut"}, {"ㅁ", "Mieum"}, {"ㅂ", "Pieup"}, {"ㅄ", "Pieup-Siot"}, {"ㅅ", "Siot"},
	{"ㅆ", "SsangSiot"}, {"ㅇ", "Ieung"}, {"ㅈ", "Jieut"}, {"ㅊ", "Chieut"}, {"ㅋ", "Kieuk"},
	{"ㅌ", "Tieut"}, {"ㅍ", "Pieup"}, {"ㅎ", "Hieut"},
}

// Common Hangul syllables
var koreanSyllables = []struct {
	character string
	name      string
}{
	{"가", "Ga"}, {"각", "Gak"}, {"간", "Gan"}, {"갈", "Gal"}, {"감", "Gam"},
	{"갑", "Gap"}, {"강", "Gang"}, {"개", "Gae"}, {"객", "Gaek"}, {"건", "Geon"},
	{"걸", "Geol"}, {"검", "Geom"}, {"겁", "Geop"}, {"게", "Ge"}, {"격", "Gyeok"},
	{"견", "Gyeon"}, {"결", "Gyeol"}, {"겸", "Gyeom"}, {"겹", "Gyeop"}, {"경", "Gyeong"},
	{"계", "Gye"}, {"고", "Go"}, {"곡", "Gok"}, {"곤", "Gon"}, {"골", "Gol"},
	{"공", "Gong"}, {"과", "Gwa"}, {"관", "Gwan"}, {"괄", "Gwal"}, {"광", "Gwang"},
	{"귀", "Gwi"}, {"규", "Gyu"}, {"균", "Gyun"}, {"굴", "Gul"}, {"굼", "Gum"},
	{"국", "Guk"}, {"군", "Gun"}, {"굽", "Gup"}, {"굿", "Gut"}, {"그", "Geu"},
}

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

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

	// Load a font that supports Korean
	font := rl.GetFontDefault()
	
	fontPaths := []string{
		"/System/Library/Fonts/AppleGothic.ttc",
		"/System/Library/Fonts/AppleMyungjo.ttc",
		"/Library/Fonts/AppleGothic.ttc",
		"/usr/share/fonts/truetype/noto/NotoSansKR-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 Korean font")
	}

	// All Korean characters
	allKorean := append(koreanChoseong, koreanJungseong...)
	allKorean = append(allKorean, koreanJongseong...)
	allKorean = append(allKorean, koreanSyllables...)

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

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