package main

import (
	"fmt"
	"os"
	"runtime"

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

// All Hiragana characters
var hiragana = []struct {
	character rune
	romaji    string
}{
	// Basic vowels
	{'あ', "a"}, {'い', "i"}, {'う', "u"}, {'え', "e"}, {'お', "o"},
	// K-row
	{'か', "ka"}, {'き', "ki"}, {'く', "ku"}, {'け', "ke"}, {'こ', "ko"},
	// S-row
	{'さ', "sa"}, {'し', "shi"}, {'す', "su"}, {'せ', "se"}, {'そ', "so"},
	// T-row
	{'た', "ta"}, {'ち', "chi"}, {'つ', "tsu"}, {'て', "te"}, {'と', "to"},
	// N-row
	{'な', "na"}, {'に', "ni"}, {'ぬ', "nu"}, {'ね', "ne"}, {'の', "no"},
	// H-row
	{'は', "ha"}, {'ひ', "hi"}, {'ふ', "fu"}, {'へ', "he"}, {'ほ', "ho"},
	// M-row
	{'ま', "ma"}, {'み', "mi"}, {'む', "mu"}, {'め', "me"}, {'も', "mo"},
	// Y-row
	{'や', "ya"}, {'ゆ', "yu"}, {'よ', "yo"},
	// R-row
	{'ら', "ra"}, {'り', "ri"}, {'る', "ru"}, {'れ', "re"}, {'ろ', "ro"},
	// W-row
	{'わ', "wa"}, {'ゐ', "wi"}, {'ゑ', "we"}, {'を', "wo"},
	// N
	{'ん', "n"},
	// Dakuten (voiced consonants)
	{'が', "ga"}, {'ぎ', "gi"}, {'ぐ', "gu"}, {'げ', "ge"}, {'ご', "go"},
	{'ざ', "za"}, {'じ', "ji"}, {'ず', "zu"}, {'ぜ', "ze"}, {'ぞ', "zo"},
	{'だ', "da"}, {'ぢ', "di"}, {'づ', "du"}, {'で', "de"}, {'ど', "do"},
	{'ば', "ba"}, {'び', "bi"}, {'ぶ', "bu"}, {'べ', "be"}, {'ぼ', "bo"},
	// Handakuten (semi-voiced consonants)
	{'ぱ', "pa"}, {'ぴ', "pi"}, {'ぷ', "pu"}, {'ぺ', "pe"}, {'ぽ', "po"},
	// Small y-characters (for combinations)
	{'ゃ', "ya"}, {'ゅ', "yu"}, {'ょ', "yo"},
}

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

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

	// Load a font that supports Japanese
	// Try to find a system font
	font := rl.GetFontDefault()
	
	// Try to load a Japanese font
	fontPaths := []string{
		"/System/Library/Fonts/Hiragino Sans GB.ttc",
		"/System/Library/Fonts/Arial Unicode.ttf",
		"/Library/Fonts/Arial Unicode.ttf",
		"/usr/share/fonts/truetype/noto/NotoSansJP-Regular.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 Japanese font")
	}

	// Create image for each hiragana
	for _, h := range hiragana {
		// Create a render texture
		texture := rl.LoadRenderTexture(256, 256)
		
		rl.BeginTextureMode(texture)
			rl.ClearBackground(rl.White)
		
		// Draw the hiragana character
		char := string(h.character)
		charWidth := rl.MeasureText(char, 128)
		charX := int32((256 - charWidth) / 2)
		charY := int32(80)
		rl.DrawText(char, charX, charY, 128, rl.Black)
		
		// Draw romaji at the bottom
		romajiWidth := rl.MeasureText(h.romaji, 32)
		romajiX := int32((256 - romajiWidth) / 2)
		romajiY := int32(200)
		rl.DrawText(h.romaji, romajiX, romajiY, 32, rl.Blue)
		
		rl.EndTextureMode()
		
		// Convert render texture to image
		img := rl.LoadImageFromTexture(texture.Texture)
		
		// Save as PNG
		filename := "images/" + char + "_" + h.romaji + ".png"
		rl.ExportImage(*img, filename)
		
		// Cleanup
		rl.UnloadImage(img)
		rl.UnloadRenderTexture(texture)
		
		fmt.Println("Generated:", filename)
	}

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