package states

import (
	"fmt"
	"image/color"
	"math/rand"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/inpututil"
	"github.com/hajimehoshi/ebiten/v2/text"
	"golang.org/x/image/font"
	"src.robn.tv/MrDonuts/RGB/assets"
	geom "src.robn.tv/MrDonuts/RGB/geometry"
)

type GameOverState struct {
}

func (st *GameOverState) Enter() {
	fmt.Println("Entering Game Over State")
	assets.SilenceSongTracks()
	assets.SFXLibrary["gameOver"].Rewind()
	assets.SFXLibrary["gameOver"].Play()

}

func (st *GameOverState) Exit() {
	fmt.Println("Exiting Game OverState")

}

func (st *GameOverState) Update() {
	if inpututil.IsKeyJustPressed(ebiten.KeyN) {
		geom.ResetPlayerValues()
		geom.BuildGrid(rand.Intn(5+geom.CurrentLevel)+2, rand.Intn(5+geom.CurrentLevel)+2)
		CurrTrack = 1
		assets.SongTracks[CurrTrack].SetVolume(0.7) //turn up track 1
		SM.Transition("load")

	}

}

func (st *GameOverState) Draw(screen *ebiten.Image) {
	w, h := ebiten.WindowSize()

	gameOverStr := "Game Over"
	face := assets.LoadFontFace("fonts/robot.otf", 30)
	stringWidth := font.MeasureString(face, gameOverStr).Ceil()
	text.Draw(screen, gameOverStr, face, w/2-stringWidth/2, h/2, color.White)

	newGameStr := "(press N for new game)"
	face = assets.LoadFontFace("fonts/robot.otf", 12)
	stringWidth = font.MeasureString(face, newGameStr).Ceil()
	text.Draw(screen, newGameStr, face, w/2-stringWidth/2, h-int(0.1*float64(h)), color.White)

}
