package main

import (
	"runtime"

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

// Button represents a clickable button with animation state
type Button struct {
	Bounds   rl.Rectangle
	Text    string
	Color   rl.Color
	Hovered bool
	Clicked bool
	Scale   float32
	Action  string
}

// Screen represents different application screens
const (
	ScreenMenu      = "menu"
	ScreenThai      = "thai"
	ScreenKorean    = "korean"
	ScreenJapanese  = "japanese"
)

func main() {
	// Initialize window
	screenWidth := int32(800)
	screenHeight := int32(450)

	rl.InitWindow(screenWidth, screenHeight, "Learning Raylib in Go")
	defer rl.CloseWindow()

	// Set target FPS
	rl.SetTargetFPS(60)

	// Button dimensions and positions
	buttonWidth := int32(200)
	buttonHeight := int32(50)
	buttonSpacing := int32(20)
	startY := int32(screenHeight/2) - (buttonHeight*3 + buttonSpacing*2)/2

	// Create buttons
	thaiBtn := Button{
		Bounds: rl.Rectangle{
			X:      float32(screenWidth/2 - buttonWidth/2),
			Y:      float32(startY),
			Width:  float32(buttonWidth),
			Height: float32(buttonHeight),
		},
		Text:   "Thai",
		Color:  rl.LightGray,
		Scale:  1.0,
		Action: ScreenThai,
	}

	koreanBtn := Button{
		Bounds: rl.Rectangle{
			X:      float32(screenWidth/2 - buttonWidth/2),
			Y:      float32(startY + buttonHeight + buttonSpacing),
			Width:  float32(buttonWidth),
			Height: float32(buttonHeight),
		},
		Text:   "Korean",
		Color:  rl.LightGray,
		Scale:  1.0,
		Action: ScreenKorean,
	}

	japaneseBtn := Button{
		Bounds: rl.Rectangle{
			X:      float32(screenWidth/2 - buttonWidth/2),
			Y:      float32(startY + (buttonHeight+buttonSpacing)*2),
			Width:  float32(buttonWidth),
			Height: float32(buttonHeight),
		},
		Text:   "Japanese",
		Color:  rl.LightGray,
		Scale:  1.0,
		Action: ScreenJapanese,
	}

	buttons := []*Button{&thaiBtn, &koreanBtn, &japaneseBtn}

	// Current screen state
	currentScreen := ScreenMenu
	prevScreen := ScreenMenu

	// Main game loop
	for !rl.WindowShouldClose() {
		// Get mouse position
		mousePos := rl.GetMousePosition()

		// Handle screen transitions
		if currentScreen != prevScreen {
			// Reset button states when changing screens
			for _, btn := range buttons {
				btn.Clicked = false
				btn.Scale = 1.0
			}
			prevScreen = currentScreen
		}

		// Update button states and handle clicks
		for _, btn := range buttons {
			btn.Hovered = rl.CheckCollisionPointRec(mousePos, btn.Bounds)

			// Handle click animation
			if btn.Clicked {
				btn.Scale += 0.05
				if btn.Scale >= 1.1 {
					btn.Clicked = false
				}
			} else if btn.Scale > 1.0 {
				btn.Scale -= 0.05
				if btn.Scale < 1.0 {
					btn.Scale = 1.0
				}
			} else {
				btn.Scale = 1.0
			}

			// Handle click - only on menu screen
			if currentScreen == ScreenMenu && btn.Hovered && rl.IsMouseButtonPressed(rl.MouseLeftButton) {
				btn.Clicked = true
				btn.Scale = 0.95
				currentScreen = btn.Action
			}
		}

		// Begin drawing
		rl.BeginDrawing()

		// Clear background
		rl.ClearBackground(rl.RayWhite)

		// Draw appropriate screen
		switch currentScreen {
		case ScreenMenu:
			// Draw title
			rl.DrawText("Main Menu", int32(screenWidth/2)-50, startY-60, 30, rl.Black)

			// Draw buttons
			for _, btn := range buttons {
				// Calculate scaled bounds for animation
				scaledWidth := btn.Bounds.Width * btn.Scale
				scaledHeight := btn.Bounds.Height * btn.Scale
				scaledX := btn.Bounds.X - (scaledWidth-btn.Bounds.Width)/2
				scaledY := btn.Bounds.Y - (scaledHeight-btn.Bounds.Height)/2

				// Draw button with animation
				if btn.Hovered {
					rl.DrawRectangleRec(rl.Rectangle{X: scaledX, Y: scaledY, Width: scaledWidth, Height: scaledHeight}, rl.DarkGray)
				} else {
					rl.DrawRectangleRec(rl.Rectangle{X: scaledX, Y: scaledY, Width: scaledWidth, Height: scaledHeight}, btn.Color)
				}
				rl.DrawRectangleLinesEx(rl.Rectangle{X: scaledX, Y: scaledY, Width: scaledWidth, Height: scaledHeight}, 2, rl.Black)

				// Draw text
				textWidth := rl.MeasureText(btn.Text, 20)
				textX := int32(scaledX) + int32(scaledWidth)/2 - textWidth/2
				textY := int32(scaledY) + int32(scaledHeight)/2 - 10
				rl.DrawText(btn.Text, textX, textY, 20, rl.Black)
			}

		case ScreenThai:
			rl.DrawText("Thai Language Screen", screenWidth/2-100, screenHeight/2-20, 30, rl.Red)
			rl.DrawText("Press ESC to return to menu", screenWidth/2-120, screenHeight/2+30, 20, rl.Black)
			if rl.IsKeyPressed(rl.KeyEscape) {
				currentScreen = ScreenMenu
			}

		case ScreenKorean:
			rl.DrawText("Korean Language Screen", screenWidth/2-120, screenHeight/2-20, 30, rl.Blue)
			rl.DrawText("Press ESC to return to menu", screenWidth/2-120, screenHeight/2+30, 20, rl.Black)
			if rl.IsKeyPressed(rl.KeyEscape) {
				currentScreen = ScreenMenu
			}

		case ScreenJapanese:
			rl.DrawText("Japanese Language Screen", screenWidth/2-140, screenHeight/2-20, 30, rl.Green)
			rl.DrawText("Press ESC to return to menu", screenWidth/2-120, screenHeight/2+30, 20, rl.Black)
			if rl.IsKeyPressed(rl.KeyEscape) {
				currentScreen = ScreenMenu
			}
		}

		// Draw FPS counter
		rl.DrawFPS(10, 10)

		// End drawing
		rl.EndDrawing()
	}
}

// Required for raylib-go on some platforms
func init() {
	runtime.LockOSThread()
}
