package game

import (
	rl "github.com/gen2brain/raylib-go/raylib"
)

type LoginScreen struct {
	username      string
	password      string
	errorMessage  string
	isRegistering bool
	focusedField  int // 0 = username, 1 = password
}

func NewLoginScreen() *LoginScreen {
	return &LoginScreen{}
}

func (l *LoginScreen) Draw() {
	screenWidth := float32(rl.GetScreenWidth())
	screenHeight := float32(rl.GetScreenHeight())

	// Draw background
	rl.DrawRectangle(0, 0, int32(screenWidth), int32(screenHeight), rl.RayWhite)

	// Draw title
	title := "GoonScape"
	if l.isRegistering {
		title += " - Register"
	} else {
		title += " - Login"
	}
	titleSize := int32(40)
	titleWidth := rl.MeasureText(title, titleSize)
	rl.DrawText(title, int32(screenWidth/2)-titleWidth/2, 100, titleSize, rl.Black)

	// Draw input fields
	inputWidth := float32(200)
	inputHeight := float32(30)
	inputX := screenWidth/2 - inputWidth/2

	// Username field
	rl.DrawRectangleRec(rl.Rectangle{
		X: inputX, Y: 200,
		Width: inputWidth, Height: inputHeight,
	}, rl.LightGray)
	rl.DrawText(l.username, int32(inputX)+5, 205, 20, rl.Black)
	if l.focusedField == 0 {
		rl.DrawRectangleLinesEx(rl.Rectangle{
			X: inputX - 2, Y: 198,
			Width: inputWidth + 4, Height: inputHeight + 4,
		}, 2, rl.Blue)
	}

	// Password field
	rl.DrawRectangleRec(rl.Rectangle{
		X: inputX, Y: 250,
		Width: inputWidth, Height: inputHeight,
	}, rl.LightGray)
	masked := ""
	for range l.password {
		masked += "*"
	}
	rl.DrawText(masked, int32(inputX)+5, 255, 20, rl.Black)
	if l.focusedField == 1 {
		rl.DrawRectangleLinesEx(rl.Rectangle{
			X: inputX - 2, Y: 248,
			Width: inputWidth + 4, Height: inputHeight + 4,
		}, 2, rl.Blue)
	}

	// Draw error message
	if l.errorMessage != "" {
		msgWidth := rl.MeasureText(l.errorMessage, 20)
		rl.DrawText(l.errorMessage, int32(screenWidth/2)-msgWidth/2, 300, 20, rl.Red)
	}

	// Draw buttons
	buttonWidth := float32(100)
	buttonHeight := float32(30)
	buttonY := float32(350)

	// Login/Register button
	actionBtn := rl.Rectangle{
		X:      screenWidth/2 - buttonWidth - 10,
		Y:      buttonY,
		Width:  buttonWidth,
		Height: buttonHeight,
	}
	rl.DrawRectangleRec(actionBtn, rl.Blue)
	actionText := "Login"
	if l.isRegistering {
		actionText = "Register"
	}
	actionWidth := rl.MeasureText(actionText, 20)
	rl.DrawText(actionText,
		int32(actionBtn.X+actionBtn.Width/2)-actionWidth/2,
		int32(actionBtn.Y+5),
		20, rl.White)

	// Switch mode button
	switchBtn := rl.Rectangle{
		X:      screenWidth/2 + 10,
		Y:      buttonY,
		Width:  buttonWidth,
		Height: buttonHeight,
	}
	rl.DrawRectangleRec(switchBtn, rl.DarkGray)
	switchText := "Register"
	if l.isRegistering {
		switchText = "Login"
	}
	switchWidth := rl.MeasureText(switchText, 20)
	rl.DrawText(switchText,
		int32(switchBtn.X+switchBtn.Width/2)-switchWidth/2,
		int32(switchBtn.Y+5),
		20, rl.White)
}

func (l *LoginScreen) Update() (string, string, bool, bool) {
	// Handle input field focus
	if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
		mousePos := rl.GetMousePosition()
		screenWidth := float32(rl.GetScreenWidth())
		inputWidth := float32(200)
		inputX := screenWidth/2 - inputWidth/2

		// Check username field
		if mousePos.X >= inputX && mousePos.X <= inputX+inputWidth &&
			mousePos.Y >= 200 && mousePos.Y <= 230 {
			l.focusedField = 0
		}
		// Check password field
		if mousePos.X >= inputX && mousePos.X <= inputX+inputWidth &&
			mousePos.Y >= 250 && mousePos.Y <= 280 {
			l.focusedField = 1
		}

		// Check buttons
		buttonWidth := float32(100)
		if mousePos.Y >= 350 && mousePos.Y <= 380 {
			// Action button
			if mousePos.X >= screenWidth/2-buttonWidth-10 &&
				mousePos.X <= screenWidth/2-10 {
				return l.username, l.password, l.isRegistering, true
			}
			// Switch mode button
			if mousePos.X >= screenWidth/2+10 &&
				mousePos.X <= screenWidth/2+buttonWidth+10 {
				l.isRegistering = !l.isRegistering
			}
		}
	}

	// Handle text input
	key := rl.GetCharPressed()
	for key > 0 {
		if l.focusedField == 0 && len(l.username) < 12 {
			l.username += string(key)
		} else if l.focusedField == 1 && len(l.password) < 20 {
			l.password += string(key)
		}
		key = rl.GetCharPressed()
	}

	// Handle backspace
	if rl.IsKeyPressed(rl.KeyBackspace) {
		if l.focusedField == 0 && len(l.username) > 0 {
			l.username = l.username[:len(l.username)-1]
		} else if l.focusedField == 1 && len(l.password) > 0 {
			l.password = l.password[:len(l.password)-1]
		}
	}

	// Handle tab to switch fields
	if rl.IsKeyPressed(rl.KeyTab) {
		l.focusedField = (l.focusedField + 1) % 2
	}

	return "", "", false, false
}

func (l *LoginScreen) SetError(msg string) {
	l.errorMessage = msg
}
