package screens

import (
	"image/color"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/inpututil"
	"github.com/hajimehoshi/ebiten/v2/text/v2"
)

// Menu options
type TitleMenuOption int

const (
	TitleOptionContinue TitleMenuOption = iota
	TitleOptionNewGame
	TitleOptionSettings
	TitleOptionQuit
	titleOptionCount
)

// Screen modes
type titleScreenMode int

const (
	titleModeMain titleScreenMode = iota
	titleModeSettings
)

type TitleScreen struct {
	selectedIndex  int
	currentMode    titleScreenMode
	settingsScreen *SettingsScreen
	hasSaveGame    bool
}

func NewTitleScreen() *TitleScreen {
	return &TitleScreen{
		selectedIndex:  0,
		currentMode:    titleModeMain,
		settingsScreen: NewSettingsScreen(),
		hasSaveGame:    false,
	}
}

func (t *TitleScreen) SetFPSMonitor(enabled *bool) {
	t.settingsScreen.SetFPSMonitor(enabled)
}

func (t *TitleScreen) SetFPSCap(cap FPSCapSetting) {
	t.settingsScreen.SetFPSCap(cap)
}

func (t *TitleScreen) SetPortalVisibility(enabled *bool) {
	t.settingsScreen.SetPortalVisibility(enabled)
}

func (t *TitleScreen) SetRaycastSettings(enabled *bool, debugMode *bool) {
	t.settingsScreen.SetRaycastSettings(enabled, debugMode)
}

func (t *TitleScreen) SetHasSaveGame(hasSave bool) {
	t.hasSaveGame = hasSave
	if !hasSave && t.selectedIndex == 0 {
		t.selectedIndex = 1
	}
}

func (t *TitleScreen) Update() *TitleMenuOption {
	if t.currentMode == titleModeSettings {
		if t.settingsScreen.Update() {
			t.currentMode = titleModeMain
			t.selectedIndex = 0
		}
		return nil
	}

	if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) || inpututil.IsKeyJustPressed(ebiten.KeyW) {
		t.selectedIndex--
		if t.selectedIndex < 0 {
			t.selectedIndex = int(titleOptionCount) - 1
		}
		if t.selectedIndex == int(TitleOptionContinue) && !t.hasSaveGame {
			t.selectedIndex--
			if t.selectedIndex < 0 {
				t.selectedIndex = int(titleOptionCount) - 1
			}
		}
	}

	if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) || inpututil.IsKeyJustPressed(ebiten.KeyS) {
		t.selectedIndex++
		if t.selectedIndex >= int(titleOptionCount) {
			t.selectedIndex = 0
		}
		if t.selectedIndex == int(TitleOptionContinue) && !t.hasSaveGame {
			t.selectedIndex++
			if t.selectedIndex >= int(titleOptionCount) {
				t.selectedIndex = 0
			}
		}
	}

	if inpututil.IsKeyJustPressed(ebiten.KeyEnter) || inpututil.IsKeyJustPressed(ebiten.KeySpace) {
		selected := TitleMenuOption(t.selectedIndex)
		if selected == TitleOptionSettings {
			t.currentMode = titleModeSettings
			t.settingsScreen.Reset()
			return nil
		}
		return &selected
	}

	return nil
}

// Rendering
func (t *TitleScreen) Draw(screen *ebiten.Image, screenWidth, screenHeight int) {
	if t.currentMode == titleModeSettings {
		t.settingsScreen.Draw(screen, screenWidth, screenHeight, "SETTINGS")
		return
	}

	screen.Fill(color.RGBA{R: 20, G: 20, B: 30, A: 255})

	titleText := "LIL GUY"
	scale := 3.0
	charWidth := 7.0 * scale
	textWidth := float64(len(titleText)) * charWidth

	titleX := float64(screenWidth)/2 - textWidth/2
	titleY := float64(screenHeight) / 3

	op := &text.DrawOptions{}
	op.GeoM.Scale(scale, scale)
	op.GeoM.Translate(titleX, titleY-basicFaceAscent*scale)
	op.ColorScale.ScaleWithColor(color.RGBA{R: 210, G: 220, B: 255, A: 255})
	text.Draw(screen, titleText, basicFace, op)

	// Draw menu
	options := []string{"Continue", "New Game", "Settings", "Quit"}
	startY := screenHeight/2 + 10

	for i, option := range options {
		if i == int(TitleOptionContinue) && !t.hasSaveGame {
			continue
		}
		optionY := startY + (i * 50)
		optionX := (screenWidth / 2) - (len(option) * 7 / 2)

		// Dim Continue option if it exists but is disabled
		optionColor := color.RGBA{R: 180, G: 180, B: 200, A: 255}
		if i == int(TitleOptionContinue) && !t.hasSaveGame {
			optionColor = color.RGBA{R: 80, G: 80, B: 100, A: 255}
		}

		if i == t.selectedIndex {
			indicatorX := optionX - 20
			t.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, optionY)
			t.drawText(screen, option, color.RGBA{R: 255, G: 255, B: 100, A: 255}, optionX, optionY)
		} else {
			t.drawText(screen, option, optionColor, optionX, optionY)
		}
	}

	// Instructions
	hintText := "Use Arrow Keys/WASD to navigate, Enter/Space to select"
	hintX := (screenWidth / 2) - (len(hintText) * 7 / 2)
	hintY := screenHeight - 50
	t.drawText(screen, hintText, color.RGBA{R: 120, G: 120, B: 150, A: 255}, hintX, hintY)
}

func (t *TitleScreen) drawText(screen *ebiten.Image, txt string, clr color.Color, x, y int) {
	op := &text.DrawOptions{}
	op.GeoM.Translate(float64(x), float64(y)-basicFaceAscent)
	op.ColorScale.ScaleWithColor(clr)
	text.Draw(screen, txt, basicFace, op)
}

func (t *TitleScreen) Reset() {
	if t.hasSaveGame {
		t.selectedIndex = 0
	} else {
		t.selectedIndex = 1
	}
	t.currentMode = titleModeMain
	t.settingsScreen.Reset()
}
